123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146(*
* Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)open!Importletsrc=Logs.Src.create"irmin.mem"~doc:"Irmin in-memory store"moduleLog=(valLogs.src_logsrc:Logs.LOG)moduleRead_only(K:Irmin.Type.S)(V:Irmin.Type.S)=structmoduleKMap=Map.Make(structtypet=K.tletcompare=Irmin.Type.(unstage(compareK.t))end)typekey=K.ttypevalue=V.ttype'at={mutablet:valueKMap.t}letmap={t=KMap.empty}letv_config=Lwt.returnmapletcleart=Log.debug(funf->f"clear");t.t<-KMap.empty;Lwt.return_unitletclose_=Log.debug(funf->f"close");Lwt.return_unitletcastt=(t:>read_writet)letbatchtf=f(castt)letpp_key=Irmin.Type.ppK.tletfind{t;_}key=Log.debug(funf->f"find %a"pp_keykey);tryLwt.return_some(KMap.findkeyt)withNot_found->Lwt.return_noneletmem{t;_}key=Log.debug(funf->f"mem %a"pp_keykey);Lwt.return(KMap.memkeyt)endmoduleAppend_only(K:Irmin.Type.S)(V:Irmin.Type.S)=structincludeRead_only(K)(V)letaddtkeyvalue=Log.debug(funf->f"add -> %a"pp_keykey);t.t<-KMap.addkeyvaluet.t;Lwt.return_unitendmoduleAtomic_write(K:Irmin.Type.S)(V:Irmin.Type.S)=structmoduleRO=Read_only(K)(V)moduleW=Irmin.Private.Watch.Make(K)(V)moduleL=Irmin.Private.Lock.Make(K)typet={t:unitRO.t;w:W.t;lock:L.t}typekey=RO.keytypevalue=RO.valuetypewatch=W.watchletwatches=W.v()letlock=L.v()letvconfig=let*t=RO.vconfiginLwt.return{t;w=watches;lock}letcloset=W.cleart.w>>=fun()->RO.closet.tletfindt=RO.findt.tletmemt=RO.memt.tletwatch_keyt=W.watch_keyt.wletwatcht=W.watcht.wletunwatcht=W.unwatcht.wletlistt=Log.debug(funf->f"list");RO.KMap.fold(funk_acc->k::acc)t.t.RO.t[]|>Lwt.returnletsettkeyvalue=Log.debug(funf->f"update");let*()=L.with_lockt.lockkey(fun()->t.t.RO.t<-RO.KMap.addkeyvaluet.t.RO.t;Lwt.return_unit)inW.notifyt.wkey(Somevalue)letremovetkey=Log.debug(funf->f"remove");let*()=L.with_lockt.lockkey(fun()->t.t.RO.t<-RO.KMap.removekeyt.t.RO.t;Lwt.return_unit)inW.notifyt.wkeyNoneletequal_v_opt=Irmin.Type.(unstage(equal(optionV.t)))lettest_and_settkey~test~set=Log.debug(funf->f"test_and_set");let*updated=L.with_lockt.lockkey(fun()->let+v=findtkeyinifequal_v_opttestvthenlet()=matchsetwith|None->t.t.RO.t<-RO.KMap.removekeyt.t.RO.t|Somev->t.t.RO.t<-RO.KMap.addkeyvt.t.RO.tintrueelsefalse)inlet+()=ifupdatedthenW.notifyt.wkeysetelseLwt.return_unitinupdatedletcleart=W.cleart.w>>=fun()->RO.cleart.tendletconfig()=Irmin.Private.Conf.emptymoduleMake=Irmin.Make(Irmin.Content_addressable(Append_only))(Atomic_write)moduleKV(C:Irmin.Contents.S)=Make(Irmin.Metadata.None)(C)(Irmin.Path.String_list)(Irmin.Branch.String)(Irmin.Hash.BLAKE2B)(* Enforce that {!KV} is a sub-type of {!Irmin.KV_MAKER}. *)moduleKV_is_a_KV_MAKER:Irmin.KV_MAKER=KV