Skip to content

Commit

Permalink
use dune variants, preserve timestamp function in type t (#6)
Browse files Browse the repository at this point in the history
* defunctorise, use mirage-ptime

* provide set_last_modified, to set the last_modified to a specific date.

This takes priority, and is useful for e.g. crunch - where the timestamp of the
mirage-kv-mem creation should be recorded.

* preserve timestamp function in t

* update dependencies
  • Loading branch information
hannesm authored Feb 10, 2025
1 parent 7ea598b commit df75cb0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 81 deletions.
9 changes: 4 additions & 5 deletions mirage-kv-mem.opam
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ depends: [
"ocaml" {>= "4.08.0"}
"dune" {>= "1.3.0"}
"alcotest" {with-test}
"mirage-clock" {>= "3.0.0"}
"mirage-ptime" {>= "5.0.0"}
"mirage-kv" {>= "6.0.0"}
"fmt"
"ptime"
"mirage-clock-unix" {>= "3.0.0"}
"optint"
"fmt" {>= "0.9.0"}
"ptime" {>= "1.1.0"}
"optint" {>= "0.3.0"}
]
conflicts: [ "result" {< "1.5"} ]

Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(library
(name mirage_kv_mem)
(public_name mirage-kv-mem)
(libraries mirage-kv fmt ptime mirage-clock optint))
(libraries mirage-kv fmt ptime mirage-ptime optint))
114 changes: 52 additions & 62 deletions src/mirage_kv_mem.ml
Original file line number Diff line number Diff line change
Expand Up @@ -205,82 +205,72 @@ module Pure = struct

end

module Make (CLOCK : Mirage_clock.PCLOCK) = struct
type key = Mirage_kv.Key.t

[@@@warning "-34"]
type nonrec error = error
let pp_error = pp_error

[@@@warning "-34"]
type nonrec write_error = write_error
let pp_write_error = pp_write_error
type key = Mirage_kv.Key.t

let now () = Ptime.v (CLOCK.now_d_ps ())
type t = Pure.t ref * (unit -> Ptime.t)

let connect () = Lwt.return (ref (Pure.empty (now ()) ()))
let disconnect _t = Lwt.return ()
let connect ?(now = Mirage_ptime.now) () =
Lwt.return (ref (Pure.empty (now ()) ()), now)

type t = Pure.t ref
let disconnect _t = Lwt.return ()

let last_modified dict key =
Lwt.return @@ Pure.last_modified !dict key
let last_modified dict key =
Lwt.return @@ Pure.last_modified !(fst dict) key

let digest dict key =
Lwt.return @@ match Pure.get_node !dict key with
| Ok (Value (_, data)) -> Ok (Digest.string data)
| Ok (Dictionary (mtime, dict)) ->
let data = Fmt.to_to_string Pure.pp (Dictionary (mtime, dict)) in
Ok (Digest.string data)
| Error e -> Error e
let digest dict key =
Lwt.return @@ match Pure.get_node !(fst dict) key with
| Ok (Value (_, data)) -> Ok (Digest.string data)
| Ok (Dictionary (mtime, dict)) ->
let data = Fmt.to_to_string Pure.pp (Dictionary (mtime, dict)) in
Ok (Digest.string data)
| Error e -> Error e

let exists dict key =
Lwt.return @@ match Pure.get_node !dict key with
| Ok (Value _) -> Ok (Some `Value)
| Ok (Dictionary _) -> Ok (Some `Dictionary)
| Error (`Not_found _) -> Ok None
| Error e -> Error e
let exists dict key =
Lwt.return @@ match Pure.get_node !(fst dict) key with
| Ok (Value _) -> Ok (Some `Value)
| Ok (Dictionary _) -> Ok (Some `Dictionary)
| Error (`Not_found _) -> Ok None
| Error e -> Error e

let get dict key = Lwt.return @@ Pure.get !dict key
let get dict key = Lwt.return @@ Pure.get !(fst dict) key

