Changeset 3 in code for trunk/lib/header_pack.ml


Ignore:
Timestamp:
Apr 15, 2022, 1:17:01 PM (3 years ago)
Author:
fox
Message:
  • Removed 'txt init'

Format

  • New B32 ID

Index

  • New option: txt index --print
  • Move scheme to peers
  • Replace peer.*.conf files with index packed locations Instead of adding a URL to peers.*.conf, run txt pull <url>

Conversion

  • Rewritten converters
  • txt-convert looks for a .convert.conf containing key: value lines.
  • Specifiable topic-roots from .convert.conf.
  • Added Topics: key, with comma seperated topics.

If set only those topics will appear in the main index and used as topic roots.
Other topics will have sub-indices generated, but won't be listed in the main index.

  • HTML converter header & footer options
  • HTML-index renamed to HTM-index

Internal

  • Change types: uuid:Uuid -> id:string
  • File_store merges identical texts
  • Use peer ID for store path, store peers' texts in .local/share/texts
  • Simple URN resolution for converter

Continue to next feed if parsing one fails

  • Phasing-out Archive, replaced by improved packs
  • Eliminate Bos, Cohttp, lwt, uri, tls, Re, Ptime, dependencies
  • Lock version for Cmdliner, fix dune-project
  • Optional resursive store
  • Improve header_pack
  • Fix recursive mkdir
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/header_pack.ml

    r2 r3  
    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 }
     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 }
    44
    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)
     5let of_id id = Msgpck.of_string id
     6let to_id = Msgpck.to_string
    77
    88let person p = Msgpck.String (Person.to_string p)
    9 let persons ps = List.rev @@ Person.Set.fold (fun p a -> person p :: a) ps []
     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)
    1014
    1115let of_set field t =
    1216        List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) []
    1317
    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))
     18let date = function "" -> Int32.zero | date -> Int32.of_int (Date.to_secs date)
    1919
    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
     20let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
    2521
    2622let fields = Msgpck.(List [String "id"; String "time";  String "title"; String "authors"; String "topics"])
    2723let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
    2824
    29 let to_pack a t =
     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 =
    3036        let open Text in
    3137        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)
     38                of_id t.id; of_uint32 (date (Date.listing t.date));
     39                String t.title; persons t.authors; List (of_set "topics" t)
    3440        ]) :: a
    3541
    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
     42let of_text_list l = Msgpck.List l
    3943
    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"
     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
    4546
    4647let 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
     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
    5255
    5356let list filename = try
     
    6164let contains text = function
    6265        | 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                (match to_id id with
     67                 | "" -> prerr_endline ("Invalid id for " ^ Msgpck.to_string title); false
     68                 | id -> text.Text.id = id)
    6669        | _ -> prerr_endline ("Invalid record pattern"); false
    6770
    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 ())])
    7371
    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])
     72(*let pack_filename ?(filename="index.pck") archive =*)
     73(*      let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*)*)
     74(*      dir ^ "/" ^ filename*)
    8375
    84 let unpublish _archive _records = ()
     76(*let add archive records =*)
     77(*      let fname = pack_filename archive in*)
     78(*      let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in*)
     79(*      match list fname with Error e -> prerr_endline e | Ok published_list ->*)
     80(*              let header_pack = List.fold_left append published_list records in*)
     81(*              let archive = Msgpck.(List [*)
     82(*                      Int 0; String archive.File_store.name; persons archive.people]) in*)
     83(*              File_store.file fname @@ Bytes.to_string*)
     84(*                      @@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack])*)
Note: See TracChangeset for help on using the changeset viewer.