Skip to content

Commit

Permalink
irmin: add a Repo.close operation to the high-level API
Browse files Browse the repository at this point in the history
  • Loading branch information
samoht committed Aug 30, 2019
1 parent 3a807b6 commit 3d47cde
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/irmin-chunk/irmin_chunk.ml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ struct
CA.v config >|= fun db ->
{ chunking; db; chunk_size; max_children; max_data }

let close _ = Lwt.return_unit

let batch t f = CA.batch t.db (fun db -> f { t with db })

let find_leaves t key =
Expand Down
2 changes: 2 additions & 0 deletions src/irmin-fs/irmin_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ struct
let path = get_path config in
IO.mkdir path >|= fun () -> { path }

let close _ = Lwt.return_unit

let cast t = (t :> [ `Read | `Write ] t)

let batch t f = f (cast t)
Expand Down
2 changes: 2 additions & 0 deletions src/irmin-git/irmin_git.ml
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,8 @@ struct
G.v ?dotgit ?compression:level ?buffers root >>= function
| Error e -> Fmt.kstrf Lwt.fail_with "%a" G.pp_error e
| Ok g -> R.v ~head ~bare g >|= fun b -> { g; b; config = conf }

let close _ = Lwt.return_unit
end
end

Expand Down
2 changes: 2 additions & 0 deletions src/irmin-http/irmin_http.ml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ module Client (Client : HTTP_CLIENT) (S : Irmin.S) = struct
Branch.v ?ctx uri >|= fun branch ->
let commit = (node, commit) in
{ contents; node; commit; branch; config }

let close _ = Lwt.return_unit
end
end

Expand Down
4 changes: 4 additions & 0 deletions src/irmin-mem/irmin_mem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module Read_only (K : Irmin.Type.S) (V : Irmin.Type.S) = struct

let v _config = Lwt.return map

let close t =
t.t <- KMap.empty;
Lwt.return_unit

let cast t = (t :> [ `Read | `Write ] t)

let batch t f = f (cast t)
Expand Down
4 changes: 3 additions & 1 deletion src/irmin-pack/irmin_pack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ struct
Commit.CA.v ~fresh ~readonly ~lru_size ~index root >>= fun commit ->
Branch.v ~fresh ~readonly root >|= fun branch ->
{ contents; node; commit; branch; config; index }

let close _ = Lwt.return_unit
end
end

Expand Down Expand Up @@ -431,7 +433,7 @@ struct
let _, capability = X.Repo.commit_t t in
X.Commit.CA.integrity_check ~offset ~length k capability;
count_increment commits
| _ -> invalid_arg "unknown content type" )
| _ -> invalid_arg "unknown content type")
t.index;
pr_stats ()

Expand Down
55 changes: 55 additions & 0 deletions src/irmin/irmin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,54 @@ module Path = struct
module type S = S.PATH
end

exception Closed

module Check_closed (CA : S.CONTENT_ADDRESSABLE_STORE_MAKER) :
S.CONTENT_ADDRESSABLE_STORE_MAKER =
functor
(K : S.HASH)
(V : Type.S)
->
struct
module S = CA (K) (V)

type 'a t = { closed : bool ref; t : 'a S.t }

type key = S.key

type value = S.value

let check_closed t = if !(t.closed) then raise Closed

let mem t k =
check_closed t;
S.mem t.t k

let find t k =
check_closed t;
S.find t.t k

let add t v =
check_closed t;
S.add t.t v

let unsafe_add t k v =
check_closed t;
S.unsafe_add t.t k v

let batch t f =
check_closed t;
S.batch t.t (fun w -> f { t = w; closed = t.closed })

let v conf = S.v conf >|= fun t -> { closed = ref false; t }

let close t =
if !(t.closed) then Lwt.return_unit
else (
t.closed := true;
S.close t.t )
end

module Make_ext
(CA : S.CONTENT_ADDRESSABLE_STORE_MAKER)
(AW : S.ATOMIC_WRITE_STORE_MAKER)
Expand All @@ -68,6 +116,8 @@ module Make_ext
and type step = P.step)
(CT : S.COMMIT with type hash = H.t) =
struct
module CA = Check_closed (CA)

module X = struct
module Hash = H

Expand Down Expand Up @@ -144,6 +194,11 @@ struct
let commits = (nodes, commits) in
Branch.v config >|= fun branch ->
{ contents; nodes; commits; branch; config }

