Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code around and renamde digest -> hash #720

Merged
merged 1 commit into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/irmin-chunk/irmin_chunk.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ struct
| `Max -> { Chunk.v; len = t.chunk_size }
| `Best_fit -> { Chunk.v; len = Chunk.size_of_v v }

let data t v =
let v = Chunk.Data v in
let data t s =
let v = Chunk.Data s in
match t.chunking with
| `Max -> { Chunk.v; len = t.chunk_size }
| `Best_fit -> { Chunk.v; len = Chunk.size_of_v v }
Expand Down Expand Up @@ -198,15 +198,15 @@ struct
let chunk_size = C.get config Conf.chunk_size in
let max_data = chunk_size - Chunk.size_of_data_header in
let max_children =
(chunk_size - Chunk.size_of_index_header) / K.digest_size
(chunk_size - Chunk.size_of_index_header) / K.hash_size
in
let chunking = C.get config Conf.chunking in
( if max_children <= 1 then
let min = Chunk.size_of_index_header + (K.digest_size * 2) in
let min = Chunk.size_of_index_header + (K.hash_size * 2) in
err_too_small ~min chunk_size );
Log.debug (fun l ->
l "config: chunk-size=%d digest-size=%d max-data=%d max-children=%d"
chunk_size K.digest_size max_data max_children );
chunk_size K.hash_size max_data max_children );
CA.v config >|= fun db ->
{ chunking; db; chunk_size; max_children; max_data }

Expand All @@ -218,7 +218,7 @@ struct
| Some x -> Tree.find_leaves t x >|= fun v -> Some v

let check_hash k v =
let k' = K.digest v in
let k' = K.hash v in
if Irmin.Type.equal K.t k k' then Lwt.return ()
else
Fmt.kstrf Lwt.fail_invalid_arg "corrupted value: got %a, expecting %a"
Expand All @@ -242,7 +242,7 @@ struct

