123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128(* The MIT License
Copyright (c) 2019 Craig Ferguson <craig@tarides.com>
Thomas Gazagnaire <thomas@tarides.com>
Ioana Cristescu <ioana@tarides.com>
Clément Pascutto <clement@tarides.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. *)open!ImportmoduletypeELT=sigtypetvalencoded_size:intvaldecode:string->int->tendmoduletypeS=sigincludeSearch.ARRAYtypeiovalv:io->tendmoduleMake(IO:Io.S)(Elt:ELT):Swithtypeio=IO.tandtypeelt=Elt.t=structmoduleElt=structincludeEltletencoded_sizeL=Int63.of_intencoded_sizeendtypeio=IO.ttypeelt=Elt.ttypebuffer={buf:bytes;low_off:int63;high_off:int63}typet={io:IO.t;mutablebuffer:bufferoption}letvio={io;buffer=None}letget_entry_from_ioiooff=letbuf=Bytes.createElt.encoded_sizeinletn=IO.readio~off~len:Elt.encoded_sizebufinassert(n=Elt.encoded_size);Elt.decode(Bytes.unsafe_to_stringbuf)0letget_entry_from_bufferbufoff=letbuf_off=Int63.(to_int_exn(off-buf.low_off))inassert(buf_off<=Bytes.lengthbuf.buf);Elt.decode(Bytes.unsafe_to_stringbuf.buf)buf_offletis_in_buffertoff=matcht.bufferwith|None->false|Someb->Int63.compareoffb.low_off>=0&&Int63.compareoffb.high_off<=0letgetti=letoff=Int63.(i*Elt.encoded_sizeL)inmatcht.bufferwith|Somebwhenis_in_buffertoff->(tryget_entry_from_bufferboffwith_->assertfalse)|_->get_entry_from_iot.iooffletlengtht=Int63.div(IO.offsett.io)Elt.encoded_sizeLletmax_buffer_size=(* The prefetched area should not exceed 4096 in most cases, thanks to the
fan out. However, if the hash function is not well distributed, some
exceptions might happen where the prefetched area actually exceeds 4096.
As long as this excess is reasonable (x8), we still want to prefetch. *)8*4096letbuf=Bytes.createmax_buffer_sizeletset_buffert~low~high=letrange=Elt.encoded_size*(1+Int63.(to_int_exn(high-low)))inletlow_off=Int63.mullowElt.encoded_sizeLinlethigh_off=Int63.mulhighElt.encoded_sizeLinletn=IO.readt.io~off:low_off~len:rangebufinassert(n=range);t.buffer<-Some{buf;low_off;high_off}letpre_fetcht~low~high=letrange=Elt.encoded_size*(1+Int63.(to_int_exn(high-low)))inifInt63.comparelowhigh>0thenLog.warn(funm->m"Requested pre-fetch region is empty: [%a, %a]"Int63.pplowInt63.pphigh)elseifrange>max_buffer_sizethenLog.warn(funm->m"Requested pre-fetch [%a, %a] is larger than %d"Int63.pplowInt63.pphighmax_buffer_size)elsematcht.bufferwith|Someb->letlow_buf,high_buf=Int63.(divb.low_offElt.encoded_sizeL,divb.high_offElt.encoded_sizeL)iniflow>=low_buf&&high<=high_bufthenLog.debug(funm->m"Pre-existing buffer [%a, %a] encloses requested pre-fetch \
[%a, %a]"Int63.pplow_bufInt63.pphigh_bufInt63.pplowInt63.pphigh)else(Log.warn(funm->m"Current buffer [%a, %a] insufficient. Prefetching in range \
[%a, %a]"Int63.pplow_bufInt63.pphigh_bufInt63.pplowInt63.pphigh);set_buffert~low~high)|None->Log.debug(funm->m"No existing buffer. Prefetching in range [%a, %a]"Int63.pplowInt63.pphigh);set_buffert~low~highend