Changeset 3 in code for trunk/lib/header_pack.ml
- Timestamp:
- Apr 15, 2022, 1:17:01 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/header_pack.ml
r2 r3 1 type info_t = { version: int; name: string; archivists: string list } 2 type text_t = { id: Msgpck.t; time: Msgpck.t; title: Msgpck.t; authors: Msgpck.t }3 type t = { info: info_t; fields: string list; texts: Msgpck.t; peers: Msgpck.t }1 let version = 0 2 type info_t = { version: int; id: string; title: string; people: string list; locations: string list } 3 type t = { info: info_t; fields: Msgpck.t; texts: Msgpck.t; peers: Msgpck.t } 4 4 5 let of_id id = Msgpck. Bytes (Id.to_bytes id)6 let to_id pck_id = Id.of_bytes Msgpck.(to_bytes pck_id)5 let of_id id = Msgpck.of_string id 6 let to_id = Msgpck.to_string 7 7 8 8 let person p = Msgpck.String (Person.to_string p) 9 let persons ps = List.rev @@ Person.Set.fold (fun p a -> person p :: a) ps [] 9 let persons ps = Msgpck.of_list @@ List.rev @@ Person.Set.fold (fun p a -> person p :: a) ps [] 10 11 let str = Msgpck.of_string 12 let str_list ls = Msgpck.of_list @@ List.map str ls 13 let to_str_list x = List.map Msgpck.to_string (Msgpck.to_list x) 10 14 11 15 let of_set field t = 12 16 List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) [] 13 17 14 let date = function 15 | None -> Int32.zero 16 | Some date -> 17 let days, ps = Ptime.Span.to_d_ps (Ptime.to_span date) in 18 Int32.add Int32.(mul (of_int days) 86400l) Int64.(to_int32 (div ps 1000000000000L)) 18 let date = function "" -> Int32.zero | date -> Int32.of_int (Date.to_secs date) 19 19 20 let to_sec = function 21 Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x 22 23 let public_peers () = 24 Peers.fold_file (fun x a -> Msgpck.String x :: a) [] Peers.public_fname 20 let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x 25 21 26 22 let fields = Msgpck.(List [String "id"; String "time"; String "title"; String "authors"; String "topics"]) 27 23 let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack) 28 24 29 let to_pack a t = 25 let to_info = function 26 | Msgpck.List (v::id::n::a::ls::[]) -> 27 let people = to_str_list a in 28 let locations = to_str_list ls in 29 Msgpck.({version = to_int v; id = to_string id; title = to_string n; people; locations}) 30 | _ -> invalid_arg "Pack header" 31 32 let of_info i = let open Msgpck in 33 List [Int i.version; String i.id; String i.title; str_list i.people; str_list i.locations] 34 35 let of_text a t = 30 36 let open Text in 31 37 Msgpck.(List [ 32 Bytes (Id.to_bytes t.uuid); of_uint32 (date (Date.listing t.date));33 String t.title; List (persons t.authors); List (of_set "topics" t)38 of_id t.id; of_uint32 (date (Date.listing t.date)); 39 String t.title; persons t.authors; List (of_set "topics" t) 34 40 ]) :: a 35 41 36 let pack_filename ?(filename="index.pck") archive = 37 let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*) 38 dir ^ "/" ^ filename 42 let of_text_list l = Msgpck.List l 39 43 40 let to_info = function 41 | Msgpck.List (v::n::a::[]) -> 42 let archivists = List.map Msgpck.to_string (Msgpck.to_list a) in 43 Msgpck.({version = to_int v; name = to_string n; archivists}) 44 | _ -> invalid_arg "Pack header" 44 let pack p = Msgpck.List [of_info p.info; p.fields; p.texts; p.peers] 45 let string p = Bytes.to_string @@ Msgpck.Bytes.to_string @@ pack p 45 46 46 47 let unpack = function 47 | Msgpck.List (i::f::texts::[]) -> 48 Some { info = to_info i; fields = to_fields f; texts; peers = Msgpck.List [] } 49 | Msgpck.List (i::f::texts::peers::[]) -> 50 Some { info = to_info i; fields = to_fields f; texts; peers } 51 | _ -> None 48 | Msgpck.List (i::fields::texts::[]) -> 49 Ok { info = to_info i; fields; texts; peers = Msgpck.List [] } 50 | Msgpck.List (i::fields::texts::peers::[]) -> 51 Ok { info = to_info i; fields; texts; peers } 52 | _ -> Error "format mismatch" 53 54 let of_string s = unpack @@ snd @@ Msgpck.StringBuf.read s 52 55 53 56 let list filename = try … … 61 64 let contains text = function 62 65 | Msgpck.List (id::_time::title::_authors::_topics::[]) -> 63 (match Id.of_bytes (Msgpck.to_bytes id)with64 | None-> prerr_endline ("Invalid id for " ^ Msgpck.to_string title); false65 | Some id -> text.Text.uuid = id)66 (match to_id id with 67 | "" -> prerr_endline ("Invalid id for " ^ Msgpck.to_string title); false 68 | id -> text.Text.id = id) 66 69 | _ -> prerr_endline ("Invalid record pattern"); false 67 70 68 let pack archive records =69 let header_pack = List.fold_left to_pack [] records in70 let info = Msgpck.(List [Int 0; String archive.File_store.name; List (persons archive.archivists)]) in71 Bytes.to_string @@ Msgpck.Bytes.to_string72 (List [info; fields; Msgpck.List header_pack; Msgpck.List (public_peers ())])73 71 74 let add archive records = 75 let fname = pack_filename archive in 76 let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in 77 match list fname with Error e -> prerr_endline e | Ok published_list -> 78 let header_pack = List.fold_left append published_list records in 79 let archive = Msgpck.(List [Int 0; String archive.File_store.name; 80 List (persons archive.archivists)]) in 81 File_store.file fname @@ Bytes.to_string 82 @@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack]) 72 (*let pack_filename ?(filename="index.pck") archive =*) 73 (* let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*)*) 74 (* dir ^ "/" ^ filename*) 83 75 84 let unpublish _archive _records = () 76 (*let add archive records =*) 77 (* let fname = pack_filename archive in*) 78 (* let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in*) 79 (* match list fname with Error e -> prerr_endline e | Ok published_list ->*) 80 (* let header_pack = List.fold_left append published_list records in*) 81 (* let archive = Msgpck.(List [*) 82 (* Int 0; String archive.File_store.name; persons archive.people]) in*) 83 (* File_store.file fname @@ Bytes.to_string*) 84 (* @@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack])*)
Note:
See TracChangeset
for help on using the changeset viewer.