package irmin-pack
Irmin backend which stores values in a pack file
Install
Dune Dependency
Authors
Maintainers
Sources
irmin-3.4.0.tbz
sha256=9e72efdc13324602d8da46e0492b1f4e09ad524149845e1a9106882f60ad6e1f
sha512=ae9cb4b086c7b93962aafc2ea339ac85c6d66bdf769f70a16f62fa5c74d9b194e12ac00b510be5ffac5adff3aa20fd69c4ca4067dcc966767fefcf5c02bc9969
doc/src/irmin-pack.unix/traverse_pack_file.ml.html
Source file traverse_pack_file.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
(* * 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! Import module Io = Io.Unix module Stats : sig type t val empty : unit -> t val add : t -> Pack_value.Kind.t -> unit val duplicate_entry : t -> unit val missing_hash : t -> unit val pp : t Fmt.t end = struct module Kind = Pack_value.Kind type t = { pack_values : int array; mutable duplicates : int; mutable missing_hashes : int; } let empty () = let pack_values = Array.make (List.length Kind.all) 0 in { pack_values; duplicates = 0; missing_hashes = 0 } let incr t n = t.pack_values.(n) <- t.pack_values.(n) + 1 let add t k = incr t (Kind.to_enum k) let duplicate_entry t = t.duplicates <- t.duplicates + 1 let missing_hash t = t.missing_hashes <- t.missing_hashes + 1 let pp = let open Fmt.Dump in let pack_values = ListLabels.map Kind.all ~f:(fun k -> let name = Fmt.str "%a" Kind.pp k in let index = Kind.to_enum k in field name (fun t -> t.pack_values.(index)) Fmt.int) in record (pack_values @ [ field "Duplicated entries" (fun t -> t.duplicates) Fmt.int; field "Missing entries" (fun t -> t.missing_hashes) Fmt.int; ]) end module type Args = sig module File_manager : File_manager.S module Hash : Irmin.Hash.S module Index : Pack_index.S with type key := Hash.t module Inode : Inode.S with type hash := Hash.t module Dict : Dict.S module Contents : Pack_value.S module Commit : Pack_value.S end module Make (Args : Args) : sig val run : [ `Reconstruct_index of [ `In_place | `Output of string ] | `Check_index | `Check_and_fix_index ] -> Irmin.config -> unit val test : [ `Reconstruct_index of [ `In_place | `Output of string ] | `Check_index | `Check_and_fix_index ] -> Irmin.config -> unit end = struct open Args module Errs = Io_errors.Make (Args.File_manager.Io) let pp_key = Irmin.Type.pp Hash.t let decode_key = Irmin.Type.(unstage (decode_bin Hash.t)) let decode_kind = Irmin.Type.(unstage (decode_bin Pack_value.Kind.t)) (* [Repr] doesn't yet support buffered binary decoders, so we hack one together by re-interpreting [Invalid_argument _] exceptions from [Repr] as requests for more data. *) exception Not_enough_buffer type index_value = int63 * int * Pack_value.Kind.t [@@deriving irmin ~equal ~pp] type index_binding = { key : Hash.t; data : index_value } type missing_hash = { idx_pack : int; binding : index_binding } let pp_binding ppf x = let off, len, kind = x.data in Fmt.pf ppf "@[<v 0>%a with hash %a@,pack offset = %a, length = %d@]" Pack_value.Kind.pp kind pp_key x.key Int63.pp off len module Index_reconstructor = struct let create ~dest config = let dest = match dest with | `Output path -> if Sys.file_exists path then Fmt.invalid_arg "Can't reconstruct index. File already exits."; path | `In_place -> if Conf.readonly config then raise Irmin_pack.RO_not_allowed; Conf.root config in let log_size = Conf.index_log_size config in [%log.app "Beginning index reconstruction with parameters: { log_size = %d }" log_size]; let index = Index.v_exn ~fresh:true ~readonly:false ~log_size dest in index let iter_pack_entry index key data = Index.add index key data; Ok () let finalise index () = (* Ensure that the log file is empty, so that subsequent opens with a smaller [log_size] don't immediately trigger a merge operation. *) [%log.app "Completed indexing of pack entries. Running a final merge ..."]; Index.try_merge index; Index.close_exn index end module Index_checker = struct let create config = let log_size = Conf.index_log_size config in [%log.app "Beginning index checking with parameters: { log_size = %d }" log_size]; let index = Index.v_exn ~fresh:false ~readonly:true ~log_size (Conf.root config) in (index, ref 0) let iter_pack_entry (index, idx_ref) key data = match Index.find index key with | None -> Error (`Missing_hash { idx_pack = !idx_ref; binding = { key; data } }) | Some data' when not @@ equal_index_value data data' -> Error `Inconsistent_entry | Some _ -> incr idx_ref; Ok () let finalise (index, _) () = Index.close_exn index end module Index_check_and_fix = struct let create config = let log_size = Conf.index_log_size config in [%log.app "Beginning index checking with parameters: { log_size = %d }" log_size]; let root = Conf.root config in let index = Index.v_exn ~fresh:false ~readonly:false ~log_size root in (index, ref 0) let iter_pack_entry (index, idx_ref) key data = match Index.find index key with | None -> Index.add index key data; Error (`Missing_hash { idx_pack = !idx_ref; binding = { key; data } }) | Some data' when not @@ equal_index_value data data' -> Error `Inconsistent_entry | Some _ -> incr idx_ref; Ok () let finalise (index, _) () = [%log.app "Completed indexing of pack entries. Running a final merge ..."]; Index.try_merge index; Index.close_exn index end let decode_entry_length = function | Pack_value.Kind.Contents -> Contents.decode_bin_length | Commit_v1 | Commit_v2 -> Commit.decode_bin_length | Inode_v1_stable | Inode_v1_unstable | Inode_v2_root | Inode_v2_nonroot -> Inode.decode_bin_length | Dangling_parent_commit -> assert false let decode_entry_exn ~off ~buffer ~buffer_off = try let buffer_pos = ref buffer_off in (* Decode the key and kind by hand *) let key = decode_key buffer buffer_pos in assert (!buffer_pos = buffer_off + Hash.hash_size); let kind = decode_kind buffer buffer_pos in assert (!buffer_pos = buffer_off + Hash.hash_size + 1); (* Get the length of the entire entry *) let entry_len = decode_entry_length kind buffer buffer_off in { key; data = (off, entry_len, kind) } with | Invalid_argument msg when msg = "index out of bounds" -> raise Not_enough_buffer | Invalid_argument msg when msg = "String.blit / Bytes.blit_string" -> raise Not_enough_buffer (* Read at most [len], by checking that [(off, len)] don't go out of bounds of the suffix file. *) let io_read_at_most ~off ~len b suffix = let bytes_after_off = let open Int63.Syntax in File_manager.Suffix.end_offset suffix - off in let len = let open Int63.Syntax in if bytes_after_off < Int63.of_int len then Int63.to_int bytes_after_off else len in File_manager.Suffix.read_exn suffix ~off ~len b; len let ingest_data_file ~initial_buffer_size ~progress ~total suffix iter_pack_entry = let buffer = ref (Bytes.create initial_buffer_size) in let refill_buffer ~from = let buffer_len = Bytes.length !buffer in let (_ : int) = io_read_at_most ~off:from ~len:buffer_len !buffer suffix in () in let expand_and_refill_buffer ~from = let length = Bytes.length !buffer in if length > 1_000_000_000 (* 1 GB *) then Fmt.failwith "Couldn't decode the value at offset %a in %d of buffer space. \ Corrupted data file?" Int63.pp from length else ( buffer := Bytes.create (2 * length); refill_buffer ~from) in let stats = Stats.empty () in let rec loop_entries ~buffer_off off missing_hash = if off >= total then (stats, missing_hash) else let buffer_off, off, missing_hash = match (* Bytes.unsafe_to_string usage: possibly safe, depending on details of implementation of decode_entry_exn TODO either justify clearly that this is safe, or change to use safe Bytes.to_string *) decode_entry_exn ~off ~buffer:(Bytes.unsafe_to_string !buffer) ~buffer_off with | { key; data } -> let off', entry_len, kind = data in let entry_lenL = Int63.of_int entry_len in assert (off = off'); [%log.debug "k = %a (off, len, kind) = (%a, %d, %a)" pp_key key Int63.pp off entry_len Pack_value.Kind.pp kind]; Stats.add stats kind; let missing_hash = match iter_pack_entry key data with | Ok () -> Option.map Fun.id missing_hash | Error `Inconsistent_entry -> Stats.duplicate_entry stats; Option.map Fun.id missing_hash | Error (`Missing_hash x) -> Stats.missing_hash stats; Some x in let off = Int63.Syntax.(off + entry_lenL) in progress entry_lenL; (buffer_off + entry_len, off, missing_hash) | exception Not_enough_buffer -> let () = if buffer_off > 0 then (* Try again with the value at the start of the buffer. *) refill_buffer ~from:off else (* The entire buffer isn't enough to hold this value: expand it. *) expand_and_refill_buffer ~from:off in (0, off, missing_hash) in loop_entries ~buffer_off off missing_hash in refill_buffer ~from:Int63.zero; loop_entries ~buffer_off:0 Int63.zero None let run_or_test ~initial_buffer_size mode config = let iter_pack_entry, finalise, message = match mode with | `Reconstruct_index dest -> let open Index_reconstructor in let v = create ~dest config in (iter_pack_entry v, finalise v, "Reconstructing index") | `Check_index -> let open Index_checker in let v = create config in (iter_pack_entry v, finalise v, "Checking index") | `Check_and_fix_index -> let open Index_check_and_fix in let v = create config in (iter_pack_entry v, finalise v, "Checking and fixing index") in let run_duration = Mtime_clock.counter () in let fm = File_manager.open_ro config |> Errs.raise_if_error in let suffix = File_manager.suffix fm in let total = File_manager.Suffix.end_offset suffix in let stats, missing_hash = let bar = let open Progress.Line.Using_int63 in list [ const message; bytes; elapsed (); bar total; percentage_of total ] in Progress.(with_reporter bar) (fun progress -> ingest_data_file ~initial_buffer_size ~progress ~total suffix iter_pack_entry) in finalise (); File_manager.close fm |> Errs.raise_if_error; let run_duration = Mtime_clock.count run_duration in let store_stats fmt = Fmt.pf fmt "Store statistics:@, @[<v 0>%a@]" Stats.pp stats in match missing_hash with | None -> [%log.app "%a in %a. %t" Fmt.(styled `Green string) "Success" Mtime.Span.pp run_duration store_stats] | Some x -> let msg = match mode with | `Check_index -> "Detected missing entries" | `Check_and_fix_index -> "Detected missing entries and added them to index" | _ -> assert false in [%log.err "%a in %a.@,\ First pack entry missing from index is the %d entry of the pack:@,\ \ %a@,\ %t" Fmt.(styled `Red string) msg Mtime.Span.pp run_duration x.idx_pack pp_binding x.binding store_stats] let run = run_or_test ~initial_buffer_size:1024 let test = run_or_test ~initial_buffer_size:100 end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>