3 module Hashtbl = MoreLabels.Hashtbl
5 module Msg = Khatus_msg
6 module Time = Khatus_time
9 { values : ((string * string * string list), string) Hashtbl.t
10 ; mtimes : ((string * string * string list), Time.t) Hashtbl.t
14 { values = Hashtbl.create 256
15 ; mtimes = Hashtbl.create 256
18 let update {values; mtimes} ~node ~modul ~key ~value:data ~time =
19 let key = (node, modul, key) in
20 Hashtbl.replace values ~key ~data;
21 Hashtbl.replace mtimes ~key ~data:time
23 let dump_to_channel {values; mtimes} ~node ~modul ~oc =
24 Hashtbl.iter values ~f:(fun ~key ~data:value ->
26 match Hashtbl.find_opt mtimes key with
28 | None -> assert false (* Implies update was incorrect *)
30 let (node', modul', key) = key in
35 ; content = Cache {mtime; node = node'; modul = modul'; key; value}
43 let (/) = Filename.concat
46 match Sys.command("mkdir -p " ^ dir) with
50 (sprintf "Failed to create directory: %S. mkdir status: %d\n" dir n)
53 match Sys.command("gzip " ^ path) with
57 (sprintf "Failed to gzip path: %S. gzip status: %d\n" path n)
59 let dump_to_dir t ~time ~node ~modul ~dir =
60 (* TODO: Just log the errors and keep it moving, instead of failing. *)
62 let dump_filename = dir / "khatus-cache-dump.psv.gz" in
63 let tmp_filename = "khatus-cache-dump-" ^ (Time.to_string time) in
64 let oc = open_out tmp_filename in
65 dump_to_channel t ~node ~modul ~oc;
68 Sys.rename (tmp_filename ^ ".gz") dump_filename