From eab5511ab7dc65786f65881a396e454bb70bdc9a Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Mon, 28 Oct 2024 21:56:53 +0100 Subject: [PATCH] only use a single commit in rename --- src/git_kv.ml | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/git_kv.ml b/src/git_kv.ml index 3e72f27..b7b8fd5 100644 --- a/src/git_kv.ml +++ b/src/git_kv.ml @@ -648,13 +648,6 @@ module Make (Pclock : Mirage_clock.PCLOCK) = struct let open Lwt.Infix in remove ?and_commit:t.committed t key >|= Result.map_error to_write_error - let rename t ~source ~dest = - (* TODO(dinosaure): optimize it! It was done on the naive way. *) - let open Lwt_result.Infix in - get t source >>= fun contents -> - remove t source >>= fun () -> - set t dest contents - let allocate t key ?last_modified:_ size = let open Lwt.Infix in exists t key >>= function @@ -714,4 +707,18 @@ module Make (Pclock : Mirage_clock.PCLOCK) = struct Lwt.wakeup_later wk () ; t.committed <- None ; Lwt.return res + + let rename t ~source ~dest = + let open Lwt_result.Infix in + let op t = + get t source >>= fun contents -> + remove t source >>= fun () -> + set t dest contents + in + match t.committed with + | Some _ -> op t + | None -> + change_and_push t op >>! function + | Ok a -> Lwt.return a + | Error _ as e -> Lwt.return e end