diff options
| author | Estevan Castilho <tevo@riajuu.net> | 2026-08-20 00:00:36 +0000 |
|---|---|---|
| committer | Estevan Castilho <tevo@riajuu.net> | 2026-08-23 03:06:57 +0000 |
| commit | 465f3378daea5a6f0d44f3729c3551a9cda78ed9 (patch) | |
| tree | 0dd0e2a9bed53ef8132d8cfef41502a54cc5b97c /lib/kernel/BranchManager.ml | |
| parent | 5a4f7d5ed6e5d6b2711db316e8de06cea78b54ec (diff) | |
| download | RNT-465f3378daea5a6f0d44f3729c3551a9cda78ed9.tar.gz RNT-465f3378daea5a6f0d44f3729c3551a9cda78ed9.zip | |
BranchManager sketch
Diffstat (limited to '')
| -rw-r--r-- | lib/kernel/BranchManager.ml | 46 | ||||
| -rw-r--r-- | lib/kernel/BranchManager.mli | 4 |
2 files changed, 37 insertions, 13 deletions
diff --git a/lib/kernel/BranchManager.ml b/lib/kernel/BranchManager.ml index 6884219..71a25b3 100644 --- a/lib/kernel/BranchManager.ml +++ b/lib/kernel/BranchManager.ml @@ -2,28 +2,52 @@ module BranchValue = Merkle.StringKey (* FIXME *) type branch = string +module Error = struct + open Concepts.Condition + + let invalid_root head = condition "invalid-root" "The root hash for the database state is missing from the backend. Either your storage is corrupted, or this is a bug in RNT!" + ("hash" |=| (Concepts.Value.String (Concepts.Hash.to_hum_string head))) +end + 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 } + type manager = { storage: S.connection; + label : string; + head: I.node Atomic.t } - let initialize storage addr = + let root_for tx label = + let open Utilities.Result in + let* label = S.get tx (S.Label label) in + let label = Option.map Concepts.Hash.hash_of_blob label in + Ok label + + let persist_root tx label new_branch = failwith "TODO" + + let initialize storage label = 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* addr = root_for tx label in + match addr with + | None -> failwith "TODO" + | Some addr -> + let* head = I.find tx addr in + let* head = Option.to_result ~none:(Error.invalid_root addr) head in + Ok { storage; label; head = Atomic.make head }) - let fetch { storage; head } branch_name = + 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 update ({ storage; head; label } 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 }) + let* _ = SI.with_transaction storage (fun tx -> + Utilities.Atomic.mswap head (fun head -> + let* branch = I.lookup tx branch_name head in + let new_branch = f branch in + let* () = persist_root tx label new_branch in + new_branch)) in + Ok () end diff --git a/lib/kernel/BranchManager.mli b/lib/kernel/BranchManager.mli index 1b572af..063f8a7 100644 --- a/lib/kernel/BranchManager.mli +++ b/lib/kernel/BranchManager.mli @@ -3,8 +3,8 @@ 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 initialize : S.connection -> string -> (manager, Concepts.Condition.condition) result + val fetch : manager -> string -> (branch option, Concepts.Condition.condition) result val update : manager -> string -> (branch -> branch) -> (manager, Concepts.Condition.condition) result (* TODO: fork, join *) |