let close t =
Contents.CA.close t.contents >>= fun () ->
Node.CA.close (snd t.nodes) >>= fun () ->
Commit.CA.close (snd t.commits)
end
end

Expand Down
14 changes: 14 additions & 0 deletions src/irmin/irmin.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,8 @@ module Private : sig

val v : config -> t Lwt.t

val close : t -> unit Lwt.t

val contents_t : t -> [ `Read ] Contents.t

val node_t : t -> [ `Read ] Node.t
Expand Down Expand Up @@ -2206,6 +2208,9 @@ module type S = sig
(** [v config] connects to a repository in a backend-specific
manner. *)

val close : t -> unit Lwt.t
(** [close t] frees up all resources associated with [t]. *)

val heads : t -> commit list Lwt.t
(** [heads] is {!Head.list}. *)

Expand Down Expand Up @@ -3601,6 +3606,9 @@ module type APPEND_ONLY_STORE_MAKER = functor (K : Type.S) (V : Type.S) -> sig
val v : config -> [ `Read ] t Lwt.t
(** [v config] is a function returning fresh store handles, with the
configuration [config], which is provided by the backend. *)

val close : 'a t -> unit Lwt.t
(** [close t] frees up all the resources associated to [t]. *)
end

(** [CONTENT_ADDRESSABLE_STOREMAKER] is the signature exposed by
Expand All @@ -3619,6 +3627,9 @@ module type CONTENT_ADDRESSABLE_STORE_MAKER = functor
val v : config -> [ `Read ] t Lwt.t
(** [v config] is a function returning fresh store handles, with the
configuration [config], which is provided by the backend. *)

val close : 'a t -> unit Lwt.t
(** [close t] frees up all the resources associated to [t]. *)
end

module Content_addressable
Expand All @@ -3638,6 +3649,9 @@ module Content_addressable
val v : config -> [ `Read ] t Lwt.t
(** [v config] is a function returning fresh store handles, with the
configuration [config], which is provided by the backend. *)

val close : 'a t -> unit Lwt.t
(** [close t] frees up all the resources associated to [t]. *)
end

(** [ATOMIC_WRITE_STORE_MAKER] is the signature exposed by atomic-write
Expand Down
8 changes: 8 additions & 0 deletions src/irmin/s.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ module type CONTENT_ADDRESSABLE_STORE_MAKER = functor
val batch : [ `Read ] t -> ([ `Read | `Write ] t -> 'a Lwt.t) -> 'a Lwt.t

val v : Conf.t -> [ `Read ] t Lwt.t

val close : 'a t -> unit Lwt.t
end

module type APPEND_ONLY_STORE = sig
Expand All @@ -121,6 +123,8 @@ module type APPEND_ONLY_STORE_MAKER = functor (K : Type.S) (V : Type.S) -> sig
val batch : [ `Read ] t -> ([ `Read | `Write ] t -> 'a Lwt.t) -> 'a Lwt.t

val v : Conf.t -> [ `Read ] t Lwt.t

val close : 'a t -> unit Lwt.t
end

module type METADATA = sig
Expand Down Expand Up @@ -463,6 +467,8 @@ module type PRIVATE = sig

val v : Conf.t -> t Lwt.t

val close : t -> unit Lwt.t

val contents_t : t -> [ `Read ] Contents.t

val node_t : t -> [ `Read ] Node.t
Expand Down Expand Up @@ -647,6 +653,8 @@ module type STORE = sig

val v : config -> t Lwt.t

val close : t -> unit Lwt.t

val heads : t -> commit list Lwt.t

val branches : t -> branch list Lwt.t
Expand Down
2 changes: 2 additions & 0 deletions src/irmin/store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ module Make (P : S.PRIVATE) = struct

let v = P.Repo.v

let close = P.Repo.close

let graph_t t = P.Repo.node_t t

let history_t t = P.Repo.commit_t t
Expand Down
2 changes: 2 additions & 0 deletions src/irmin/store.mli
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ module Content_addressable
val batch : [ `Read ] t -> ([ `Read | `Write ] t -> 'a Lwt.t) -> 'a Lwt.t

val v : Conf.t -> [ `Read ] t Lwt.t

val close : 'a t -> unit Lwt.t
end

0 comments on commit 3d47cde

Please sign in to comment.