diff options
| author | Estevan Castilho <tevo@riajuu.net> | 2026-05-09 01:12:57 +0000 |
|---|---|---|
| committer | Estevan Castilho <tevo@riajuu.net> | 2026-05-09 01:12:57 +0000 |
| commit | af1ef794f27600f2040237c2ae4aa5afe8a08b71 (patch) | |
| tree | c00907f7468876df41b1515636009f363b2d5d5f /lib/management/storage | |
| parent | 06faa066d11e2e76ffcf0207f34ea8c6e66a1a76 (diff) | |
| download | Sakura-physical-layer-lmdb.tar.gz Sakura-physical-layer-lmdb.zip | |
TEMPORARILY create an implicit transaction for operations if neededphysical-layer-lmdb
Diffstat (limited to '')
| -rw-r--r-- | lib/management/storage/lmdb.ml | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/lib/management/storage/lmdb.ml b/lib/management/storage/lmdb.ml index e1c4307..a01d5ef 100644 --- a/lib/management/storage/lmdb.ml +++ b/lib/management/storage/lmdb.ml @@ -73,23 +73,6 @@ module B : Physical.BACKEND with type error = string = struct let disconnect { env; _ } = mdb_env_close env - let get { dbi; _ } hash = - begin - match mdb_get' (current_tx ()) dbi (Bytes.of_string hash) with - | Ok x -> Ok (Some x) - | Error e when e = MDB_Errors.mdb_notfound -> Ok (None) - | Error e -> Error e - end - |> Result.map_error Int.to_string - - let put { dbi; _ } hash value = - mdb_put' (current_tx ()) dbi (Bytes.of_string hash) value Unsigned.UInt.zero - |> Result.map_error Int.to_string - - let exists conn hash = - get conn hash - |> Result.map Option.is_some - let lift_result r = BatEnum.fold (fun acc x -> @@ -104,6 +87,41 @@ module B : Physical.BACKEND with type error = string = struct | Ok (Error e) -> Error e | Ok (Ok x) -> Ok x + (* + * FIXME: this exists just to get things going right now, but I am + * now convinced that the proper thing to do would be to reify + * transactions as values at the interface level, and have every + * function that needs to read or write to a database take a + * transaction instead. + *) + let ensure_tx conn cont = + try cont (current_tx ()) + with Effect.Unhandled Current -> + with_transaction conn (fun () -> cont (current_tx ())) + |> Result.map Option.get + |> flatten_result + + let get ({ dbi; _ } as conn) hash = + ensure_tx conn + (fun tx -> + begin + match mdb_get' tx dbi (Bytes.of_string hash) with + | Ok x -> Ok (Some x) + | Error e when e = MDB_Errors.mdb_notfound -> Ok (None) + | Error e -> Error e + end + |> Result.map_error Int.to_string) + + let put ({ dbi; _ } as conn) hash value = + ensure_tx conn + (fun tx -> + mdb_put' tx dbi (Bytes.of_string hash) value Unsigned.UInt.zero + |> Result.map_error Int.to_string) + + let exists conn hash = + get conn hash + |> Result.map Option.is_some + let get_many conn hashes = BatEnum.map (get conn) hashes |> lift_result let put_many conn values = |
