[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
|
---|
[42] | 13 | let to_str_list x = List.map Msgpck.to_string
|
---|
| 14 | (try Msgpck.to_list x with e -> prerr_endline "to_str_list"; raise e)
|
---|
[3] | 15 |
|
---|
[2] | 16 | let of_set field t =
|
---|
| 17 | List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) []
|
---|
| 18 |
|
---|
[3] | 19 | let date = function "" -> Int32.zero | date -> Int32.of_int (Date.to_secs date)
|
---|
[2] | 20 |
|
---|
[3] | 21 | let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
|
---|
[2] | 22 |
|
---|
[42] | 23 | let fields = Msgpck.(List [
|
---|
| 24 | String "id"; String "time"; String "title"; String "authors"; String "topics";
|
---|
| 25 | String "references"; String "replies";
|
---|
| 26 | ])
|
---|
[2] | 27 | let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
|
---|
| 28 |
|
---|
[3] | 29 | let to_info = function
|
---|
| 30 | | Msgpck.List (v::id::n::a::ls::[]) ->
|
---|
| 31 | let people = to_str_list a in
|
---|
| 32 | let locations = to_str_list ls in
|
---|
| 33 | Msgpck.({version = to_int v; id = to_string id; title = to_string n; people; locations})
|
---|
| 34 | | _ -> invalid_arg "Pack header"
|
---|
| 35 |
|
---|
| 36 | let of_info i = let open Msgpck in
|
---|
| 37 | List [Int i.version; String i.id; String i.title; str_list i.people; str_list i.locations]
|
---|
| 38 |
|
---|
| 39 | let of_text a t =
|
---|
[2] | 40 | let open Text in
|
---|
| 41 | Msgpck.(List [
|
---|
[42] | 42 | of_id t.id;
|
---|
| 43 | of_uint32 (date (Date.listing t.date));
|
---|
| 44 | String t.title;
|
---|
| 45 | persons t.authors;
|
---|
| 46 | List (of_set "topics" t);
|
---|
| 47 | List (of_set "references" t);
|
---|
| 48 | List (of_set "in-reply-to" t);
|
---|
[2] | 49 | ]) :: a
|
---|
| 50 |
|
---|
[3] | 51 | let of_text_list l = Msgpck.List l
|
---|
[2] | 52 |
|
---|
[3] | 53 | let pack p = Msgpck.List [of_info p.info; p.fields; p.texts; p.peers]
|
---|
| 54 | let string p = Bytes.to_string @@ Msgpck.Bytes.to_string @@ pack p
|
---|
[2] | 55 |
|
---|
| 56 | let unpack = function
|
---|
[3] | 57 | | Msgpck.List (i::fields::texts::[]) ->
|
---|
| 58 | Ok { info = to_info i; fields; texts; peers = Msgpck.List [] }
|
---|
| 59 | | Msgpck.List (i::fields::texts::peers::[]) ->
|
---|
| 60 | Ok { info = to_info i; fields; texts; peers }
|
---|
| 61 | | _ -> Error "format mismatch"
|
---|
[2] | 62 |
|
---|
[3] | 63 | let of_string s = unpack @@ snd @@ Msgpck.StringBuf.read s
|
---|
| 64 |
|
---|
[22] | 65 | let of_kv kv =
|
---|
| 66 | let find k kv = try Store.KV.find k kv with Not_found -> "" in
|
---|
| 67 | let find_ls k kv = try String_set.list_of_csv (Store.KV.find k kv) with Not_found -> [] in
|
---|
| 68 | {
|
---|
| 69 | info = { version = version; id = find "Id" kv; title = find "Title" kv;
|
---|
| 70 | people = find_ls "Authors" kv; locations = find_ls "Locations" kv };
|
---|
| 71 | fields;
|
---|
| 72 | texts = Msgpck.List [];
|
---|
| 73 | peers = str_list (find_ls "Peers" kv);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[2] | 76 | let list filename = try
|
---|
| 77 | let texts_list = function
|
---|
| 78 | | Msgpck.List (_info :: _fields :: [texts]) -> Msgpck.to_list texts
|
---|
| 79 | | _ -> prerr_endline "malformed feed"; [] in
|
---|
| 80 | let _pos, data = Msgpck.StringBuf.read @@ File_store.to_string filename in
|
---|
| 81 | Ok (texts_list data)
|
---|
| 82 | with Not_found -> Error "unspecified export dir"
|
---|
| 83 |
|
---|
| 84 | let contains text = function
|
---|
| 85 | | Msgpck.List (id::_time::title::_authors::_topics::[]) ->
|
---|
[3] | 86 | (match to_id id with
|
---|
[43] | 87 | | "" -> Printf.eprintf "Invalid id for %s" (Msgpck.to_string title); false
|
---|
[3] | 88 | | id -> text.Text.id = id)
|
---|
[2] | 89 | | _ -> prerr_endline ("Invalid record pattern"); false
|
---|
| 90 |
|
---|
[7] | 91 | let numof_texts pack = List.length (Msgpck.to_list pack.texts)
|
---|
[2] | 92 |
|
---|
[42] | 93 | let txt_iter_apply fn i = function
|
---|
| 94 | | Msgpck.List (id::time::title::authors::topics::extra) ->
|
---|
[7] | 95 | let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
|
---|
| 96 | | x -> Msgpck.to_uint32 x in
|
---|
| 97 | let id = to_id id in
|
---|
| 98 | let title = Msgpck.to_string title in
|
---|
| 99 | let topics = to_str_list topics in
|
---|
| 100 | let authors = to_str_list authors in
|
---|
[42] | 101 | let references, replies =
|
---|
| 102 | try begin match extra with [] -> [], []
|
---|
| 103 | | refs::[] -> to_str_list refs, []
|
---|
| 104 | | refs::replies::_xs -> to_str_list refs, to_str_list replies
|
---|
| 105 | end with e -> prerr_endline "iter ref reps"; raise e
|
---|
| 106 | in
|
---|
| 107 | fn i id t title authors topics references replies
|
---|
[43] | 108 | | x -> Printf.eprintf "Invalid record structure: %s\n%!" (Msgpck.show x)
|
---|
[7] | 109 |
|
---|
[43] | 110 | let txt_fold_apply fn i = function
|
---|
[42] | 111 | | Msgpck.List (id::time::title::authors::topics::extra) ->
|
---|
| 112 | let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
|
---|
| 113 | | x -> Msgpck.to_uint32 x in
|
---|
| 114 | let id = to_id id in
|
---|
| 115 | let title = Msgpck.to_string title in
|
---|
[43] | 116 | let topics = to_str_list topics in
|
---|
| 117 | let authors = to_str_list authors in
|
---|
[42] | 118 | let references, replies = begin match extra with
|
---|
| 119 | | [] -> [], []
|
---|
[43] | 120 | | refs::[] -> to_str_list refs, []
|
---|
[42] | 121 | | refs::replies::_xs -> to_str_list refs, to_str_list replies
|
---|
| 122 | end
|
---|
| 123 | in
|
---|
| 124 | fn i id t title authors topics references replies
|
---|
[43] | 125 | | x -> Printf.eprintf "Invalid record structure: %s\n%!" (Msgpck.show x); i
|
---|
[2] | 126 |
|
---|
[43] | 127 | let iteri fn pack = List.iteri
|
---|
| 128 | (txt_iter_apply fn)
|
---|
| 129 | (Msgpck.to_list pack.texts)
|
---|
| 130 |
|
---|
| 131 | let fold fn init pack = List.fold_left
|
---|
| 132 | (fun acc m -> try txt_fold_apply fn acc m with Invalid_argument x -> prerr_endline x; acc) init
|
---|
| 133 | (try Msgpck.to_list pack.texts with e -> prerr_string "Invalid pack.texts"; raise e)
|
---|