Skip to content

Commit

Permalink
irmin-mirage-git: clean up write error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
metanivek committed Jun 9, 2023
1 parent 61e2872 commit 8b0e4fd
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions src/irmin-mirage/git/irmin_mirage_git.ml
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,6 @@ module KV_RW (G : Irmin_git.G) (C : Mirage_clock.PCLOCK) = struct

type write_error = [ RO.error | Mirage_kv.write_error | RO.Sync.push_error ]

let write_error = function
| Ok _ -> Ok ()
| Error e -> Error (e :> write_error)

let pp_write_error ppf = function
| #RO.error as e -> RO.pp_error ppf e
| #RO.Sync.push_error as e -> RO.Sync.pp_push_error ppf e
Expand All @@ -285,23 +281,22 @@ module KV_RW (G : Irmin_git.G) (C : Mirage_clock.PCLOCK) = struct
let info t op = Info.f ~author:(t.author ()) "%s" (t.msg op)
let path = RO.path

let ( >?= ) r f =
r >>= function
| Error e -> Lwt.return_error (e :> write_error)
| Ok r -> f r

let set t k v =
let info = info t (`Set k) in
match t.store with
| Store s -> (
S.set ~info s.t (path k) v >>= function
| Ok _ -> RO.Sync.push s.t t.remote >|= write_error
| Error e -> Lwt.return (Error (e :> write_error)))
| Store s ->
S.set ~info s.t (path k) v >?= fun () ->
RO.Sync.push s.t t.remote >?= fun _ -> Lwt.return_ok ()
| Batch b ->
S.Tree.add b.tree (path k) v >|= fun tree ->
b.tree <- tree;
Ok ()

let ( >?= ) r f =
r >>= function
| Error e -> Lwt.return_error (e :> write_error)
| Ok r -> f r

let set_partial t k ~offset v =
let off = Optint.Int63.to_int offset in
if off < 0 then Lwt.return_ok ()
Expand All @@ -317,10 +312,9 @@ module KV_RW (G : Irmin_git.G) (C : Mirage_clock.PCLOCK) = struct
let remove t k =
let info = info t (`Remove k) in
match t.store with
| Store s -> (
S.remove ~info s.t (path k) >>= function
| Ok _ -> RO.Sync.push s.t t.remote >|= write_error
| Error e -> Lwt.return (Error (e :> write_error)))
| Store s ->
S.remove ~info s.t (path k) >?= fun () ->
RO.Sync.push s.t t.remote >?= fun _ -> Lwt.return_ok ()
| Batch b ->
S.Tree.remove b.tree (path k) >|= fun tree ->
b.tree <- tree;
Expand Down

0 comments on commit 8b0e4fd

Please sign in to comment.