Changeset 42 in code for trunk/lib


Ignore:
Timestamp:
Dec 15, 2022, 9:25:18 PM (2 years ago)
Author:
fox
Message:

Preliminary support for cross-domain references

Location:
trunk/lib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/file_store.ml

    r32 r42  
    7070        let expand_dir d = Array.(to_list @@ map (Filename.concat d) (Sys.readdir d)) in
    7171        let rec loop result = function
    72                 | f::fs when valid_dir f -> expand_dir f |> List.append fs |> loop result
     72                | f::fs when valid_dir f -> prerr_endline f; expand_dir f |> List.append fs |> loop result
    7373                | f::fs -> loop (f::result) fs
    7474                | [] -> result in
  • trunk/lib/header_pack.ml

    r22 r42  
    1111let str = Msgpck.of_string
    1212let str_list ls = Msgpck.of_list @@ List.map str ls
    13 let to_str_list x = List.map Msgpck.to_string (Msgpck.to_list x)
     13let to_str_list x = List.map Msgpck.to_string
     14        (try Msgpck.to_list x with e -> prerr_endline "to_str_list"; raise e)
    1415
    1516let of_set field t =
     
    2021let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
    2122
    22 let fields = Msgpck.(List [String "id"; String "time";  String "title"; String "authors"; String "topics"])
     23let fields = Msgpck.(List [
     24        String "id"; String "time";  String "title"; String "authors"; String "topics";
     25        String "references"; String "replies";
     26        ])
    2327let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
    2428
     
    3640        let open Text in
    3741        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)
     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);
    4049        ]) :: a
    4150
     
    8291let numof_texts pack = List.length (Msgpck.to_list pack.texts)
    8392
    84 let iteri fn pack =
    85         let of_pck i = function Msgpck.List (id::time::title::authors::topics::[]) ->
     93let txt_iter_apply fn i = function
     94        | Msgpck.List (id::time::title::authors::topics::extra) ->
    8695                let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
    8796                        | x -> Msgpck.to_uint32 x in
     
    9099                let topics = to_str_list topics in
    91100                let authors = to_str_list authors in
    92                 fn i id t title authors topics
     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
    93108        | _  -> prerr_endline ("\n\nInvalid record structure\n\n")
    94         in List.iteri of_pck (Msgpck.to_list pack.texts);
    95109
    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*)
     110let txt_fold_apply fn i m =
     111(*      Printf.eprintf "%s\n%!" @@ Msgpck.show m;*)
     112        match m with
     113        | Msgpck.List (id::time::title::authors::topics::extra) ->
     114                let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
     115                        | x -> Msgpck.to_uint32 x in
     116                let id = to_id id in
     117                let title = Msgpck.to_string title in
     118                let topics = try to_str_list topics with _e -> Printf.eprintf "topics %s" title; [] in
     119                let authors = try to_str_list authors with _e -> Printf.eprintf "authors %s" title; [] in
     120                let references, replies = begin match extra with
     121                        | [] -> [], []
     122                        | refs::[] -> (try to_str_list refs, [] with e -> prerr_endline "fold ref"; raise e)
     123                        | refs::replies::_xs -> to_str_list refs, to_str_list replies
     124                        end
     125                in
     126                fn i id t title authors topics references replies
     127        | x  -> Printf.eprintf "Invalid record structure: %s\n%!" (Msgpck.show x); i
    99128
    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])*)
     129let iteri fn pack = List.iteri (txt_iter_apply fn) (Msgpck.to_list pack.texts)
     130let fold fn init pack = List.fold_left (txt_fold_apply fn) init
     131        (try Msgpck.to_list pack.texts with e -> prerr_string "Pack.fold"; raise e)
  • trunk/lib/peers.ml

    r31 r42  
    11let text_dir = Filename.concat (File_store.txtdir ()) "peers"
    22
    3 type t = { path: string; locations: string list }
     3type t = { path: string; pack: Header_pack.t }
    44
    55let fold fn init = match Sys.readdir text_dir with
     
    1414                        | true -> match Header_pack.of_string (File_store.to_string pack_path) with
    1515                                | Error s -> Printf.eprintf "%s %s\n" s pack_path; init
    16                                 | Ok p -> fn init { path; locations = Header_pack.(p.info.locations) }
     16                                | Ok pack -> fn init { path; pack }
    1717                        end else init
    1818                in
Note: See TracChangeset for help on using the changeset viewer.