struct
let flush t =
database t
>>= fun db ->
let action =
let key = Unique_id.create () in
let value = Ketrew_measurement.Collection.serialize t.measurements in
Trakeva.Action.(set ~collection:"measurements" ~key value)
in
begin Database.act db ~action
>>= function
| `Done ->
Ketrew_measurement.Collection.clear t.measurements;
return ()
| `Not_done -> fail (`Database_unavailable "measurements")
end
let get_all t =
let collection = "measurements" in
database t
>>= fun db ->
Database.get_all db ~collection
>>= fun all_keys ->
Deferred_list.while_sequential all_keys (fun key ->
Database.get db ~collection ~key
>>= function
| Some s ->
begin try
return (Ketrew_measurement.Collection.deserialize_exn s)
with e -> fail (`Deserialization (e, s))
end
| None -> fail (`Missing_data (fmt "Missing measurement (from %s)" key)))
>>| Ketrew_measurement.Collection.concat
>>= fun collection ->
return collection
end