Persistent store with Git semantics: lazy reads, delayed writes, content-addressing
at inline-small-objects 84 lines 2.4 kB view raw
1(** Persistent pointers to OCaml values. 2 3 Links delimit serialization boundaries in data structures. Insert [v] calls 4 to control persistence granularity - only new links are written. 5 6 {[ 7 type tree = node t 8 and node = Empty | Node of { l : tree; x : int; r : tree } 9 10 let rec add s x t = 11 match get t with 12 | Empty -> v s (Node { l = v s Empty; x; r = v s Empty }) 13 | Node n -> 14 if x = n.x then t 15 else if x < n.x then v s (Node { n with l = add s x n.l }) 16 else v s (Node { n with r = add s x n.r }) 17 ]} 18 19 Properties: [get (v s x) = x] and [equal (v s (get l)) l]. *) 20 21(** {1:types Types} *) 22 23type 'a t 24(** The type for links to ['a] values. Links embed their content store. *) 25 26type 'a store 27(** The type for stores with root of type ['a]. *) 28 29type address 30(** The type for content addresses. Opaque. *) 31 32(** {1:links Links} *) 33 34val v : _ store -> 'a -> 'a t 35(** [v s x] is a link to [x], using content store from [s]. *) 36 37val of_address : _ store -> address -> 'a t 38(** [of_address s addr] is a link that lazily loads from [addr]. *) 39 40val get : 'a t -> 'a 41(** [get l] is the value linked by [l]. Fetches if needed. *) 42 43val address : 'a t -> address 44(** [address l] is the content address of [l]. Writes if needed. *) 45 46val equal : 'a t -> 'a t -> bool 47(** [equal l0 l1] is [true] iff [l0] and [l1] have the same address. *) 48 49val is_val : 'a t -> bool 50(** [is_val l] is [true] if the value is in memory (like {!Lazy.is_val}). *) 51 52val pp : Format.formatter -> 'a t -> unit 53(** [pp] formats the link's address (or ["<mem>"] if not yet stored). *) 54 55(** {1:stores Stores} *) 56 57val read : 'a store -> 'a 58(** [read s] is the current root of [s]. Raises if no root set. *) 59 60val write : 'a store -> 'a -> unit 61(** [write s x] sets the root of [s] to [x]. *) 62 63val is_open : _ store -> bool 64(** [is_open s] is [true] if [s] is open. *) 65 66val close : _ store -> unit 67(** [close s] closes [s]. Further operations raise. *) 68 69(** {2 Store creation} *) 70 71module Make (_ : Codec.S) : sig 72 val v : unit -> _ store 73 (** [v ()] is a new in-memory store. *) 74end 75 76module Git : sig 77 val v : unit -> _ store 78 (** [v ()] is a new in-memory Git-compatible store (SHA-1). *) 79end 80 81module Mst : sig 82 val v : unit -> _ store 83 (** [v ()] is a new in-memory MST store (SHA-256, ATProto). *) 84end