From 2ef62f3307e43bc7c12d02a4f1527e02d501f1cf Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Wed, 21 Sep 2022 12:01:51 +0200 Subject: [PATCH 1/3] Implement exists function --- src/git_kv.ml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/git_kv.ml b/src/git_kv.ml index e7c4cb9..e41abed 100644 --- a/src/git_kv.ml +++ b/src/git_kv.ml @@ -109,10 +109,18 @@ let of_octets ctx ~remote data = let edn, branch = split_url remote in { ctx ; edn ; branch ; store ; head = Some head } -let exists _t _key = - (* Search.find t.store t.head (`Path (Mirage_kv.Key.segments key)) >>= function *) - (* ([`Value | `Dictionary] option, error) result Lwt.t *) - assert false +let exists t key = + let open Lwt.Infix in + match t.head with + | None -> Lwt.return (Ok None) + | Some head -> + Search.mem t.store head (`Path (Mirage_kv.Key.segments key)) >>= function + | false -> Lwt.return (Ok None) + | true -> + Search.find t.store head (`Path (Mirage_kv.Key.segments key)) + >|= Option.get >>= Store.read_exn t.store >>= function + | Blob _ -> Lwt.return (Ok (Some `Value)) + | Tree _ | Commit _ | Tag _ -> Lwt.return (Ok (Some `Dictionary)) let get t key = let open Lwt.Infix in From ad09080ee61ad8de5fe9ca09da6764c10c2ba951 Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Wed, 21 Sep 2022 12:02:19 +0200 Subject: [PATCH 2/3] Remove an assert false and return an error instead of --- src/git_kv.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git_kv.ml b/src/git_kv.ml index e41abed..97375be 100644 --- a/src/git_kv.ml +++ b/src/git_kv.ml @@ -132,7 +132,7 @@ let get t key = | Some blob -> Store.read_exn t.store blob >|= function | Blob b -> Ok (Git.Blob.to_string b) - | _ -> assert false + | _ -> Error (`Value_expected key) let get_partial t key ~offset ~length = let open Lwt_result.Infix in From 503bf47ccb68ddd6dbcec6d73f1595e88bde105d Mon Sep 17 00:00:00 2001 From: Romain Calascibetta Date: Wed, 21 Sep 2022 12:02:34 +0200 Subject: [PATCH 3/3] Implement list function --- src/git_kv.ml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/git_kv.ml b/src/git_kv.ml index 97375be..8f4c7b5 100644 --- a/src/git_kv.ml +++ b/src/git_kv.ml @@ -143,9 +143,22 @@ let get_partial t key ~offset ~length = let l = min length (String.length data - offset) in String.sub data offset l -let list _t _key = - (* ((string * [`Value | `Dictionary]) list, error) result Lwt.t *) - assert false +let list t key = + let open Lwt.Infix in + match t.head with + | None -> Lwt.return (Error (`Not_found key)) + | Some head -> + Search.find t.store head (`Path (Mirage_kv.Key.segments key)) >>= function + | None -> Lwt.return (Error (`Not_found key)) + | Some tree -> + Store.read_exn t.store tree >>= function + | Tree t -> + Lwt_list.map_p (fun { Git.Tree.perm; name; _ } -> match perm with + | `Commit | `Dir -> Lwt.return (name, `Dictionary) + | `Everybody | `Exec | `Normal -> Lwt.return (name, `Value) + | `Link -> failwith "Unimplemented link follow") + (Store.Value.Tree.to_list t) >|= Result.ok + | _ -> Lwt.return (Error (`Dictionary_expected key)) let last_modified t key = let open Lwt.Infix in