package octez-l2-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file store_utils.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com>               *)
(* Copyright (c) 2022 Trili Tech, <contact@trili.tech>                       *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Store_sigs

let decode encoding encoded_value =
  let open Result_syntax in
  let res = Data_encoding.Binary.of_bytes encoding encoded_value in
  Result.bind_error res (fun e -> tzfail @@ Store_errors.Decoding_error e)

let encode encoding value =
  let open Result_syntax in
  let res = Data_encoding.Binary.to_bytes encoding value in
  Result.bind_error res (fun e -> tzfail @@ Store_errors.Encoding_error e)

module Make (B : BACKEND) = struct
  type 'a t = 'a B.t

  module Make_map (S : STORAGE_INFO) (K : KEY) (V : VALUE) = struct
    type key = K.key

    type 'a store = 'a B.t

    type value = V.value

    let path = S.path

    let make_key key = B.make_key_path path @@ K.to_path_representation key

    let mem store key = B.mem store (make_key key)

    let decode_value = decode V.encoding

    let encode_value = encode V.encoding

    let get store key =
      let open Lwt_result_syntax in
      let* e = B.get store (make_key key) in
      Lwt.return @@ decode_value e

    let find store key =
      let open Lwt_result_syntax in
      let* value = B.find store (make_key key) in
      Lwt.return @@ Option.map_e decode_value value

    let find_with_default store key ~on_default =
      let open Lwt_result_syntax in
      let* value = find store key in
      return @@ Option.value_f value ~default:on_default
  end

  module Make_updatable_map (S : STORAGE_INFO) (K : KEY) (V : VALUE) = struct
    include Make_map (S) (K) (V)

    let add store key value =
      let open Lwt_result_syntax in
      let*? encoded_value = encode_value value in
      B.set store (make_key key) encoded_value
  end

  module Make_append_only_map (S : STORAGE_INFO) (K : KEY) (V : VALUE) = struct
    include Make_map (S) (K) (V)

    let add store key value =
      let open Lwt_result_syntax in
      let* existing_value = find store key in
      let*? encoded_value = encode_value value in
      match existing_value with
      | None -> B.set store (make_key key) encoded_value
      | Some existing_value ->
          let*? encoded_existing_value = encode_value existing_value in
          (* To be robust to interruption in the middle of processes,
             we accept to redo some work when we restart the node.
             Hence, it is fine to insert twice the same value for a
             given value. *)
          if not (Bytes.equal encoded_existing_value encoded_value) then
            tzfail
            @@ Store_errors.Cannot_overwrite_key_in_store
                 {
                   name = B.name;
                   key = B.path_to_string @@ make_key key;
                   old_value = String.of_bytes encoded_existing_value;
                   new_value = String.of_bytes encoded_value;
                 }
          else return_unit
  end

  module Make_mutable_value (S : STORAGE_INFO) (V : VALUE) = struct
    type 'a store = 'a B.t

    type value = V.value

    let path_key = S.path

    let decode_value = decode V.encoding

    let encode_value = encode V.encoding

    let set store value =
      let open Lwt_result_syntax in
      let*? encoded_value = encode_value value in
      B.set store path_key encoded_value

    let get store =
      let open Lwt_result_syntax in
      let* value = B.get store path_key in
      Lwt.return @@ decode_value value

    let find store =
      let open Lwt_result_syntax in
      let* value = B.find store path_key in
      Lwt.return @@ Option.map_e decode_value value
  end

  module Make_nested_map
      (S : STORAGE_INFO)
      (K1 : KEY)
      (K2 : COMPARABLE_KEY)
      (V : VALUE) =
  struct
    type 'a store = 'a B.t

    type primary_key = K1.key

    type secondary_key = K2.key

    type value = V.value

    let path = S.path

    module Secondary_key_map = Map.Make (struct
      type t = K2.key

      let compare = K2.compare
    end)

    module Map_as_value = struct
      type value = V.value Secondary_key_map.t

      let encoding =
        Data_encoding.conv
          (fun map -> Secondary_key_map.bindings map)
          (fun bindings -> Secondary_key_map.of_seq @@ List.to_seq bindings)
          Data_encoding.(
            list @@ obj2 (req K2.name K2.encoding) (req V.name V.encoding))

      let encode_value = encode V.encoding

      let name = ""
    end

    module M = Make_updatable_map (S) (K1) (Map_as_value)

    (* Return the bindings associated with a primary key. *)
    let list_secondary_keys_with_values store ~primary_key =
      let open Lwt_result_syntax in
      let+ slots_map = M.find store primary_key in
      Option.fold ~none:[] ~some:Secondary_key_map.bindings slots_map

    (* Check whether the updatable store contains an entry
       for the primary_key, which itself contains a
       binding whose key is ~secondary_key. *)
    let mem store ~primary_key ~secondary_key =
      let open Lwt_result_syntax in
      let* primary_key_binding_exists = M.mem store primary_key in
      if not primary_key_binding_exists then return_false
      else
        let+ secondary_key_map = M.get store primary_key in
        Secondary_key_map.mem secondary_key secondary_key_map

    (* Unsafe call. The existence of a value for
       primary and secondary key in the store must be
       checked before invoking this function. *)
    let get store ~primary_key ~secondary_key =
      let open Lwt_result_syntax in
      let* secondary_key_map = M.get store primary_key in
      match Secondary_key_map.find secondary_key secondary_key_map with
      | None ->
          let key = K1.to_path_representation primary_key in
          tzfail @@ Store_errors.Cannot_read_key_from_store {name = B.name; key}
      | Some value -> return value

    let find store ~primary_key ~secondary_key =
      let open Lwt_result_syntax in
      let* binding_exists = mem store ~primary_key ~secondary_key in
      if binding_exists then
        let+ value = get store ~primary_key ~secondary_key in
        Some value
      else return_none

    (* Returns the set of keys from the bindings of
       ~primary_key in the store. *)
    let list_secondary_keys store ~primary_key =
      let open Lwt_result_syntax in
      let+ secondary_keys_with_values =
        list_secondary_keys_with_values store ~primary_key
      in
      List.map (fun (key, _value) -> key) secondary_keys_with_values

    (* Returns the set of values from the bindings of
       ~primary_key in the store. *)
    let list_values store ~primary_key =
      let open Lwt_result_syntax in
      let+ secondary_keys_with_values =
        list_secondary_keys_with_values store ~primary_key
      in
      List.map (fun (_key, value) -> value) secondary_keys_with_values

    (* Updates the entry of the updatable map with key ~primary_key
       by adding to it a binding with key ~secondary_key and
       value `value`.*)
    let add store ~primary_key ~secondary_key value =
      let open Lwt_result_syntax in
      let* value_map = M.find store primary_key in
      let value_map = Option.value ~default:Secondary_key_map.empty value_map in
      let*? value_can_be_added, encoded_old_value, encoded_value =
        let open Result_syntax in
        let* encoded_value = Map_as_value.encode_value value in
        match Secondary_key_map.find secondary_key value_map with
        | None -> return (true, None, encoded_value)
        | Some old_value ->
            let* encoded_old_value = Map_as_value.encode_value old_value in
            return
            @@ ( Bytes.equal encoded_old_value encoded_value,
                 Some encoded_old_value,
                 encoded_value )
      in
      if value_can_be_added then
        let updated_map = Secondary_key_map.add secondary_key value value_map in
        M.add store primary_key updated_map
      else
        let key =
          B.path_to_string @@ B.make_key_path S.path
          @@ K1.to_path_representation primary_key
        in
        match encoded_old_value with
        | None ->
            tzfail
            @@ Store_errors.Cannot_write_key_value_pair_to_store
                 {name = B.name; key; value = String.of_bytes encoded_value}
        | Some encoded_old_value ->
            tzfail
            @@ Store_errors.Cannot_overwrite_key_in_store
                 {
                   name = B.name;
                   key;
                   old_value = String.of_bytes encoded_old_value;
                   new_value = String.of_bytes encoded_value;
                 }
  end
end
OCaml

Innovation. Community. Security.