[3] | 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 }
|
---|
[2] | 4 |
|
---|
[3] | 5 | let of_id id = Msgpck.of_string id
|
---|
| 6 | let to_id = Msgpck.to_string
|
---|
[2] | 7 |
|
---|
| 8 | let person p = Msgpck.String (Person.to_string p)
|
---|
[3] | 9 | let persons ps = Msgpck.of_list @@ List.rev @@ Person.Set.fold (fun p a -> person p :: a) ps []
|
---|
[2] | 10 |
|
---|
[3] | 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)
|
---|
| 14 |
|
---|
[2] | 15 | let of_set field t =
|
---|
| 16 | List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) []
|
---|
| 17 |
|
---|
[3] | 18 | let date = function "" -> Int32.zero | date -> Int32.of_int (Date.to_secs date)
|
---|
[2] | 19 |
|
---|
[3] | 20 | let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
|
---|
[2] | 21 |
|
---|
| 22 | let fields = Msgpck.(List [String "id"; String "time"; String "title"; String "authors"; String "topics"])
|
---|
| 23 | let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
|
---|
| 24 |
|
---|
[3] | 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 =
|
---|
[2] | 36 | let open Text in
|
---|
| 37 | Msgpck.(List [
|
---|
[3] | 38 | of_id t.id; of_uint32 (date (Date.listing t.date));
|
---|
| 39 | String t.title; persons t.authors; List (of_set "topics" t)
|
---|
[2] | 40 | ]) :: a
|
---|
| 41 |
|
---|
[3] | 42 | let of_text_list l = Msgpck.List l
|
---|
[2] | 43 |
|
---|
[3] | 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
|
---|
[2] | 46 |
|
---|
| 47 | let unpack = function
|
---|
[3] | 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"
|
---|
[2] | 53 |
|
---|
[3] | 54 | let of_string s = unpack @@ snd @@ Msgpck.StringBuf.read s
|
---|
| 55 |
|
---|
[22] | 56 | let of_kv kv =
|
---|
| 57 | let find k kv = try Store.KV.find k kv with Not_found -> "" in
|
---|
| 58 | let find_ls k kv = try String_set.list_of_csv (Store.KV.find k kv) with Not_found -> [] in
|
---|
| 59 | {
|
---|
| 60 | info = { version = version; id = find "Id" kv; title = find "Title" kv;
|
---|
| 61 | people = find_ls "Authors" kv; locations = find_ls "Locations" kv };
|
---|
| 62 | fields;
|
---|
| 63 | texts = Msgpck.List [];
|
---|
| 64 | peers = str_list (find_ls "Peers" kv);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[2] | 67 | let list filename = try
|
---|
| 68 | let texts_list = function
|
---|
| 69 | | Msgpck.List (_info :: _fields :: [texts]) -> Msgpck.to_list texts
|
---|
| 70 | | _ -> prerr_endline "malformed feed"; [] in
|
---|
| 71 | let _pos, data = Msgpck.StringBuf.read @@ File_store.to_string filename in
|
---|
| 72 | Ok (texts_list data)
|
---|
| 73 | with Not_found -> Error "unspecified export dir"
|
---|
| 74 |
|
---|
| 75 | let contains text = function
|
---|
| 76 | | Msgpck.List (id::_time::title::_authors::_topics::[]) ->
|
---|
[3] | 77 | (match to_id id with
|
---|
| 78 | | "" -> prerr_endline ("Invalid id for " ^ Msgpck.to_string title); false
|
---|
| 79 | | id -> text.Text.id = id)
|
---|
[2] | 80 | | _ -> prerr_endline ("Invalid record pattern"); false
|
---|
| 81 |
|
---|
[7] | 82 | let numof_texts pack = List.length (Msgpck.to_list pack.texts)
|
---|
[2] | 83 |
|
---|
[7] | 84 | let iteri fn pack =
|
---|
| 85 | let of_pck i = function Msgpck.List (id::time::title::authors::topics::[]) ->
|
---|
| 86 | let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
|
---|
| 87 | | x -> Msgpck.to_uint32 x in
|
---|
| 88 | let id = to_id id in
|
---|
| 89 | let title = Msgpck.to_string title in
|
---|
| 90 | let topics = to_str_list topics in
|
---|
| 91 | let authors = to_str_list authors in
|
---|
| 92 | fn i id t title authors topics
|
---|
| 93 | | _ -> prerr_endline ("\n\nInvalid record structure\n\n")
|
---|
| 94 | in List.iteri of_pck (Msgpck.to_list pack.texts);
|
---|
| 95 |
|
---|
[3] | 96 | (*let pack_filename ?(filename="index.pck") archive =*)
|
---|
| 97 | (* let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*)*)
|
---|
| 98 | (* dir ^ "/" ^ filename*)
|
---|
[2] | 99 |
|
---|
[3] | 100 | (*let add archive records =*)
|
---|
| 101 | (* let fname = pack_filename archive in*)
|
---|
| 102 | (* let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in*)
|
---|
| 103 | (* match list fname with Error e -> prerr_endline e | Ok published_list ->*)
|
---|
| 104 | (* let header_pack = List.fold_left append published_list records in*)
|
---|
| 105 | (* let archive = Msgpck.(List [*)
|
---|
| 106 | (* Int 0; String archive.File_store.name; persons archive.people]) in*)
|
---|
| 107 | (* File_store.file fname @@ Bytes.to_string*)
|
---|
| 108 | (* @@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack])*)
|
---|