source: code/trunk/lib/header_pack.ml@ 8

Last change on this file since 8 was 7, checked in by fox, 3 years ago

Implement author & topic filter for txt pull

File size: 3.9 KB
Line 
1let version = 0
2type info_t = { version: int; id: string; title: string; people: string list; locations: string list }
3type t = { info: info_t; fields: Msgpck.t; texts: Msgpck.t; peers: Msgpck.t }
4
5let of_id id = Msgpck.of_string id
6let to_id = Msgpck.to_string
7
8let person p = Msgpck.String (Person.to_string p)
9let persons ps = Msgpck.of_list @@ List.rev @@ Person.Set.fold (fun p a -> person p :: a) ps []
10
11let str = Msgpck.of_string
12let str_list ls = Msgpck.of_list @@ List.map str ls
13let to_str_list x = List.map Msgpck.to_string (Msgpck.to_list x)
14
15let of_set field t =
16 List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) []
17
18let date = function "" -> Int32.zero | date -> Int32.of_int (Date.to_secs date)
19
20let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
21
22let fields = Msgpck.(List [String "id"; String "time"; String "title"; String "authors"; String "topics"])
23let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
24
25let 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
32let 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
35let 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
42let of_text_list l = Msgpck.List l
43
44let pack p = Msgpck.List [of_info p.info; p.fields; p.texts; p.peers]
45let string p = Bytes.to_string @@ Msgpck.Bytes.to_string @@ pack p
46
47let 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
54let of_string s = unpack @@ snd @@ Msgpck.StringBuf.read s
55
56let 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
64let 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
71let numof_texts pack = List.length (Msgpck.to_list pack.texts)
72
73let 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])*)
Note: See TracBrowser for help on using the repository browser.