Skip to content
This repository
Newer
Older
100644 381 lines (320 sloc) 15.716 kb
fccc6851 » MLstate
2011-06-21 Initial open-source release
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (*
19 @author Louis Gesbert
20 **)
21 (** This module describes the signature of database implementations available to
22 client. It is shared by local, network and distributed implementations.
23
24 Only contains types, a sig and a signed module: no mli needed.
25 *)
26
27 (** {2 Exported sub-modules and types} *)
28
29 module Data = DataImpl
30 module Structure = Badop_structure
31 module Key = Badop_structure.Key
32 module Path = Badop_structure.Path
33 module Dialog = Badop_lib.Dialog
34 module Node_property = Badop_structure.Node_property
35
36 (** Some shortcuts to very frequent types *)
37
38 type data = Data.t
39 type key = Structure.Key.t
40 type path = Structure.Path.t
41 type ('which, 'q, 'r) dialog = ('which, 'q, 'r) Dialog.t
42
43 (** {2 Shared types for requests} *)
44
45 (** Optional start point and number of results. 0 is unlimited. Negative means
46 go down from the start. When start is None, goes from the beginning or the
47 end, respectively, if n is >=0 or <0. *)
48 type 'a range = 'a option * int (* * 'a option -- TODO: add end point option, we need it alongside "max results" *)
49
50 (* TODO
51 (** This type describes a subset of a set of values, and is used by queries
52 to restrict the results returned. *)
53 type 'a range = {
54 first: 'a option; (** The value at which the results should start; from the beginning/end if [None] **)
55 last: 'a option; (** Stop after reaching that value if specified *)
56 count: int; (** Absolute value gives the maximum number of results wanted (unbounded if 0)
57 Sign gives the way of the traversal:
58 - if [count >= 0] go UP from [start] (or beginning if not specified)
59 - if [count < 0] go DOWN from [start] (or end if not specified) *)
60 (** All requests should guarantee that
61 - all results are within [\[first, last\]], in increasing order, if [count >= 0]
62 - all results are within [\[last, first\]], in decreasing order, if [count < 0] *)
63 }
64 *)
65
66 (** All the operations that query the db (generic version, for all
67 backends). They operate within a given transaction and at a given path. *)
68 type ('which,'revision) generic_read_op =
69 | Stat of ('which, unit,
70 path * 'revision option * [`Data|`Link|`Unset]) dialog
71 (** Checks the path, returns the path after unwinding all links
72 (including the ones inside copies), except the one in the node
73 at the end of the path (if the node contains a link). Does not
74 unwind copies (but finds and unwinds links inside them).
75 Also returns the kind of the last node at so unwound path.
76 Also returns the last revision when the node was modified,
77 where None means "modified in the current transaction"
78 and the node to determine the revision is taken after unwinding all,
79 including the last one, links on the path.
80 The Absent value is returned when any key of the path
81 points to a non-existing node, including when
82 the links or copies on the path are dangling,
83 but not when the last node contains a dangling link. *)
84 | Contents of ('which, unit, data) dialog
85 (** Returns the data contained in that node *)
86 | Children of ('which, key range, path list) dialog
87 (** Returns the paths of the children of the node *)
88 | Revisions of ('which, 'revision range, ('revision * Time.t) list) dialog
89 (** Returns the list of revisions for a given node, with timestamps *)
90 | Search of ('which, string list * int range, key list) dialog
91 (** Returns a list of keys from the path, which subtrees match the
92 given words. Ordered by decreasing relevance. *)
93
94 (** All the operations that write to the db (generic version, for all
95 backends). They operate in a given transaction, at a given path and return
96 the modified transaction *)
97 type ('which,'transaction,'revision) generic_write_op =
98 | Set of ('which, data, 'transaction) dialog
99 (** Write some data into a node *)
100 | Clear of ('which, unit, 'transaction) dialog
101 (** Removes a node from the database, making its subtree unreachable *)
102 | Link of ('which, path, 'transaction) dialog
103 (** Create a (symbolic) link to [path] *)
104 | Copy of ('which, path * 'revision option, 'transaction) dialog
105 (** Copy the whole subtree at [(path,revision)] *)
106
107 type 'a answer = [
108 | `Answer of 'a
109 | `Absent
110 | `Linkto of path
111 ]
112
113 (** A type for the introspection of a running badop *)
114 type status =
115 | Local of string (* path where the files are stored *)
8e93a4de » nrs135
2011-06-21 [feature] Badop_light: Added Light to status.
116 | Light of string (* light version, path where the dbm files are stored *)
fccc6851 » MLstate
2011-06-21 Initial open-source release
117 | Client of Unix.inet_addr * (Unix.inet_addr * int) * status (* local, remote, remote status *)
118 | Layer of string * status
119 | Layer_multi of string * status list
120 | Other of string
121
122 type local_options =
123 { path : string; (** path *)
124 revision : int option; (** recover db at revision *)
125 restore : bool option; (** restore, and do or not a backup in case of recover*)
126 dot : bool; (** output a the internal representation of the db in a dot each commit *)
127 readonly : bool; (** open the database on readonly mode *)
128 }
129
7e07d58e » nrs135
2011-06-14 [feature] Badop_light: First version.
130 type light_options =
131 { lpath : string; (** path *)
85e8f7e5 » nrs135
2011-07-06 [feature] database: Added ondemand and max_size options to db_light.
132 ondemand : bool option; (** no preload, read disk on access *)
2f448a86 » nrs135
2011-08-08 [feature] database: Added direct mode and functorised db_light. Massi…
133 direct : bool option; (** no memory cache, all accesses to/from disk *)
85e8f7e5 » nrs135
2011-07-06 [feature] database: Added ondemand and max_size options to db_light.
134 max_size : int option; (** store node data in files if larger than this value *)
7e07d58e » nrs135
2011-06-14 [feature] Badop_light: First version.
135 }
136
fccc6851 » MLstate
2011-06-21 Initial open-source release
137 type options =
138 | Options_Local of local_options
6ff7da0f » Louis Gesbert
2011-06-28 [fix] database: adding the possibility to automatically attempt to re…
139 | Options_Client of Scheduler.t * (Unix.inet_addr * int) * (unit -> [ `retry of Time.t | `abort ])
140 (** scheduler, server, on_disconnect *)
7e07d58e » nrs135
2011-06-14 [feature] Badop_light: First version.
141 | Options_Light of light_options
fccc6851 » MLstate
2011-06-21 Initial open-source release
142 | Options_Debug of string * options (** debug line prefix, backend options *)
143 | Options_Dispatcher of int * options list (** flat-replication factor, backends options *)
144
145 (** {2 The shared DB interface}
146
147 For implementations of this interface, check:
148 - Badop_db3: a binding to the simpler, local database
149 - Badop_db4: a binding to the deprecated distributed fork of db3 prototype
150 - Badop_client: forwards requests to a remote server
151 - Badop_dispatcher: distributes requests among a set of backends
152 - more to come ? We may have a Badop_replicated too, for two-level
153 replication handling.
154
155 @inline doc
156 *)
157 module type S = sig
158
159 type database
160 type transaction
161 type revision
162
163 val open_database: options -> database Cps.t
164 val close_database: database -> unit Cps.t
165 val status: database -> status Cps.t
166
167 (** Transaction-handling functions are grouped in this module *)
168 module Tr : sig
3321f323 » Louis Gesbert
2011-06-29 [enhance] database: fatal database errors now trigger the fail-transa…
169 (** Takes a hook that can be triggered at any point during the life of the
170 transaction if a fatal database error occurs (disconnection...) *)
171 val start: database -> (exn -> unit) -> transaction Cps.t
fccc6851 » MLstate
2011-06-21 Initial open-source release
172
3321f323 » Louis Gesbert
2011-06-29 [enhance] database: fatal database errors now trigger the fail-transa…
173 val start_at_revision: database -> revision -> (exn -> unit) -> transaction Cps.t
fccc6851 » MLstate
2011-06-21 Initial open-source release
174
175 (** In [prepare] and [commit], the returned boolean is [true] for success. *)
176 val prepare: transaction -> (transaction * bool) Cps.t
177
178 val commit: transaction -> bool Cps.t
179
180 (** The abort operation may be used on a running or prepared transaction *)
181 val abort: transaction -> unit Cps.t
182 end
183
184 (** All the operations that query the db (specialised version for this backend) *)
185 type 'which read_op = ('which,revision) generic_read_op
186
187 (** All the operations that write to the db (specialised version for this backend) *)
188 type 'which write_op = ('which,transaction,revision) generic_write_op
189
190 (** Generic read function to query the database. Example of use: {[
191 read tr path (Contents (query ()))
192 (function
193 | `Answer (Contents resp) -> Some (response resp) |> k
194 | `Answer _ -> assert false
195 | `Absent -> None |> k)
196 ]}
197 *)
198 val read: transaction -> path -> Dialog.query read_op -> Dialog.response read_op answer Cps.t
199
200 (** Generic write function to query the database *)
201 val write: transaction -> path -> Dialog.query write_op -> Dialog.response write_op Cps.t
202
203 (** Performing whole lists of writes at once, atomically. *)
204 val write_list: transaction -> (path * Dialog.query write_op) list -> transaction Cps.t
205
206 (** Extract node properties from given schema, use it on the db *)
207 val node_properties : database -> Node_property.config -> unit Cps.t
208
209 (** Todo: add combiners for multiple reads (possibly chained), multiple writes
210 and chained reads/writes *)
211
212 module Debug : sig
213 val revision_to_string: revision -> string
214 end
215 end
216
217 (** This module provides helper functions on the shared types above *)
218 module Aux : sig
219
220 (** Helper functions to extract and set back the resulting transaction from write operations *)
221 val result_transaction: (Dialog.response,'transaction,'revision) generic_write_op -> 'transaction
222 val respond_set_transaction:
223 (Dialog.query,'transaction,'revision) generic_write_op -> 'transaction ->
224 (Dialog.response,'transaction,'revision) generic_write_op
225
226 (** Helper functions to apply transformations inside read/write operations (for
227 use by database engines, esp. wrapping ones) *)
228
229 val map_range: ('a -> 'b Cps.t) -> 'a range -> 'b range Cps.t
230
231 val map_read_op:
232 revision:('revision1 -> 'revision2 Cps.t) ->
233 ('which, 'revision1) generic_read_op ->
234 ('which, 'revision2) generic_read_op Cps.t
235
1db6f116 » nrs135
2011-06-24 [fix] database: Fixed problem with signed ints in Encode_light.
236 val map_read_list_op:
237 revision:('revision1 -> 'revision2 Cps.t) ->
238 ('which, 'revision1) generic_read_op list ->
239 ('which, 'revision2) generic_read_op list Cps.t
240
fccc6851 » MLstate
2011-06-21 Initial open-source release
241 val map_write_op:
242 transaction:('transaction1 -> 'transaction2 Cps.t) ->
243 revision:('revision1 -> 'revision2 Cps.t) ->
244 ('which, 'transaction1, 'revision1) generic_write_op ->
245 ('which, 'transaction2, 'revision2) generic_write_op Cps.t
246
247 val map_write_list_op:
248 transaction:('transaction1 -> 'transaction2 Cps.t) ->
249 revision:('revision1 -> 'revision2 Cps.t) ->
250 ('which, 'transaction1, 'revision1) generic_write_op list ->
251 ('which, 'transaction2, 'revision2) generic_write_op list Cps.t
252
0f295898 » Louis Gesbert
2011-07-01 [fix] badop: fixed closure comparison during command-line parsing
253 (** checks if the two sets of options may conflict (bind to the same database twice) *)
254 val options_conflict: options -> options -> bool
255
fccc6851 » MLstate
2011-06-21 Initial open-source release
256 (** Printers and debug helpers *)
257
258 val path_to_string: path -> string
259
260 end = struct
261
262 module D = Badop_lib
666a08e7 » nrs135
2011-06-23 [feature] database: Implemented read cache update on write.
263 let (@>) = Cps.Ops.(@>)
264 let (|>) = Cps.Ops.(|>)
fccc6851 » MLstate
2011-06-21 Initial open-source release
265
266 (** Helper function to extract the resulting transaction from write operations *)
267 let result_transaction write_op_response =
268 match write_op_response with
269 | Set (D.Response tr) | Clear (D.Response tr) | Link (D.Response tr) | Copy (D.Response tr) -> tr
270 | Set (D.Query _) | Clear (D.Query _) | Link (D.Query _) | Copy (D.Query _) -> assert false
271
272 let respond_set_transaction write_op_query tr =
273 match write_op_query with
274 | Set q -> Set (D.Dialog_aux.respond q tr)
275 | Clear q -> Clear (D.Dialog_aux.respond q tr)
276 | Link q -> Link (D.Dialog_aux.respond q tr)
277 | Copy q -> Copy (D.Dialog_aux.respond q tr)
278
279
280 (** Helper functions to apply transformations inside read/write operations (for
281 use by database engines, esp. wrapping ones) *)
282
283 let map_range f r k = match r with
284 | (Some x, len) -> f x @> fun x -> (Some x, len) |> k
285 | None, len -> (None, len) |> k
286
287 let map_read_op
288 ~(revision: 'revision1 -> 'revision2 Cps.t)
289 (op: ('which, 'revision1) generic_read_op)
290 : ('which, 'revision2) generic_read_op Cps.t =
291 fun k -> match op with
292 | Stat dialog ->
293 D.Dialog_aux.map_dialog
294 ~query:(fun () k -> () |> k)
295 ~response:(fun d k ->
296 (fun (path, rev_opt, kind) k ->
297 Cps.Option.map revision rev_opt
298 @> fun rev_opt -> (path, rev_opt, kind) |> k)
299 d
300 @> k)
301 dialog
302 @> fun dialog -> Stat dialog |> k
303 | Contents dialog ->
304 D.Dialog_aux.map_dialog ~query:(fun () k -> () |> k) ~response:(fun d k -> d |> k) dialog
305 @> fun dialog -> Contents dialog |> k
306 | Children dialog ->
307 D.Dialog_aux.map_dialog ~query:(fun r k -> r |> k) ~response:(fun l k -> l |> k) dialog
308 @> fun dialog -> Children dialog |> k
309 | Revisions dialog ->
310 D.Dialog_aux.map_dialog
311 ~query:(fun r k -> map_range revision r @> k)
312 ~response:(fun l k ->
313 Cps.List.map
314 (fun (r,ts) k ->
315 revision r @> fun r -> (r, ts) |> k)
316 l
317 @> k)
318 dialog
319 @> fun dialog -> Revisions dialog |> k
320 | Search dialog ->
321 D.Dialog_aux.map_dialog ~query:(fun x k -> x |> k) ~response:(fun l k -> l |> k) dialog
322 @> fun dialog -> Search dialog |> k
323
666a08e7 » nrs135
2011-06-23 [feature] database: Implemented read cache update on write.
324 let map_read_list_op
325 ~(revision: 'revision1 -> 'revision2 Cps.t)
326 (l_op: ('which, 'revision1) generic_read_op list)
327 : ('which, 'revision2) generic_read_op list Cps.t =
328 fun k ->
329 let rd acc op k =
330 map_read_op ~revision op @> fun op -> op::acc |> k
331 in
332 Cps.List.fold rd [] l_op k
333
fccc6851 » MLstate
2011-06-21 Initial open-source release
334 let map_write_op
335 ~(transaction: 'transaction1 -> 'transaction2 Cps.t)
336 ~(revision: 'revision1 -> 'revision2 Cps.t)
337 (op: ('which, 'transaction1, 'revision1) generic_write_op)
338 : ('which, 'transaction2, 'revision2) generic_write_op Cps.t =
339 fun k -> match op with
340 | Set dialog ->
341 D.Dialog_aux.map_dialog ~query:(fun d k -> d |> k) ~response:transaction dialog
342 @> fun dialog -> Set dialog |> k
343 | Clear dialog ->
344 D.Dialog_aux.map_dialog ~query:(fun () k -> () |> k) ~response:transaction dialog
345 @> fun dialog -> Clear dialog |> k
346 | Link dialog ->
347 D.Dialog_aux.map_dialog ~query:(fun p k -> p |> k) ~response:transaction dialog
348 @> fun dialog -> Link dialog |> k
349 | Copy dialog ->
350 D.Dialog_aux.map_dialog
351 ~query:(fun (p, rev) k ->
352 match rev with
353 | None -> (p, None) |> k
354 | Some rev -> revision rev @> fun rev -> (p, Some rev) |> k)
355 ~response:transaction
356 dialog
357 @> fun dialog -> Copy dialog |> k
358
359 let map_write_list_op
360 ~(transaction: 'transaction1 -> 'transaction2 Cps.t)
361 ~(revision: 'revision1 -> 'revision2 Cps.t)
362 (l_op: ('which, 'transaction1, 'revision1) generic_write_op list)
363 : ('which, 'transaction2, 'revision2) generic_write_op list Cps.t =
364 fun k ->
365 let wr acc op k =
366 map_write_op ~transaction ~revision op @> fun op -> op::acc |> k
367 in
368 Cps.List.fold wr [] l_op k
369
0f295898 » Louis Gesbert
2011-07-01 [fix] badop: fixed closure comparison during command-line parsing
370 let rec options_conflict o1 o2 = match (o1,o2) with
371 | Options_Local l1, Options_Local l2 -> l1 = l2
372 | Options_Client (_,remote1,_), Options_Client (_,remote2,_) -> remote1 = remote2
373 | Options_Debug (_,o1), o2
374 | o1, Options_Debug (_,o2) -> options_conflict o1 o2
375 | Options_Dispatcher (_, ol), o
376 | o, Options_Dispatcher (_, ol) ->
377 List.exists (fun o1 -> options_conflict o1 o) ol
378 | _ -> false
379
fccc6851 » MLstate
2011-06-21 Initial open-source release
380 let path_to_string = Path.to_string
381 end
Something went wrong with that request. Please try again.