Skip to content

Commit fd680d7

Browse files
authored
Merge pull request #28 from palainp/update-api
add partial read/write, size and rename
2 parents a769e8b + 0440ee0 commit fd680d7

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/mirage_kv.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ module type RO = sig
6666
type key = Key.t
6767
val exists: t -> key -> ([`Value | `Dictionary] option, error) result Lwt.t
6868
val get: t -> key -> (string, error) result Lwt.t
69+
val get_partial: t -> key -> offset:int -> length:int -> (string, error) result Lwt.t
6970
val list: t -> key -> ((string * [`Value | `Dictionary]) list, error) result Lwt.t
7071
val last_modified: t -> key -> (int * int64, error) result Lwt.t
7172
val digest: t -> key -> (string, error) result Lwt.t
73+
val size: t -> key -> (int, error) result Lwt.t
7274
end
7375

7476
type write_error = [ error | `No_space | `Too_many_retries of int ]
@@ -84,6 +86,8 @@ module type RW = sig
8486
type nonrec write_error = private [> write_error]
8587
val pp_write_error: write_error Fmt.t
8688
val set: t -> key -> string -> (unit, write_error) result Lwt.t
89+
val set_partial: t -> key -> offset:int -> string -> (unit, write_error) result Lwt.t
8790
val remove: t -> key -> (unit, write_error) result Lwt.t
91+
val rename: t -> source:key -> dest:key -> (unit, write_error) result Lwt.t
8892
val batch: t -> ?retries:int -> (t -> 'a Lwt.t) -> 'a Lwt.t
8993
end

src/mirage_kv.mli

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ module type RO = sig
134134
The result is [Error (`Value_expected k)] if [k] refers to a
135135
dictionary in [t]. *)
136136

137+
val get_partial: t -> key -> offset:int -> length:int -> (string, error) result Lwt.t
138+
(** [get_partial t k ~offset ~length] is the [length] bytes wide value
139+
bound at [offset] of [k] in [t].
140+
141+
If the size of [k] is less than [offset], [get_partial] returns an
142+
empty string.
143+
If the size of [k] is less than [offset]+[length], [get_partial]
144+
returns a short string.
145+
The result is [Error (`Value_expected k)] if [k] refers to a
146+
dictionary in [t]. *)
147+
137148
val list: t -> key -> ((string * [`Value | `Dictionary]) list, error) result Lwt.t
138149
(** [list t k] is the list of entries and their types in the
139150
dictionary referenced by [k] in [t].
@@ -161,6 +172,10 @@ module type RO = sig
161172
When the value bound to [k] is a dictionary, the digest is a
162173
unique and deterministic digest of its entries. *)
163174

175+
val size: t -> key -> (int, error) result Lwt.t
176+
(** [size t k] is the size of [k] in [t]. *)
177+
178+
164179
end
165180

166181
type write_error = [
@@ -199,6 +214,19 @@ module type RW = sig
199214
{!batch} operation, where durability will be guaranteed at the
200215
end of the batch. *)
201216

217+
val set_partial: t -> key -> offset:int -> string -> (unit, write_error) result Lwt.t
218+
(** [set_partial t k offset v] attempts to write [v] at [offset] in the
219+
value bound to [k] in [t].
220+
If [k] contains directories that do not exist, [set_partial] will
221+
attempt to create them.
222+
If the size of [k] is less than [offset], [set_partial] appends [v]
223+
at the end of [k].
224+
If the size of [k] is greater than [offset]+length of [v],
225+
[set_partial] leaves the last bytes of [k] unchanged.
226+
227+
The result is [Error (`Value_expected k)] if [k] refers to a
228+
dictionary in [t]. *)
229+
202230
val remove: t -> key -> (unit, write_error) result Lwt.t
203231
(** [remove t k] removes any binding of [k] in [t]. If [k] was bound
204232
to a dictionary, the full dictionary will be removed.
@@ -207,6 +235,19 @@ module type RW = sig
207235
enclosing {!batch} operation, where durability will be guaranteed
208236
at the end of the batch. *)
209237

238+
val rename: t -> source:key -> dest:key -> (unit, write_error) result Lwt.t
239+
(** [rename t source dest] rename [source] to [dest] in [t].
240+
If [source] and [dest] are both bound to values in [t], [dest]
241+
is removed and the binding of [source] is moved to [dest].
242+
If [dest] is bound to a dictionary in [t], [source] is moved
243+
inside [dest]. If [source] is bound to a dictionary, the full
244+
dictionary is moved.
245+
246+
The result is [Error (`Not_found source)] if [source] does not
247+
exists in [t].
248+
The result is [Error (`Value_expected source)] if [source] is
249+
bound to a dictionary in [t] and [dest] is bound to a value in [t]. *)
250+
210251
val batch: t -> ?retries:int -> (t -> 'a Lwt.t) -> 'a Lwt.t
211252
(** [batch t f] run [f] in batch. Ensure the durability of
212253
operations.

0 commit comments

Comments
 (0)