let get_partial dict key ~offset ~length =
Lwt.return @@ Pure.get_partial !dict key ~offset ~length
let get_partial dict key ~offset ~length =
Lwt.return @@ Pure.get_partial !(fst dict) key ~offset ~length

let size dict key = Lwt.return @@ Pure.size !dict key
let size dict key = Lwt.return @@ Pure.size !(fst dict) key

let remove dict key = Lwt.return @@ match Pure.remove !dict key (now ()) with
| Error e -> Error e
| Ok dict' -> dict := dict'; Ok ()
let remove dict key = Lwt.return @@ match Pure.remove !(fst dict) key ((snd dict) ()) with
| Error e -> Error e
| Ok dict' -> fst dict := dict'; Ok ()

let list dict key = Lwt.return @@ Pure.list !dict key
let list dict key = Lwt.return @@ Pure.list !(fst dict) key

let set dict key data = Lwt.return @@ match Pure.set !dict key (now ()) data with
| Error e -> Error e
| Ok dict' -> dict := dict'; Ok ()
let set dict key data = Lwt.return @@ match Pure.set !(fst dict) key ((snd dict) ()) data with
| Error e -> Error e
| Ok dict' -> fst dict := dict'; Ok ()

let set_partial dict key ~offset data =
Lwt.return @@ match Pure.set_partial !dict key (now ()) ~offset data with
| Error e -> Error e
| Ok dict' -> dict := dict'; Ok ()
let set_partial dict key ~offset data =
Lwt.return @@ match Pure.set_partial !(fst dict) key ((snd dict) ()) ~offset data with
| Error e -> Error e
| Ok dict' -> fst dict := dict'; Ok ()

let rename dict ~source ~dest =
Lwt.return @@ match Pure.rename !dict ~source ~dest (now ()) with
| Error e -> Error e
| Ok dict' -> dict := dict'; Ok ()
let rename dict ~source ~dest =
Lwt.return @@ match Pure.rename !(fst dict) ~source ~dest ((snd dict) ()) with
| Error e -> Error e
| Ok dict' -> fst dict := dict'; Ok ()

let pp fmt dict = Pure.pp fmt !dict
let pp fmt dict = Pure.pp fmt !(fst dict)

let equal a b = Pure.equal !a !b
let equal a b = Pure.equal !(fst a) !(fst b)

let allocate dict key ?last_modified size =
let open Lwt.Infix in
exists dict key >|= function
| Error _ as e -> e
| Ok Some _ -> Error (`Already_present key)
| Ok None ->
let data = String.make (Optint.Int63.to_int size) '\000' in
let now = Option.value ~default:(now ()) last_modified in
match Pure.set !dict key now data with
| Error e -> Error e
| Ok dict' -> dict := dict'; Ok ()
end
let allocate dict key ?last_modified size =
let open Lwt.Infix in
exists dict key >|= function
| Error _ as e -> e
| Ok Some _ -> Error (`Already_present key)
| Ok None ->
let data = String.make (Optint.Int63.to_int size) '\000' in
let now = Option.value ~default:((snd dict) ()) last_modified in
match Pure.set !(fst dict) key now data with
| Error e -> Error e
| Ok dict' -> fst dict := dict'; Ok ()
19 changes: 6 additions & 13 deletions src/mirage_kv_mem.mli
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
type error = Mirage_kv.error
val pp_error : error Fmt.t

type write_error = Mirage_kv.write_error
val pp_write_error : write_error Fmt.t

module Pure : sig
type t
Expand All @@ -23,15 +21,10 @@ module Pure : sig
val pp : t Fmt.t
end

module Make (Clock : Mirage_clock.PCLOCK) : sig
type nonrec error = error
type nonrec write_error = write_error
include Mirage_kv.RW
with type write_error := write_error
and type error := error

include Mirage_kv.RW
with type write_error := write_error
and type error := error

val connect : unit -> t Lwt.t
val pp : t Fmt.t
val equal : t -> t -> bool
end
val connect : ?now:(unit -> Ptime.t) -> unit -> t Lwt.t
val pp : t Fmt.t
val equal : t -> t -> bool

0 comments on commit df75cb0

Please sign in to comment.