Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file context_dump.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* Copyright (c) 2018-2021 Nomadic Labs. <contact@nomadic-labs.com> *)(* Copyright (c) 2018-2020 Tarides <contact@tarides.com> *)(* Copyright (c) 2020 Metastate AG <hello@metastate.dev> *)(* *)(* 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. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)includeContext_dump_intf(*****************************************************************************)typeerror+=|System_write_errorofstring|Context_not_foundofBytes.t|System_read_errorofstring|Inconsistent_context_dump|Restore_context_failurelet()=letopenData_encodinginregister_error_kind`Permanent~id:"context_dump.writing_error"~title:"Writing error"~description:"Cannot write in file for context dump"~pp:(funppfs->Format.fprintfppf"Unable to write file for context dumping: %s"s)(obj1(req"context_dump_no_space"string))(functionSystem_write_errors->Somes|_->None)(funs->System_write_errors);register_error_kind`Permanent~id:"context_dump.context_not_found"~title:"Context not found"~description:"Cannot find context corresponding to hash"~pp:(funppfmb->Format.fprintfppf"No context with hash: %s"(Bytes.to_stringmb))(obj1(req"context_not_found"bytes))(functionContext_not_foundmb->Somemb|_->None)(funmb->Context_not_foundmb);register_error_kind`Permanent~id:"context_dump.system_read_error"~title:"System read error"~description:"Failed to read file"~pp:(funppfuerr->Format.fprintfppf"Error while reading file for context dumping: %s"uerr)(obj1(req"system_read_error"string))(functionSystem_read_errore->Somee|_->None)(fune->System_read_errore);register_error_kind`Permanent~id:"context_dump.inconsistent_context_dump"~title:"Inconsistent context dump"~description:"Error while reading context dump"~pp:(funppf()->Format.fprintfppf"Failed to read context dump. The provided file is inconsistent.")empty(functionInconsistent_context_dump->Some()|_->None)(fun()->Inconsistent_context_dump);register_error_kind`Permanent~id:"context_dump.restore_context_failure"~title:"Failed to restore context"~description:"Internal error while restoring the context"~pp:(funppf()->Format.fprintfppf"Internal error while restoring the context.")empty(functionRestore_context_failure->Some()|_->None)(fun()->Restore_context_failure)moduleMake(I:Dump_interface)=structtypecommand=|Root|Nodeof(string*I.Kinded_hash.t)list|Node_seqof(string*I.Kinded_hash.t,errortrace)Seq_es.t|Blobofbytes|Eocof{info:I.commit_info;parents:I.Commit_hash.tlist}|Eof(* Command encoding. *)letblob_encoding=letopenData_encodingincase~title:"blob"(Tag(Char.code'b'))bytes(functionBlobb->Someb|_->None)(functionb->Blobb)letnode_encoding=letopenData_encodingincase~title:"node"(Tag(Char.code'n'))(list(obj2(req"name"string)(req"hash"I.Kinded_hash.encoding)))(functionNodex->Somex|_->None)(functionx->Nodex)leteof_encoding=letopenData_encodingincase~title:"eof"(Tag(Char.code'e'))empty(functionEof->Some()|_->None)(fun()->Eof)letroot_encoding=letopenData_encodingincase~title:"root"(Tag(Char.code'r'))empty(functionRoot->Some()|_->None)(fun()->Root)leteoc_encoding=letopenData_encodingincase~title:"eoc"(Tag(Char.code'c'))(obj2(req"info"I.commit_info_encoding)(req"parents"(listI.Commit_hash.encoding)))(functionEoc{info;parents}->Some(info,parents)|_->None)(fun(info,parents)->Eoc{info;parents})letcommand_encoding=Data_encoding.union~tag_size:`Uint8[blob_encoding;node_encoding;eoc_encoding;root_encoding;eof_encoding](* IO toolkit. *)letrecread_stringrbuf~len=let(fd,buf,ofs,total)=!rbufinifBytes.lengthbuf-ofs<lenthen(letblen=Bytes.lengthbuf-ofsinletneu=Bytes.create(blen+1_000_000)inBytes.blitbufofsneu0blen;Lwt_unix.readfdneublen1_000_000>>=funbread->total:=!total+bread;ifbread=0thenfailInconsistent_context_dumpelseletneu=ifbread<>1_000_000thenBytes.subneu0(blen+bread)elseneuinrbuf:=(fd,neu,0,total);read_stringrbuf~len)elseletres=Bytes.sub_stringbufofsleninrbuf:=(fd,buf,ofs+len,total);returnresletread_mbytesrbufb=read_stringrbuf~len:(Bytes.lengthb)>>=?funstring->Bytes.blit_stringstring0b0(Bytes.lengthb);return()letset_int64bufi=letb=Bytes.create8inEndianBytes.BigEndian.set_int64b0i;Buffer.add_bytesbufbletget_int64rbuf=read_string~len:8rbuf>>=?funs->return@@EndianString.BigEndian.get_int64s0letget_charrbuf=read_string~len:1rbuf>>=?funs->return@@EndianString.BigEndian.get_int8s0letget_int4rbuf=read_string~len:4rbuf>>=?funs->return@@EndianString.BigEndian.get_int32s0letset_mbytesbufb=set_int64buf(Int64.of_int(Bytes.lengthb));Buffer.add_bytesbufb(* To decode a variable size string we need to: 1/ read the length of the
string, encoded on 4 bytes; 2/ reset the offset to the beginning of the string
encoding. *)letget_length_and_reset_offsetrbuf=get_int4rbuf>|=?Int32.to_int>>=?funlength->let(fd,buf,ofs,total)=!rbufinrbuf:=(fd,buf,ofs-4,total);Lwt.return_ok(length+4)letread_variable_length_stringrbuf=get_length_and_reset_offsetrbuf>>=?funlength_name->letb=Bytes.createlength_nameinread_mbytesrbufb>|=?fun()->letname=Data_encoding.(Binary.of_bytes_exnstring)bin(length_name,name)letread_fixed_length_hashrbuf=letlength_hash=1+4+32(*enum + size + hash*)inletb=Bytes.createlength_hashinread_mbytesrbufb>|=?fun()->lethash=Data_encoding.Binary.of_bytes_exnI.Kinded_hash.encodingbin(length_hash,hash)letread_seqrbuftotal=letstepi=ifi>=totalthenLwt.return_okNoneelseread_variable_length_stringrbuf>>=?fun(length_name,name)->read_fixed_length_hashrbuf>|=?fun(length_hash,hash)->letnode=(name,hash)inleti=i+length_name+length_hashinSome(node,i)inSeq_es.unfold_esstep0leteoc_encoding_raw=letopenData_encodinginobj2(req"info"I.commit_info_encoding)(req"parents"(listI.Commit_hash.encoding))letget_commandrbuf=get_int64rbuf>|=?Int64.to_int>>=?funtotal->get_charrbuf>|=?Char.chr>>=?funtag->letread_empty()=letlen=total-1inletb=Bytes.createleninread_mbytesrbufb>|=?fun()->Data_encoding.Binary.of_bytes_exnData_encoding.emptybinmatchtagwith|'r'->read_empty()>|=?fun()->Root|'e'->read_empty()>|=?fun()->Eof|'c'->letlen=total-1inletb=Bytes.createleninread_mbytesrbufb>|=?fun()->let(info,parents)=Data_encoding.Binary.of_bytes_exneoc_encoding_rawbinEoc{info;parents}|'b'->letlen=total-1inletb=Bytes.createleninread_mbytesrbufb>|=?fun()->letdata=Data_encoding.Binary.of_bytes_exnData_encoding.bytesbinBlobdata|'n'->get_int4rbuf>|=?Int32.to_int>>=?funlist_size->letdata=read_seqrbuflist_sizeinLwt.return_ok(Node_seqdata)|_->failRestore_context_failure(* Getter and setters *)letset_rootbuf=letroot=Rootinletbytes=Data_encoding.Binary.to_bytes_exncommand_encodingrootinset_mbytesbufbytesletset_treebuftree=(matchtreewith`Branchnode->Nodenode|`Leafblob->Blobblob)|>Data_encoding.Binary.to_bytes_exncommand_encoding|>set_mbytesbufletset_eocbufinfoparents=leteoc=Eoc{info;parents}inletbytes=Data_encoding.Binary.to_bytes_exncommand_encodingeocinset_mbytesbufbytesletset_endbuf=letbytes=Data_encoding.Binary.to_bytes_exncommand_encodingEofinset_mbytesbufbytesletserialize_tree~notify~maybe_flushbuf=I.tree_iteri_unique(funsub_tree->set_treebufsub_tree;maybe_flush()>>=fun()->notify())letdump_context_fdidxcontext_hash~context_fd=(* Dumping *)letbuf=Buffer.create1_000_000inletwritten=ref0inletflush()=letcontents=Buffer.contentsbufinBuffer.clearbuf;written:=!written+String.lengthcontents;Lwt_utils_unix.write_stringcontext_fdcontentsinletmaybe_flush()=ifBuffer.lengthbuf>1_000_000thenflush()elseLwt.return_unitinLwt.catch(fun()->I.checkoutidxcontext_hash>>=function|None->(* FIXME: dirty *)fail@@Context_not_found(I.Commit_hash.to_bytescontext_hash)|Somectxt->Animation.display_progress~every:1000~pp_print_step:(funfmti->Format.fprintffmt"Copying context: %dK elements, %s written"(i/1000)(if!written>1_048_576thenFormat.asprintf"%dMiB"(!written/1_048_576)elseFormat.asprintf"%dKiB"(!written/1_024)))(funnotify->set_rootbuf;I.context_treectxt|>serialize_tree~notify~maybe_flushbuf>>=funelements->letparents=I.context_parentsctxtinset_eocbuf(I.context_infoctxt)parents;set_endbuf;return_unit>>=?fun()->flush()>>=fun()->returnelements))(function|Unix.Unix_error(e,_,_)->fail@@System_write_error(Unix.error_messagee)|err->Lwt.failerr)(* Restoring *)letrestore_context_fdindex~expected_context_hash~fd~nb_context_elements=letread=ref0inletrbuf=ref(fd,Bytes.empty,0,read)in(* Editing the repository *)letadd_blobtblob=I.add_bytestblob>>=funtree->returntreeinletadd_dirtkeys=I.add_dirtkeys>>=?function|None->failRestore_context_failure|Sometree->returntreeinletrestore()=letfirst_pass()=get_commandrbuf>>=?function|Root->return_unit|_->failInconsistent_context_dumpinletrecsecond_passbatchctxtcontext_hashnotify=notify()>>=fun()->get_commandrbuf>>=?function|Node_seqcontents->add_dirbatchcontents>>=?funtree->second_passbatch(I.update_contextctxttree)context_hashnotify|Blobdata->add_blobbatchdata>>=?funtree->second_passbatch(I.update_contextctxttree)context_hashnotify|Eoc{info;parents}->(I.set_context~info~parentsctxtcontext_hash>>=function|false->failInconsistent_context_dump|true->return_unit)|_->failInconsistent_context_dumpinletcheck_eof()=get_commandrbuf>>=?function|Eof->return_unit|_->failInconsistent_context_dumpinfirst_pass()>>=?funblock_data->Animation.display_progress~every:1000~pp_print_step:(funfmti->Format.fprintffmt"Writing context: %dK/%dK (%d%%) elements, %s read"(i/1_000)(nb_context_elements/1_000)(100*i/nb_context_elements)(if!read>1_048_576thenFormat.asprintf"%dMiB"(!read/1_048_576)elseFormat.asprintf"%dKiB"(!read/1_024)))(funnotify->I.batchindex(funbatch->second_passbatch(I.make_contextindex)expected_context_hashnotify))>>=?fun()->check_eof()>>=?fun()->returnblock_datainLwt.catch(fun()->restore())(function|Unix.Unix_error(e,_,_)->fail@@System_read_error(Unix.error_messagee)|err->Lwt.failerr)end(* Legacy errors*)typeerror+=|Inconsistent_snapshot_file|Inconsistent_snapshot_data|Invalid_snapshot_versionofstring*stringlistlet()=letopenData_encodinginregister_error_kind`Permanent~id:"context_dump.inconsistent_snapshot_file"~title:"Inconsistent snapshot file"~description:"Error while opening snapshot file"~pp:(funppf()->Format.fprintfppf"Failed to read snapshot file. The provided file is inconsistent.")empty(functionInconsistent_snapshot_file->Some()|_->None)(fun()->Inconsistent_snapshot_file);register_error_kind`Permanent~id:"context_dump.inconsistent_snapshot_data"~title:"Inconsistent snapshot data"~description:"The data provided by the snapshot is inconsistent"~pp:(funppf()->Format.fprintfppf"The data provided by the snapshot file is inconsistent (context_hash \
does not correspond for block).")empty(functionInconsistent_snapshot_data->Some()|_->None)(fun()->Inconsistent_snapshot_data);register_error_kind`Permanent~id:"context_dump.invalid_snapshot_version"~title:"Invalid snapshot version"~description:"The version of the snapshot to import is not valid"~pp:(funppf(found,expected)->Format.fprintfppf"The snapshot to import has version \"%s\" but one of %a was expected."foundFormat.(pp_print_list~pp_sep:(funppf()->fprintfppf", ")(funppfversion->fprintfppf"\"%s\""version))expected)(obj2(req"found"string)(req"expected"(liststring)))(function|Invalid_snapshot_version(found,expected)->Some(found,expected)|_->None)(fun(found,expected)->Invalid_snapshot_version(found,expected))