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

Last change on this file since 33 was 22, checked in by fox, 2 years ago

Use txt.conf to generate index.pck meta; fix double load while indexing

File size: 4.3 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 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
67let 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
75let contains text = function
76 | Msgpck.List (id::_time::title::_authors::_topics::[]) ->
77 (match to_id id with
78 | "" -> prerr_endline ("Invalid id for " ^ Msgpck.to_string title); false
79 | id -> text.Text.id = id)
80 | _ -> prerr_endline ("Invalid record pattern"); false
81
82let numof_texts pack = List.length (Msgpck.to_list pack.texts)
83
84let 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
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*)
99
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])*)
Note: See TracBrowser for help on using the repository browser.