diff options
| author | Marcos Magueta <maguetamarcos@gmail.com> | 2026-06-23 23:53:26 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-06-23 23:53:26 -0300 |
| commit | c284fcd570342c31bc21169960579a20a763e32e (patch) | |
| tree | ce7485055116d794c55090ad0978e0b3f5ff4c2a /lib/management/physical.ml | |
| parent | b81586ee4a0543a5f9866586c78dccdd6328934a (diff) | |
| download | Sakura-c284fcd570342c31bc21169960579a20a763e32e.tar.gz Sakura-c284fcd570342c31bc21169960579a20a763e32e.zip | |
Format
Diffstat (limited to 'lib/management/physical.ml')
| -rw-r--r-- | lib/management/physical.ml | 97 |
1 files changed, 41 insertions, 56 deletions
diff --git a/lib/management/physical.ml b/lib/management/physical.ml index 7c6f6b2..b896330 100644 --- a/lib/management/physical.ml +++ b/lib/management/physical.ml @@ -13,37 +13,35 @@ module type BACKEND = sig type connection - val connect : configuration -> (connection, Condition.t) result (** Connect to the storage backend *) + val connect : configuration -> (connection, Condition.t) result - val disconnect : connection -> unit (** Disconnect from the backend *) + val disconnect : connection -> unit - val get : connection -> Conventions.Hash.t -> (bytes option, Condition.t) result (** Get a value by its hash. Returns None if not found. *) + val get : connection -> Conventions.Hash.t -> (bytes option, Condition.t) result - val put : connection -> Conventions.Hash.t -> bytes -> (unit, Condition.t) result (** Store a value at its hash *) + val put : connection -> Conventions.Hash.t -> bytes -> (unit, Condition.t) result - val exists : connection -> Conventions.Hash.t -> (bool, Condition.t) result (** Check if a hash exists *) + val exists : connection -> Conventions.Hash.t -> (bool, Condition.t) result - val begin_transaction : connection -> (unit, Condition.t) result (** Begin a transaction *) + val begin_transaction : connection -> (unit, Condition.t) result - val commit : connection -> (unit, Condition.t) result (** Commit a transaction *) + val commit : connection -> (unit, Condition.t) result - val rollback : connection -> (unit, Condition.t) result (** Rollback a transaction *) + val rollback : connection -> (unit, Condition.t) result - val get_many : - connection -> Conventions.Hash.t list -> (bytes option list, Condition.t) result (** Batch get multiple values *) + val get_many : connection -> Conventions.Hash.t list -> (bytes option list, Condition.t) result - val put_many : - connection -> (Conventions.Hash.t * bytes) list -> (unit, Condition.t) result (** Batch put multiple values *) + val put_many : connection -> (Conventions.Hash.t * bytes) list -> (unit, Condition.t) result end (** Storage operations built on top of a backend *) @@ -52,38 +50,34 @@ module type S = sig type t - val create : configuration -> (t, Condition.t) result (** Create a storage instance from a backend connection *) + val create : configuration -> (t, Condition.t) result - val close : t -> unit (** Close the storage *) + val close : t -> unit - val store_attribute : - t -> Conventions.AbstractValue.t -> (Conventions.Hash.t, Condition.t) result (** Store an attribute value, returns its hash *) + val store_attribute : t -> Conventions.AbstractValue.t -> (Conventions.Hash.t, Condition.t) result - val load_attribute : - t -> - Conventions.Hash.t -> - (Conventions.AbstractValue.t option, Condition.t) result (** Load an attribute value by hash *) + val load_attribute : + t -> Conventions.Hash.t -> (Conventions.AbstractValue.t option, Condition.t) result - val store_raw : t -> Conventions.Hash.t -> bytes -> (unit, Condition.t) result (** Store raw bytes at a hash *) + val store_raw : t -> Conventions.Hash.t -> bytes -> (unit, Condition.t) result - val load_raw : t -> Conventions.Hash.t -> (bytes option, Condition.t) result (** Load raw bytes by hash *) + val load_raw : t -> Conventions.Hash.t -> (bytes option, Condition.t) result - val exists : t -> Conventions.Hash.t -> (bool, Condition.t) result (** Check if a hash exists *) + val exists : t -> Conventions.Hash.t -> (bool, Condition.t) result - val with_transaction : t -> (unit -> ('a, Condition.t) result) -> ('a, Condition.t) result (** Execute a function within a transaction *) + val with_transaction : t -> (unit -> ('a, Condition.t) result) -> ('a, Condition.t) result end (** Functor to create storage operations from a backend *) -module Make (B : BACKEND) : - S with type configuration = B.configuration = struct +module Make (B : BACKEND) : S with type configuration = B.configuration = struct type configuration = B.configuration type t = B.connection @@ -110,30 +104,31 @@ module Make (B : BACKEND) : match B.begin_transaction conn with | Error e -> Error e | Ok () -> ( - match f () with - | Ok result -> ( - match B.commit conn with Ok () -> Ok result | Error e -> Error e) - | Error e -> - let _ = B.rollback conn in - Error e) + match f () with + | Ok result -> ( match B.commit conn with Ok () -> Ok result | Error e -> Error e ) + | Error e -> + let _ = B.rollback conn in + Error e ) end (** In-memory backend for testing and development *) -module MemoryBackend : - BACKEND with type configuration = unit = struct +module MemoryBackend : BACKEND with type configuration = unit = struct type configuration = unit - type connection = { - data : (string, bytes) Hashtbl.t; - mutable in_transaction : bool; - mutable transaction_buffer : (string * bytes) list; - } + type connection = + { data: (string, bytes) Hashtbl.t; + mutable in_transaction: bool; + mutable transaction_buffer: (string * bytes) list } module Error = struct open Condition + (* TODO: more structure *) let parse_error msg = condition "parse-error" msg empty - let already_in_transaction = condition "already-in-transaction" "Already inside a transaction" empty + + let already_in_transaction = + condition "already-in-transaction" "Already inside a transaction" empty + let not_in_transaction = condition "not-in-transaction" "Not inside a transaction" empty end @@ -144,16 +139,9 @@ module MemoryBackend : Error (Error.parse_error (Printf.sprintf "memory backend takes no configuration, got: %s" - (Sexplib.Sexp.to_string sexp))) - - let connect () = - Ok - { - data = Hashtbl.create 1024; - in_transaction = false; - transaction_buffer = []; - } + (Sexplib.Sexp.to_string sexp) ) ) + let connect () = Ok {data= Hashtbl.create 1024; in_transaction= false; transaction_buffer= []} let disconnect _ = () let get conn hash = Ok (Hashtbl.find_opt conn.data hash) @@ -180,9 +168,7 @@ module MemoryBackend : let commit conn = if not conn.in_transaction then Error Error.not_in_transaction else begin - List.iter - (fun (hash, value) -> Hashtbl.replace conn.data hash value) - conn.transaction_buffer; + List.iter (fun (hash, value) -> Hashtbl.replace conn.data hash value) conn.transaction_buffer; conn.in_transaction <- false; conn.transaction_buffer <- []; Ok () @@ -196,18 +182,17 @@ module MemoryBackend : Ok () end - let get_many conn hashes = - Ok (List.map (fun h -> Hashtbl.find_opt conn.data h) hashes) + let get_many conn hashes = Ok (List.map (fun h -> Hashtbl.find_opt conn.data h) hashes) let put_many conn pairs = List.iter (fun (hash, value) -> if conn.in_transaction then conn.transaction_buffer <- (hash, value) :: conn.transaction_buffer - else Hashtbl.replace conn.data hash value) + else Hashtbl.replace conn.data hash value ) pairs; Ok () end -module Memory = Make (MemoryBackend) (** Default in-memory storage for development *) +module Memory = Make (MemoryBackend) |
