123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167(*
* Copyright (c) 2013-2022 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)moduleConf=structincludeIrmin.Backend.Confletspec=Spec.v"mem"letrootconfig=find_rootconfig|>Option.value ~default:"."endmoduleRead_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;root:string}letnew_instanceroot={t=KMap.empty;root}letv=letcache:(string,'at)Hashtbl.t=Hashtbl.create0infunconfig->letroot=Conf.rootconfiginlett=matchHashtbl.find_optcacherootwith|None->lett=new_instancerootinHashtbl.addcacheroott;t|Somet->tinLwt.returntletcleart=[%log.debug"clear"];t.t<-KMap.empty;Lwt.return_unitletclose_=[%log.debug"close"];Lwt.return_unitletcastt=(t:>read_writet)letbatchtf=f(castt)letpp_key=Irmin.Type.ppK.tletfind{t;_}key=[%log.debug"find %a"pp_keykey];tryLwt.return_some(KMap.findkeyt)withNot_found->Lwt.return_noneletmem{t;_}key=[%log.debug"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"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.Backend.Watch.Make(K)(V)moduleL=Irmin.Backend.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"list"];RO.KMap.fold(funk_acc->k::acc)t.t.RO.t[]|>Lwt.returnletsettkeyvalue=[%log.debug"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"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"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()=Conf.emptyConf.specmoduleContent_addressable=Irmin.Content_addressable.Make(Append_only)moduleS=Irmin.Maker(Content_addressable)(Atomic_write)moduleKV=Irmin.KV_maker(Content_addressable)(Atomic_write)includeS(* Enforce that {!S} is a sub-type of {!Irmin.Maker}. *)moduleMaker_is_a_maker:Irmin.Maker=S(* Enforce that {!KV} is a sub-type of {!Irmin.KV_maker}. *)moduleKV_is_a_KV:Irmin.KV_maker=KV