Source file vertex_intf.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
(*
 * Copyright (c) 2021 Tarides <contact@tarides.com>
 * Copyright (c) 2021 Gabriel Belouze <gabriel.belouze@ens.psl.eu>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

module type S = sig
  type t
  (** The type for vertices. For leafs its a map (key * value). For nodes it maps a [key] interval
      to an [address]. *)

  type key

  type value

  type address

  type store

  val create : store -> Field.kind -> address -> t
  (** [create s k p] creates a new empty vertex (of kind either Leaf or Node), stored at address [p]
      in [s]. *)

  val load : store -> address -> t
  (** [load s p] loads the table stored at address [p] in [s]. *)

  val reconstruct : t -> Field.kind -> (key * value) list -> unit
  (** [reconstruct t kind kvs] overwrite [t] with the list of bindings [kvs] which is assumed to be
      sorted. *)

  val migrate : string list -> Field.kind -> string
  (** [migrate kvs kind] is the representation of the key-value association list [kvs] in a vertex
      of type [kind] *)

  val leftmost : t -> key
  (** [leftmost t] is the smallest key bound in [t] *)

  val split : t -> address -> key * t
  (** [split t p] moves half of the bindings from [t] (with keys larger than a pivot, which is the
      middle key bounded in [t]) to a new table [t_mv] stored at address [p]. Return [pivot, t_mv]. *)

  val replace : t -> key -> key -> unit
  (** [replace t k1 k2] replaces key [k1] in [t] with [k2] *)

  val add : t -> key -> value -> unit
  (** [add t k v] adds a binding from [k] to [v] in [t]. Contrary to [Map.add], previous bindings
      from [k] are not hidden, but deleted. *)

  val find : t -> key -> value
  (** [find t k] returns the current binding of [k] in [t], or raises [Not_found] if no such binding
      exists. *)

  type with_neighbour = {
    main : key * value;
    neighbour : (key * value) option;
    order : [ `Lower | `Higher ];
  }

  val find_with_neighbour : t -> key -> with_neighbour
  (** [find_with_neighbour t k] as [find t k] but also returns a neighbour of [k] in [t] (the
      right-most one when [k] has neighbours in both directions). *)

  val mem : t -> key -> bool
  (** [mem t k] checks if [k] is bound in [t] in case the vertex is a leaf and raises
      [Invalid_argument] if its a node. *)

  val iter : t -> (key -> value -> unit) -> unit
  (** [iter t func] applies [func key value] on every bindings [(key, value)] stored in [t] *)

  val fold_left : ('a -> key * value -> 'a) -> 'a -> t -> 'a

  val merge : t -> t -> [ `Partial | `Total ] -> unit
  (** [merge t1 t2 mode] merges bindings in [t1] and [t2]. A partial merge merely redistribute the
      keys evenly among the nodes, a total merge moves all keys from [t2] to [t1]. It is assumed,
      and relied upon, that all keys from [t2] are greater than every key from [t1]. *)

  val remove : t -> key -> unit
  (** [remove t k] removes the binding of [k] in [t], or raises [Not_found] if no such binding
      exists. *)

  val length : t -> int
  (** [length t] is the number of keys bound in [t]. It takes constant time. *)

  val depth : t -> int
  (** [depth t] is the depth of the vertex [t]. *)

  val pp : t Fmt.t
  (** [pp ppf t] outputs a human-readable representation of [t] to the formatter [ppf] *)
end

module type VALUE = sig
  type t

  val set : marker:(unit -> unit) -> bytes -> off:int -> t -> unit

  val get : bytes -> off:int -> t

  val size : int

  val pp : t Fmt.t

  val kind : [ `Leaf | `Node ]
end

module type LEAFMAKER = functor
  (Params : Params.S)
  (Store : Store.S)
  (Key : Data.K)
  (Value : Data.V)
  ->
  S
    with type key := Key.t
     and type value := Value.t
     and type store = Store.t
     and type address = Store.address

module type NODEMAKER = functor (Params : Params.S) (Store : Store.S) (Key : Data.K) ->
  S
    with type key := Key.t
     and type value := Field.MakeCommon(Params).Address.t
     and type store = Store.t
     and type address = Store.address

module type MAKER = functor (Params : Params.S) (Store : Store.S) (Key : Data.K) (Value : VALUE) ->
  S
    with type key := Key.t
     and type value := Value.t
     and type store = Store.t
     and type address = Store.address

module type Vertex = sig
  module LeafMake : LEAFMAKER

  module NodeMake : NODEMAKER
end