package plebeia

  1. Overview
  2. Docs
Functional storage using Merkle Patricia tree

Install

Dune Dependency

Authors

Maintainers

Sources

plebeia-2.2.0.tar.gz
md5=7191dbbd3057df0a78032b560039bb59
sha512=f09790dfa65a6c8dc0da9618123d93f145c16c3b5be719dad04114bbe95a7e94697cacf2c6fb5b50c14408f864954dbf8d47e5994093623eb77f488bdf5c4205

doc/src/plebeia/deep.ml.html

Source file deep.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2019,2020 DaiLambda, Inc. <contact@dailambda.jp>            *)
(*                                                                           *)
(* 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 Result_lwt.Infix
open Cursor

type Error.t += Segments_cannot_be_empty

let () = Error.register_printer (function
    | Segments_cannot_be_empty ->
        Some "Segments cannot be empty"
    | _ -> None)

(** Multi Bud level interface *)
let deep ~go_up ~create_subtrees cur segs f =
  let rec ups cur = function
    | [] -> Ok cur
    | _seg::segs -> parent cur >>? fun cur -> ups cur segs
  in
  let rec aux hist cur = function
    | [] -> assert false
    | [seg] ->
        f cur seg >>? fun (cur, res) ->
        if go_up then
          ups cur hist >>? fun cur ->
          Ok (cur, res)
        else
          Ok (cur, res)
    | seg::segs ->
        (if create_subtrees then subtree_or_create else subtree) cur seg >>? fun cur ->
        aux (seg::hist) cur segs
  in
  (* XXX We may have an Error *)
  if segs = [] then Error Segments_cannot_be_empty
  else aux [] cur segs

let get cur segs =
  deep ~go_up:true ~create_subtrees:false cur segs get

let get_value cur segs =
  deep ~go_up:true ~create_subtrees:false cur segs get_value

let upsert cur segs v =
  deep ~go_up:true ~create_subtrees:true cur segs (fun cur seg ->
      upsert cur seg v >|? fun cur -> cur, ()) >|? fst

let insert cur segs v =
  deep ~go_up:true ~create_subtrees:true cur segs (fun cur seg ->
      insert cur seg v >|? fun cur -> cur, ()) >|? fst

let update cur segs v =
  deep ~go_up:true ~create_subtrees:false cur segs (fun cur seg ->
      update cur seg v >|? fun cur -> cur, ()) >|? fst

let delete cur segs =
  deep ~go_up:true ~create_subtrees:false cur segs (fun cur seg ->
      delete cur seg >|? fun cur -> cur, ()) >|? fst

let delete' cur segs =
  deep ~go_up:true ~create_subtrees:false cur segs (fun cur seg ->
      delete' cur seg >|? fun cur -> cur, ()) >|? fst

(* XXX This function should be deprecated
   because of its semantics complexity *)
let delete_and_clean_empty cur segs =
  deep ~go_up:false ~create_subtrees:false cur segs (fun cur seg ->
      match Cursor.delete cur seg with
      | Ok cur -> Ok (cur, ())
      | Error _ -> Ok (cur, ())) >|? fst
  >>? Cursor.remove_empty_bud

let delete_and_clean_empty' cur segs =
  let rec loop c = function
    | [] -> assert false
    | [seg] -> Cursor.delete c seg
    | seg::segs ->
        subtree c seg
        >>? fun c -> loop c segs
        >>? fun c ->
        let empty = match snd @@ Cursor.view c with
          | Bud (None, _, _) -> true
          | Bud _ -> false
          | _ -> assert false
        in
        Cursor.go_up_to_bud c
        >>? fun c ->
        if empty then Cursor.delete c seg
        else Ok c
  in
  loop cur segs >>? fun c ->
  (* If the Bud at the original point becomes empty,
     the function fails.  Exception: root *)
  match Cursor.view c with
  | Cursor (Top, _, _, _), _ -> Ok c
  | _, Bud (None, _, _) -> Error (Cursor.Write "delete_and_clean_empty': cannot delete the starting point")
  | _, Bud _ -> Ok c
  | _ -> assert false

let create_subtree ~create_subtrees cur segs =
  deep ~go_up:true ~create_subtrees cur segs (fun cur seg ->
      create_subtree cur seg >|? fun cur -> (cur, ())) >|? fst

let subtree cur segs =
  deep ~go_up:false ~create_subtrees:false cur segs (fun cur seg ->
      subtree cur seg >|? fun cur -> (cur, ())) >|? fst

let subtree_or_create ~create_subtrees cur segs =
  deep ~go_up:false ~create_subtrees cur segs (fun cur seg ->
      subtree_or_create cur seg >|? fun cur -> (cur, ())) >|? fst

(* bud copy by link *)
let copy' ?(allow_overwrite=false) ?(only_bud=false) ~create_subtrees ~copy_ref cur segs1 segs2 =
  (* the absolute position *)
  (* find the source *)
  deep ~go_up:false ~create_subtrees:false cur segs1
    (fun cur seg -> access_gen cur seg >>? function
       | Reached (cur, (Bud _ as v)) -> Ok (cur, v) (* Bud is copiable anytime *)
       | Reached (cur, v) when not only_bud -> Ok (cur, v) (* Not only Bud but also internals when only_bud= false *)
       | res -> error_access res) >>? fun (src_cur, v) ->

  (* the absolute source postion and remember it for copy GC optimization *)
  let gpath1 = Cursor.path_of_cursor src_cur in
  (* XXX gsegs1 = segs1 *)
  assert (Segment.equal_list segs1 (Path.to_segments gpath1));

  (* gsegs1 may not exist in the copy_ref *)
  (* we record the copy only if the source segment exists in [copy_ref] *)
  let src_exists =
    match
      deep ~go_up:false ~create_subtrees:false copy_ref (Path.to_segments gpath1)
        (fun cur seg -> access_gen cur seg >>? function
           | Reached (cur, _) -> Ok (cur, true)
           | _ -> Ok (cur, false))
    with
    | Ok (_, b) -> b
    | _ -> false
  in

  let Cursor (trail, n, context, info) = cur in
  let cur =
    _Cursor(trail, n, context,
            if src_exists then { copies= gpath1 :: info.copies }
            else info)
  in

  (* make a link *)
  deep ~go_up:true ~create_subtrees cur segs2
    (fun cur seg ->
       alter cur seg (function
           | Some _ when not allow_overwrite -> Error (Cursor.Write "a node already presents for this segment")
           | _ -> Ok (View v)
         ) >>? fun cur ->
       Ok (cur, ())) >|? fst

(* bud copy by link *)
let copy ?allow_overwrite ~create_subtrees cur segs1 segs2 =
  copy' ?allow_overwrite ~only_bud:true ~create_subtrees ~copy_ref:cur cur segs1 segs2

let link n cur segs =
  deep ~go_up:true ~create_subtrees:true
    cur
    segs
    (fun c seg ->
       (* XXX this alter has once inserted Disk (Maybe_Extender)
          under an extender and crashed program.

          We here workaround this problem to force to view [n]
       *)
       let Cursor (_, _, context, _) = c in
       (* [n] must be under [context] *)
       let v = Node_storage.view context n in
       let n = Node_type.View v in
       alter c seg (function
           | Some _ -> assert false
           | None -> Ok n) >|? fun c -> (c, ()))
  >|? fst

let alter cur segs f =
  deep ~go_up:true ~create_subtrees:true
    cur
    segs
    (fun c seg -> alter c seg f >|? fun c -> (c, ()))
  >|? fst
OCaml

Innovation. Community. Security.