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