Changeset 42 in code for trunk


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

Preliminary support for cross-domain references

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/convert.ml

    r41 r42  
    2727        t
    2828
    29 let acc_ref id t a =
    30         Conversion.Id_map.update t (function
    31         | Some s -> Some (Conversion.Ref_set.add id s)
    32         | None -> Some (Conversion.Ref_set.singleton id)
    33         ) a
     29let acc_rel source target a =
     30        prerr_endline source;
     31        Conversion.Id_map.update target
     32        (function Some set -> Some (Conversion.Ref_set.add source set)
     33                | None -> Some (Conversion.Ref_set.singleton source))
     34        a
    3435
    35 let fold_refs text refs = String_set.fold (acc_ref text.Text.id) (Text.set "references" text) refs
    36 let fold_reps text reps = String_set.fold (acc_ref text.Text.id) (Text.set "in-reply-to" text) reps
     36let empty_rels () = Conversion.Id_map.empty, Conversion.Id_map.empty
     37
     38let acc_txt_refs text refs = String_set.fold (acc_rel text.Text.id) (Text.set "references" text) refs
     39let acc_txt_reps text reps = String_set.fold (acc_rel text.Text.id) (Text.set "in-reply-to" text) reps
     40let acc_txt_rels (refs, reps) (elt, _paths) =
     41        acc_txt_refs elt refs, acc_txt_reps elt reps
     42
     43let acc_pck_refs id refs_ls refs = String_set.fold (acc_rel id) (String_set.of_list refs_ls) refs
     44let acc_pck_reps id reps_ls reps = String_set.fold (acc_rel id) (String_set.of_list reps_ls) reps
     45let acc_pck_rels refs_reps peer =
     46        let path = try List.hd peer.Peers.pack.Header_pack.info.locations with Failure _ -> "" in
     47        try Header_pack.fold
     48                (fun (refs, reps) id _t _title _authors _topics refs_ls reps_ls ->
     49                        let id = Filename.concat path id in
     50                        acc_pck_refs id refs_ls refs, acc_pck_reps id reps_ls reps)
     51                refs_reps peer.Peers.pack
     52        with e -> prerr_endline "acc_pck_rels"; raise e
    3753
    3854let directory converters noindex repo =
    39         let fn (ts,refs,reps,ls,acc) ((elt,_) as r) =
    40                 Topic_set.to_map ts (Text.set "topics" elt),
    41                 fold_refs elt refs, fold_reps elt reps,
    42                 elt::ls,
    43                 if convert converters {repo with references = refs; replies = reps} r then acc+1 else acc in
    44         let topics, references, replies, texts, count =
    45                 File_store.(fold ~dir:repo.Conversion.dir ~order:oldest fn
    46                         (Topic_set.Map.empty, Conversion.Id_map.empty, Conversion.Id_map.empty, [], 0)) in
     55        let order = File_store.oldest in
     56        let repo =
     57                let references, replies =
     58                        File_store.fold ~dir:repo.Conversion.dir ~order acc_txt_rels (empty_rels ()) in
     59                let references, replies = Peers.fold acc_pck_rels (references, replies) in
     60                Printf.eprintf "%s %d\n" repo.Conversion.dir (Conversion.Id_map.cardinal replies);
     61                { repo with references; replies } in
     62        let acc (ts,ls,acc) ((elt,_) as r) = Topic_set.to_map ts (Text.set "topics" elt), elt::ls,
     63                if convert converters repo r then acc+1 else acc in
     64        let topics, texts, count =
     65                File_store.fold ~dir:repo.Conversion.dir ~order acc (Topic_set.Map.empty, [], 0) in
    4766        let topic_roots = try List.rev @@ String_set.list_of_csv (Store.KV.find "Topics" repo.kv)
    4867                with Not_found -> Topic_set.roots topics in
    49         Printf.eprintf "%d\n" (Conversion.Id_map.cardinal replies);
    50         let repo = Conversion.{ repo with
    51                 topic_roots; topics; references; replies; texts = List.rev texts } in
     68        let repo = Conversion.{ repo with topic_roots; topics; texts = List.rev texts } in
    5269        if not noindex then
    5370                List.iter (fun c -> match c.Conversion.indices with None -> () | Some f -> f repo) converters;
     
    7996                        | Ok text ->
    8097                                let dir = "." in
    81                                 let references, replies = File_store.(fold ~dir ~order:newest
    82                                         (fun (refs, reps) (elt, _) -> fold_refs elt refs, fold_reps elt reps)
    83                                         (Conversion.Id_map.empty, Conversion.Id_map.empty)) in
     98                                let references, replies = File_store.(fold ~dir ~order:newest acc_txt_rels (empty_rels ())) in
    8499                                let repo = { (Conversion.empty ()) with dir; kv = load_kv ""; references; replies } in
    85100                                ignore @@ convert (converters types repo.kv) repo (text, [path])
  • trunk/cli/html.ml

    r41 r42  
    8989                ^ opt_kv "Replies:" (try
    9090                                ref_links (Conversion.Id_map.find text.id conversion.Conversion.replies)
    91                                 with Not_found -> "empty replies")
     91                                with Not_found -> "")
    9292                ^ {|</dl></header><pre style="white-space:pre-wrap">|} in
    9393        wrap conversion htm text.title ((T.of_string text.body header) ^ "</pre></article>")
  • trunk/cli/peers.ml

    r31 r42  
    1010        let open Logarion.Peers in
    1111        Printf.printf "%s" peer.path;
    12         List.iter (Printf.printf "\t%s\n") peer.locations
     12        List.iter (Printf.printf "\t%s\n") peer.pack.info.locations
    1313
    1414let remove_repo id =
  • trunk/cli/pull.ml

    r31 r42  
    7676                        output_string file txt; close_out file
    7777
    78 let per_text url dir filter print i id time title authors topics = match id with
     78let per_text url dir filter print i id time title authors topics _refs _reps = match id with
    7979        | "" -> Printf.eprintf "\nInvalid id for %s\n" title
    8080        | id -> let open Logarion in
     
    128128        let pull got_one peer_url = if got_one then got_one else
    129129                (pull_index peer_url auths topics) in
     130        let open Logarion in
    130131        let fold_locations init peer =
    131                 ignore @@ List.fold_left pull init peer.Logarion.Peers.locations;
     132                ignore @@ List.fold_left pull init peer.Peers.pack.Header_pack.info.locations;
    132133                false
    133134        in
    134         ignore @@ Logarion.Peers.fold fold_locations false;
     135        ignore @@ Peers.fold fold_locations false;
    135136        Curl.global_cleanup ()
    136137
  • 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.