123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122(*
* 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!ImportincludePack_index_intfmoduleMake(K:Irmin.Hash.S)=structmoduleKey=structtypet=K.t[@@derivingirmin~short_hash~equal~to_bin_string~decode_bin]lethash=short_hash?seed:Nonelethash_size=30letencode=to_bin_stringletencoded_size=K.hash_sizeletdecodesoff=decode_bins(refoff)endmoduleVal=structtypet=int63*int*Pack_value.Kind.t[@@derivingirmin]letencoded_size=(64/8)+(32/8)+1letencode((off,len,kind):t)=letbuf=Bytes.createencoded_sizeinBytes.set_int64_bebuf0(Int63.to_int64off);Bytes.set_int32_bebuf8(Int32.of_intlen);Bytes.setbuf12(Pack_value.Kind.to_magickind);(* Bytes.unsafe_to_string usage: buf is local, uniquely owned. We assume the various
functions above return unique ownership of buf. Then in the call to
Bytes.unsafe_to_string we give up unique ownership of buf for ownership of the
resulting string. This is safe. *)Bytes.unsafe_to_stringbufletdecodespos:t=(* Bytes.unsafe_of_string usage: s is shared ownership; buf is shared ownership (we
cannot mutate buf) and the lifetime of buf ends on return from this function; we
assume the Bytes.get... functions require only shared ownership. This usage is
safe. *)letbuf=Bytes.unsafe_of_stringsinletoff=Bytes.get_int64_bebufpos|>Int63.of_int64inletlen=Bytes.get_int32_bebuf(pos+8)|>Int32.to_intinletkind=Bytes.getbuf(pos+12)|>Pack_value.Kind.of_magic_exnin(off,len,kind)endmoduleStats=Index.StatsmoduleI=IndexmoduleIndex=Index_unix.Make(Key)(Val)(Index.Cache.Unbounded)includeIndexmoduleIo=Io.Unixletv_exn=letcache=NoneinIndex.v?cacheletv?flush_callback?fresh?readonly?throttle?lru_size~log_sizeroot=tryOk(v_exn?flush_callback?fresh?readonly?throttle?lru_size~log_sizeroot)with|I.RO_not_allowed->(* Happens when [fresh = true = readonly] *)assertfalse|Index_unix.Private.Raw.Not_written->(* This is not expected to be raised but let's catch anyway to trigger
a more precise error instead (i.e. the [assert false] below). This
error is expected to be raised when a RO instance attemps an opening
on a non-existing file. *)assertfalse|Unix.Unix_error(x,y,z)->Error(`Io_misc(x,y,z))|Failuremsg->Error(`Index_failuremsg)letadd?overcommittkv=replace?overcommittkvletfindtk=matchfindtkwithexceptionNot_found->None|h->Somehletclose_exnt=Index.closetletcloset=tryclose_exnt;Ok()with|I.RO_not_allowed->Error`Ro_not_allowed|Index_unix.Private.Raw.Not_written->assertfalse|Unix.Unix_error(x,y,z)->Error(`Io_misc(x,y,z))|Failuremsg->Error(`Index_failuremsg)letreloadt=tryIndex.synct;Ok()with|I.RO_not_allowed->Error`Ro_not_allowed|Index_unix.Private.Raw.Not_written->assertfalse|Unix.Unix_error(x,y,z)->Error(`Io_misc(x,y,z))|Failuremsg->Error(`Index_failuremsg)letflusht~with_fsync=tryIndex.flush~no_callback:()~with_fsynct;Ok()with|I.RO_not_allowed->Error`Ro_not_allowed|Index_unix.Private.Raw.Not_written->assertfalse|Unix.Unix_error(x,y,z)->Error(`Io_misc(x,y,z))|Failuremsg->Error(`Index_failuremsg)end