let add t v =
let buf = Irmin.Type.to_bin_string V.t v in
let key = K.digest buf in
let key = K.hash buf in
let len = String.length buf in
if len <= t.max_data then (
AO.add t.db key (data t buf) >|= fun () ->
Expand Down
2 changes: 1 addition & 1 deletion src/irmin-http/irmin_http.ml
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ module Client (Client : HTTP_CLIENT) (S : Irmin.S) = struct
module Key = struct
include S.Hash

let digest v = digest (Irmin.Type.pre_digest Val.t v)
let hash v = hash (Irmin.Type.pre_hash Val.t v)
end

include AO (Client) (S.Hash) (Val)
Expand Down
8 changes: 4 additions & 4 deletions src/irmin-test/store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ module Make (S : S) = struct
(* Test caching (makesure that no tree is lying in scope) *)
( S.Tree.Cache.trim ();
Gc.full_major ();
let v0 = S.Tree.shallow repo (P.Contents.Key.digest "foo") in
let v0 = S.Tree.shallow repo (P.Contents.Key.hash "foo") in
check_cache "empty" 0 0;
let foo = "foo-x" in
S.Tree.add v0 [ "foo" ] foo >>= fun v0 ->
Expand All @@ -1165,7 +1165,7 @@ module Make (S : S) = struct
()
in
S.Tree.Cache.trim ();
let v0 = S.Tree.shallow repo (P.Contents.Key.digest "bar") in
let v0 = S.Tree.shallow repo (P.Contents.Key.hash "bar") in
let xxx = "xxx" in
let yyy = "yyy" in
let zzz = "zzz" in
Expand Down Expand Up @@ -1751,8 +1751,8 @@ module Make (S : S) = struct

let test_shallow_objects x () =
let test repo =
let foo_k = S.Private.Contents.Key.digest "foo" in
let bar_k = S.Private.Contents.Key.digest "bar" in
let foo_k = S.Private.Contents.Key.hash "foo" in
let bar_k = S.Private.Contents.Key.hash "bar" in
let tree_1 = S.Tree.shallow repo foo_k in
let tree_2 = S.Tree.shallow repo bar_k in
let node_3 =
Expand Down
5 changes: 3 additions & 2 deletions src/irmin/commit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ struct

let merge t ~info = Merge.(option (v S.Key.t (merge_commit info t)))

module Key = Hash.With_digest (S.Key) (S.Val)
module Key = Hash.With_hash (S.Key) (S.Val)
module Val = S.Val
end

Expand Down Expand Up @@ -188,7 +188,7 @@ module History (S : S.COMMIT_STORE) = struct

let compare = Type.compare S.Key.t

let hash = S.Key.hash
let hash = S.Key.short_hash

let equal = Type.equal S.Key.t
end
Expand Down Expand Up @@ -511,6 +511,7 @@ module V1 (C : S.COMMIT) = struct
type t = { parents : hash list; c : C.t }

let import c = { c; parents = C.parents c }

let export t = t.c

let node t = C.node t.c
Expand Down
2 changes: 1 addition & 1 deletion src/irmin/contents.ml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ module Store (S : sig
module Val : S.CONTENTS with type t = value
end) =
struct
module Key = Hash.With_digest (S.Key) (S.Val)
module Key = Hash.With_hash (S.Key) (S.Val)
module Val = S.Val

type 'a t = 'a S.t
Expand Down
16 changes: 8 additions & 8 deletions src/irmin/hash.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ module Make (H : Digestif.S) = struct

external get_64 : string -> int -> int64 = "%caml_string_get64u"

let hash c = Int64.to_int (get_64 (H.to_raw_string c) 0)
let short_hash c = Int64.to_int (get_64 (H.to_raw_string c) 0)

let digest_size = H.digest_size
let hash_size = H.digest_size

let digest x = H.digest_string x
let hash x = H.digest_string x

let of_hex s =
match H.consistent_of_hex s with
Expand All @@ -34,7 +34,7 @@ module Make (H : Digestif.S) = struct

let t =
Type.map ~cli:(pp_hex, of_hex)
Type.(string_of (`Fixed digest_size))
Type.(string_of (`Fixed hash_size))
H.of_raw_string H.to_raw_string
end

Expand All @@ -47,20 +47,20 @@ module SHA512 = Make (Digestif.SHA512)
module BLAKE2B = Make (Digestif.BLAKE2B)
module BLAKE2S = Make (Digestif.BLAKE2S)

module With_digest (K : S.HASH) (V : Type.S) = struct
module With_hash (K : S.HASH) (V : Type.S) = struct
include K

let digest v = K.digest (Type.pre_digest V.t v)
let hash v = K.hash (Type.pre_hash V.t v)
end

module V1 (K : S.HASH) : S.HASH with type t = K.t = struct
type t = K.t

let hash = K.hash

let digest = K.digest
let short_hash = K.short_hash

let digest_size = K.digest_size
let hash_size = K.hash_size

let h = Type.string_of `Int64

Expand Down
4 changes: 2 additions & 2 deletions src/irmin/hash.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ module BLAKE2B : S.HASH

module BLAKE2S : S.HASH

module With_digest (K : S.HASH) (V : Type.S) : sig
module With_hash (K : S.HASH) (V : Type.S) : sig
include S.HASH with type t = K.t

val digest : V.t -> t
val hash : V.t -> t
end

module V1 (H : S.HASH) : S.HASH with type t = H.t
59 changes: 8 additions & 51 deletions src/irmin/irmin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
open Lwt.Infix
module Type = Type
module Diff = Diff
module Content_addressable = Store.Content_addressable

module Contents = struct
include Contents
Expand Down Expand Up @@ -51,54 +52,6 @@ module Path = struct
module type S = S.PATH
end

module type APPEND_ONLY_STORE = sig
type 'a t

type key

type value

val mem : [> `Read ] t -> key -> bool Lwt.t

val find : [> `Read ] t -> key -> value option Lwt.t

val add : [> `Write ] t -> key -> value -> unit Lwt.t
end

module type APPEND_ONLY_STORE_MAKER = functor (K : Type.S) (V : Type.S) -> sig
include APPEND_ONLY_STORE with type key = K.t and type value = V.t

val batch : [ `Read ] t -> ([ `Read | `Write ] t -> 'a Lwt.t) -> 'a Lwt.t

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

module Content_addressable
(AO : APPEND_ONLY_STORE_MAKER)
(K : S.HASH)
(V : Type.S) =
struct
include AO (K) (V)

let pp_key = Type.pp K.t

let digest v = K.digest (Type.pre_digest V.t v)

let find t k =
find t k >>= function
| None -> Lwt.return None
| Some v as r ->
let k' = digest v in
if Type.equal K.t k k' then Lwt.return r
else
Fmt.kstrf Lwt.fail_invalid_arg
"corrupted value: got %a, expecting %a" pp_key k' pp_key k

let add t v =
let k = digest v in
add t k v >|= fun () -> k
end

module Make_ext
(CA : S.CONTENT_ADDRESSABLE_STORE_MAKER)
(AW : S.ATOMIC_WRITE_STORE_MAKER)
Expand All @@ -118,8 +71,8 @@ struct

module Contents = struct
module CA = struct
module Val = C
module Key = Hash
module Val = C
include CA (Key) (Val)
end

Expand All @@ -128,8 +81,8 @@ struct

module Node = struct
module CA = struct
module Val = N
module Key = Hash
module Val = N
include CA (Key) (Val)
end

Expand All @@ -138,8 +91,8 @@ struct

module Commit = struct
module CA = struct
module Val = CT
module Key = Hash
module Val = CT
include CA (Key) (Val)
end

Expand Down Expand Up @@ -213,6 +166,8 @@ module Of_private = Store.Make

module type CONTENT_ADDRESSABLE_STORE = S.CONTENT_ADDRESSABLE_STORE

module type APPEND_ONLY_STORE = S.APPEND_ONLY_STORE

module type ATOMIC_WRITE_STORE = S.ATOMIC_WRITE_STORE

module type TREE = S.TREE
Expand All @@ -225,6 +180,8 @@ type 'a diff = 'a Diff.t

module type CONTENT_ADDRESSABLE_STORE_MAKER = S.CONTENT_ADDRESSABLE_STORE_MAKER

module type APPEND_ONLY_STORE_MAKER = S.APPEND_ONLY_STORE_MAKER

module type ATOMIC_WRITE_STORE_MAKER = S.ATOMIC_WRITE_STORE_MAKER

module type S_MAKER = S.MAKER
Expand Down
34 changes: 17 additions & 17 deletions src/irmin/irmin.mli
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ module Type : sig
val compare : 'a t -> 'a -> 'a -> int
(** [compare t] compares values of type [t]. *)

val hash : 'a t -> ?seed:int -> 'a -> int
val short_hash : 'a t -> ?seed:int -> 'a -> int
(** [hash t x] is a short hash of [x] of type [t]. *)

(** The type for pretty-printers for CLI arguments. *)
Expand Down Expand Up @@ -440,8 +440,8 @@ module Type : sig
(** The type for size function related to binary encoder/decoders. *)
type 'a size_of = ?headers:bool -> 'a -> int option

val pre_digest : 'a t -> 'a -> string
(** [pre_digest t x] is the string representation of [x], of type
val pre_hash : 'a t -> 'a -> string
(** [pre_hash t x] is the string representation of [x], of type
[t], which will be used to compute the digest of the value. By
default it's [to_bin_string t x] but it can be overriden by {!v},
{!like} and {!map} operators. *)
Expand Down Expand Up @@ -480,8 +480,8 @@ module Type : sig
bin:'a encode_bin * 'a decode_bin * 'a size_of ->
equal:('a -> 'a -> bool) ->
compare:('a -> 'a -> int) ->
hash:(?seed:int -> 'a -> int) ->
pre_digest:('a -> string) ->
short_hash:(?seed:int -> 'a -> int) ->
pre_hash:('a -> string) ->
'a t

val like :
Expand All @@ -490,8 +490,8 @@ module Type : sig
?bin:'a encode_bin * 'a decode_bin * 'a size_of ->
?equal:('a -> 'a -> bool) ->
?compare:('a -> 'a -> int) ->
?hash:('a -> int) ->
?pre_digest:('a -> string) ->
?short_hash:('a -> int) ->
?pre_hash:('a -> string) ->
'a t ->
'a t

Expand All @@ -501,8 +501,8 @@ module Type : sig
?bin:'a encode_bin * 'a decode_bin * 'a size_of ->
?equal:('a -> 'a -> bool) ->
?compare:('a -> 'a -> int) ->
?hash:('a -> int) ->
?pre_digest:('a -> string) ->
?short_hash:('a -> int) ->
?pre_hash:('a -> string) ->
'b t ->
('b -> 'a) ->
('a -> 'b) ->
Expand Down Expand Up @@ -1037,15 +1037,15 @@ module Hash : sig
(** The type for digest hashes. *)
type t

val digest : string -> t
val hash : string -> t
(** Compute a deterministic store key from a string. *)

val hash : t -> int
(** [hash h] is a small hash of [h], to be used for instance as
val short_hash : t -> int
(** [short_hash h] is a small hash of [h], to be used for instance as
the `hash` function of an OCaml [Hashtbl]. *)

val digest_size : int
(** [digest_size] is the size of hash results, in bytes. *)
val hash_size : int
(** [hash_size] is the size of hash results, in bytes. *)

(** {1 Value Types} *)

Expand Down Expand Up @@ -1178,7 +1178,7 @@ module Contents : sig
module Key : sig
include Hash.S with type t = key

val digest : value -> key
val hash : value -> key
end

(** [Val] provides base functions for user-defined contents values. *)
Expand Down Expand Up @@ -1604,7 +1604,7 @@ module Private : sig
module Key : sig
include Hash.S with type t = key

val digest : value -> key
val hash : value -> key
end

(** [Metadata] provides base functions for node metadata. *)
Expand Down Expand Up @@ -1809,7 +1809,7 @@ module Private : sig
module Key : sig
include Hash.S with type t = key

val digest : value -> key
val hash : value -> key
end

(** [Val] provides functions for commit values. *)
Expand Down
Loading