aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/kernel/BranchManager.ml29
-rw-r--r--lib/kernel/BranchManager.mli11
2 files changed, 40 insertions, 0 deletions
diff --git a/lib/kernel/BranchManager.ml b/lib/kernel/BranchManager.ml
new file mode 100644
index 0000000..6884219
--- /dev/null
+++ b/lib/kernel/BranchManager.ml
@@ -0,0 +1,29 @@
+module BranchValue = Merkle.StringKey (* FIXME *)
+
+type branch = string
+
+module Make (S : Abstract.Storage.STORAGE) = struct
+ module I = Merkle.Interface (S) (Merkle.StringKey) (BranchValue)
+ module SI = Storage.Make (S)
+
+ type manager = { storage: S.connection; head: I.node Atomic.t }
+
+ let initialize storage addr =
+ let open Utilities.Result in
+ SI.with_transaction storage (fun tx ->
+ let* head = I.find tx addr in
+ Ok { storage; head = Option.value head ~default:I.empty |> Atomic.make })
+
+ let fetch { storage; head } branch_name =
+ SI.with_transaction storage (fun tx ->
+ let head = Atomic.get head in
+ I.lookup tx branch_name head)
+
+ let update ({ storage; head } as manager) branch_name f =
+ let open Utilities.Result in
+ SI.with_transaction storage (fun tx ->
+ let* new_head = Utilities.Atomic.mswap head (fun head ->
+ let* branch = I.lookup tx branch_name head in
+ f branch) in
+ Ok { manager with head = new_head })
+end
diff --git a/lib/kernel/BranchManager.mli b/lib/kernel/BranchManager.mli
new file mode 100644
index 0000000..1b572af
--- /dev/null
+++ b/lib/kernel/BranchManager.mli
@@ -0,0 +1,11 @@
+type branch = string (* FIXME *)
+
+module Make (S : Abstract.Storage.STORAGE) : sig
+ type manager
+
+ val initialize : S.connection -> Concepts.Hash.hash -> (manager, Concepts.Condition.condition) result
+ val fetch : manager -> string -> (Concepts.Branch.t option, Concepts.Condition.condition) result
+ val update : manager -> string -> (branch -> branch) -> (manager, Concepts.Condition.condition) result
+
+ (* TODO: fork, join *)
+end