1 | type info_t = { version: int; name: string; archivists: string list }
|
---|
2 | type text_t = { id: Msgpck.t; time: Msgpck.t; title: Msgpck.t; authors: Msgpck.t }
|
---|
3 | type t = { info: info_t; fields: string list; texts: Msgpck.t; peers: Msgpck.t }
|
---|
4 |
|
---|
5 | let of_id id = Msgpck.Bytes (Id.to_bytes id)
|
---|
6 | let to_id pck_id = Id.of_bytes Msgpck.(to_bytes pck_id)
|
---|
7 |
|
---|
8 | let person p = Msgpck.String (Person.to_string p)
|
---|
9 | let persons ps = List.rev @@ Person.Set.fold (fun p a -> person p :: a) ps []
|
---|
10 |
|
---|
11 | let of_set field t =
|
---|
12 | List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) []
|
---|
13 |
|
---|
14 | let date = function
|
---|
15 | | None -> Int32.zero
|
---|
16 | | Some date ->
|
---|
17 | let days, ps = Ptime.Span.to_d_ps (Ptime.to_span date) in
|
---|
18 | Int32.add Int32.(mul (of_int days) 86400l) Int64.(to_int32 (div ps 1000000000000L))
|
---|
19 |
|
---|
20 | let to_sec = function
|
---|
21 | Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
|
---|
22 |
|
---|
23 | let public_peers () =
|
---|
24 | Peers.fold_file (fun x a -> Msgpck.String x :: a) [] Peers.public_fname
|
---|
25 |
|
---|
26 | let fields = Msgpck.(List [String "id"; String "time"; String "title"; String "authors"; String "topics"])
|
---|
27 | let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
|
---|
28 |
|
---|
29 | let to_pack a t =
|
---|
30 | let open Text in
|
---|
31 | Msgpck.(List [
|
---|
32 | Bytes (Id.to_bytes t.uuid); of_uint32 (date (Date.listing t.date));
|
---|
33 | String t.title; List (persons t.authors); List (of_set "topics" t)
|
---|
34 | ]) :: a
|
---|
35 |
|
---|
36 | let pack_filename ?(filename="index.pck") archive =
|
---|
37 | let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*)
|
---|
38 | dir ^ "/" ^ filename
|
---|
39 |
|
---|
40 | let to_info = function
|
---|
41 | | Msgpck.List (v::n::a::[]) ->
|
---|
42 | let archivists = List.map Msgpck.to_string (Msgpck.to_list a) in
|
---|
43 | Msgpck.({version = to_int v; name = to_string n; archivists})
|
---|
44 | | _ -> invalid_arg "Pack header"
|
---|
45 |
|
---|
46 | let unpack = function
|
---|
47 | | Msgpck.List (i::f::texts::[]) ->
|
---|
48 | Some { info = to_info i; fields = to_fields f; texts; peers = Msgpck.List [] }
|
---|
49 | | Msgpck.List (i::f::texts::peers::[]) ->
|
---|
50 | Some { info = to_info i; fields = to_fields f; texts; peers }
|
---|
51 | | _ -> None
|
---|
52 |
|
---|
53 | let list filename = try
|
---|
54 | let texts_list = function
|
---|
55 | | Msgpck.List (_info :: _fields :: [texts]) -> Msgpck.to_list texts
|
---|
56 | | _ -> prerr_endline "malformed feed"; [] in
|
---|
57 | let _pos, data = Msgpck.StringBuf.read @@ File_store.to_string filename in
|
---|
58 | Ok (texts_list data)
|
---|
59 | with Not_found -> Error "unspecified export dir"
|
---|
60 |
|
---|
61 | let contains text = function
|
---|
62 | | Msgpck.List (id::_time::title::_authors::_topics::[]) ->
|
---|
63 | (match Id.of_bytes (Msgpck.to_bytes id) with
|
---|
64 | | None -> prerr_endline ("Invalid id for " ^ Msgpck.to_string title); false
|
---|
65 | | Some id -> text.Text.uuid = id)
|
---|
66 | | _ -> prerr_endline ("Invalid record pattern"); false
|
---|
67 |
|
---|
68 | let pack archive records =
|
---|
69 | let header_pack = List.fold_left to_pack [] records in
|
---|
70 | let info = Msgpck.(List [Int 0; String archive.File_store.name; List (persons archive.archivists)]) in
|
---|
71 | Bytes.to_string @@ Msgpck.Bytes.to_string
|
---|
72 | (List [info; fields; Msgpck.List header_pack; Msgpck.List (public_peers ())])
|
---|
73 |
|
---|
74 | let add archive records =
|
---|
75 | let fname = pack_filename archive in
|
---|
76 | let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in
|
---|
77 | match list fname with Error e -> prerr_endline e | Ok published_list ->
|
---|
78 | let header_pack = List.fold_left append published_list records in
|
---|
79 | let archive = Msgpck.(List [Int 0; String archive.File_store.name;
|
---|
80 | List (persons archive.archivists)]) in
|
---|
81 | File_store.file fname @@ Bytes.to_string
|
---|
82 | @@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack])
|
---|
83 |
|
---|
84 | let unpublish _archive _records = ()
|
---|