Skip to content

Commit 05c581d

Browse files
authored
Merge pull request #34 from hannesm/add-allocate
RW: add a new function "allocate" which reserves some space for a new file
2 parents 3acccc9 + 9161b54 commit 05c581d

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

mirage-kv.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ depends: [
1919
"fmt" {>= "0.8.7"}
2020
"lwt" {>= "4.0.0"}
2121
"optint" {>= "0.2.0"}
22+
"ptime" {>= "1.0.0"}
2223
"alcotest" {with-test}
2324
]
2425
synopsis: "MirageOS signatures for key/value devices"

src/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
(library
22
(name mirage_kv)
33
(public_name mirage-kv)
4-
(libraries fmt optint lwt))
4+
(libraries fmt optint lwt ptime))

src/mirage_kv.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,26 @@ module type RO = sig
6868
val get: t -> key -> (string, error) result Lwt.t
6969
val get_partial: t -> key -> offset:Optint.Int63.t -> length:int -> (string, error) result Lwt.t
7070
val list: t -> key -> ((key * [`Value | `Dictionary]) list, error) result Lwt.t
71-
val last_modified: t -> key -> (int * int64, error) result Lwt.t
71+
val last_modified: t -> key -> (Ptime.t, error) result Lwt.t
7272
val digest: t -> key -> (string, error) result Lwt.t
7373
val size: t -> key -> (Optint.Int63.t, error) result Lwt.t
7474
end
7575

76-
type write_error = [ error | `No_space | `Rename_source_prefix of Key.t * Key.t ]
76+
type write_error = [ error | `No_space | `Rename_source_prefix of key * key | `Already_present of key ]
7777

7878
let pp_write_error ppf = function
7979
| #error as e -> pp_error ppf e
8080
| `No_space -> Fmt.string ppf "No space left on device"
8181
| `Rename_source_prefix (src, dest) ->
8282
Fmt.pf ppf "Rename: source %a is prefix of destination %a"
8383
Key.pp src Key.pp dest
84+
| `Already_present k -> Fmt.pf ppf "Key %a is already present" Key.pp k
8485

8586
module type RW = sig
8687
include RO
8788
type nonrec write_error = private [> write_error]
8889
val pp_write_error: write_error Fmt.t
90+
val allocate : t -> key -> ?last_modified:Ptime.t -> Optint.Int63.t -> (unit, write_error) result Lwt.t
8991
val set: t -> key -> string -> (unit, write_error) result Lwt.t
9092
val set_partial: t -> key -> offset:Optint.Int63.t -> string -> (unit, write_error) result Lwt.t
9193
val remove: t -> key -> (unit, write_error) result Lwt.t

src/mirage_kv.mli

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,10 @@ module type RO = sig
152152
The result is [Error (`Dictionary_expected k)] if [k] refers to a
153153
value in [t]. *)
154154

155-
val last_modified: t -> key -> (int * int64, error) result Lwt.t
155+
val last_modified: t -> key -> (Ptime.t, error) result Lwt.t
156156
(** [last_modified t k] is the last time the value bound to [k] in
157157
[t] has been modified.
158158
159-
The modification time [(d, ps)] is a span for the signed POSIX
160-
picosecond span [d] * 86_400e12 + [ps]. [d] is a signed number of
161-
POSIX days and [ps] a number of picoseconds in the range
162-
\[[0];[86_399_999_999_999_999L]\].
163-
164159
When the value bound to [k] is a dictionary, the modification
165160
time is the latest modification of all entries in that
166161
dictionary. This behaviour is only one level deep and not recursive. *)
@@ -182,6 +177,7 @@ type write_error = [
182177
| error
183178
| `No_space (** No space left on the device. *)
184179
| `Rename_source_prefix of key * key (** The source is a prefix of destination in rename. *)
180+
| `Already_present of key (** The key is already present. *)
185181
]
186182

187183
val pp_write_error: write_error Fmt.t
@@ -203,6 +199,15 @@ module type RW = sig
203199
val pp_write_error: write_error Fmt.t
204200
(** The pretty-printer for [pp_write_error]. *)
205201

202+
val allocate : t -> key -> ?last_modified:Ptime.t -> Optint.Int63.t ->
203+
(unit, write_error) result Lwt.t
204+
(** [allocate t key ~last_modified size] allocates space for [key] in [t] with
205+
the provided [size] and [last_modified]. This is useful for e.g.
206+
append-only backends that could still use {!set_partial}. The data will
207+
be filled with 0. If [key] already exists, [Error (`Already_present key)]
208+
is returned. If there's not enough space, [Error `No_space] is returned.
209+
*)
210+
206211
val set: t -> key -> string -> (unit, write_error) result Lwt.t
207212
(** [set t k v] replaces the binding [k -> v] in [t].
208213

0 commit comments

Comments
 (0)