12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(*
* Copyright (c) 2018-2022 Tarides <contact@tarides.com>
*
* 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!ImportincludeDict_intfmoduleMake(Fm:File_manager.S)=structmoduleFm=Fmtypet={capacity:int;cache:(string,int)Hashtbl.t;index:(int,string)Hashtbl.t;fm:Fm.t;mutablelast_refill_offset:int63;}moduleFile=structletappend_exnt=Fm.Dict.append_exn(Fm.dictt.fm)letoffsett=Fm.Dict.end_poff(Fm.dictt.fm)letread_to_stringt=Fm.Dict.read_to_string(Fm.dictt.fm)endtypenonrecint32=int32[@@derivingirmin~to_bin_string~decode_bin]letappend_stringtv=letlen=Int32.of_int(String.lengthv)inletbuf=int32_to_bin_stringlen^vinFile.append_exntbuf(* Refill is only called once for a RW instance *)letrefillt=letopenResult_syntaxinletfrom=t.last_refill_offsetinletlen=Int63.to_intInt63.Syntax.(File.offsett-from)int.last_refill_offset<-File.offsett;let+raw=File.read_to_stringt~off:from~leninletpos_ref=ref0inletrecauxn=if!pos_ref>=lenthen()elseletv=decode_bin_int32rawpos_refinletlen=Int32.to_intvinletv=String.subraw!pos_refleninpos_ref:=!pos_ref+len;Hashtbl.addt.cachevn;Hashtbl.addt.indexnv;(aux[@tailcall])(n+1)in(aux[@tailcall])(Hashtbl.lengtht.cache)letindextv=[%log.debug"[dict] index %S"v];trySome(Hashtbl.findt.cachev)withNot_found->letid=Hashtbl.lengtht.cacheinifid>t.capacitythenNoneelse(append_stringtv;Hashtbl.addt.cachevid;Hashtbl.addt.indexidv;Someid)letfindtid=[%log.debug"[dict] find %d"id];letv=trySome(Hashtbl.findt.indexid)withNot_found->Noneinvletdefault_capacity=100_000letvfm=letopenResult_syntaxinletcache=Hashtbl.create997inletindex=Hashtbl.create997inletlast_refill_offset=Int63.zeroinlett={capacity=default_capacity;index;cache;fm;last_refill_offset}inlet*()=refilltinFm.register_dict_consumerfm~after_reload:(fun()->refillt);Oktletclose_=()end