Preliminary support for cross-domain references

git-svn-id: file:///srv/svn/repo/kosuzu/trunk@42 eb64cd80-c68d-6f47-b6a3-0ada418499da
This commit is contained in:
fox
2022-12-15 21:25:18 +00:00
parent 3e971f40dc
commit 4dff69cb4a
7 changed files with 88 additions and 49 deletions

View File

@@ -69,7 +69,7 @@ let list_fs ?(r=false) dir =
let valid_dir f = r && String.get f 0 <> '.' && Sys.is_directory f in
let expand_dir d = Array.(to_list @@ map (Filename.concat d) (Sys.readdir d)) in
let rec loop result = function
| f::fs when valid_dir f -> expand_dir f |> List.append fs |> loop result
| f::fs when valid_dir f -> prerr_endline f; expand_dir f |> List.append fs |> loop result
| f::fs -> loop (f::result) fs
| [] -> result in
let dirs = if dir = "." then Array.to_list (Sys.readdir dir) else

View File

@@ -10,7 +10,8 @@ let persons ps = Msgpck.of_list @@ List.rev @@ Person.Set.fold (fun p a -> perso
let str = Msgpck.of_string
let str_list ls = Msgpck.of_list @@ List.map str ls
let to_str_list x = List.map Msgpck.to_string (Msgpck.to_list x)
let to_str_list x = List.map Msgpck.to_string
(try Msgpck.to_list x with e -> prerr_endline "to_str_list"; raise e)
let of_set field t =
List.rev @@ String_set.fold (fun s a -> Msgpck.String s :: a) (Text.set field t) []
@@ -19,7 +20,10 @@ let date = function "" -> Int32.zero | date -> Int32.of_int (Date.to_secs date)
let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
let fields = Msgpck.(List [String "id"; String "time"; String "title"; String "authors"; String "topics"])
let fields = Msgpck.(List [
String "id"; String "time"; String "title"; String "authors"; String "topics";
String "references"; String "replies";
])
let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
let to_info = function
@@ -35,8 +39,13 @@ let of_info i = let open Msgpck in
let of_text a t =
let open Text in
Msgpck.(List [
of_id t.id; of_uint32 (date (Date.listing t.date));
String t.title; persons t.authors; List (of_set "topics" t)
of_id t.id;
of_uint32 (date (Date.listing t.date));
String t.title;
persons t.authors;
List (of_set "topics" t);
List (of_set "references" t);
List (of_set "in-reply-to" t);
]) :: a
let of_text_list l = Msgpck.List l
@@ -81,28 +90,42 @@ let contains text = function
let numof_texts pack = List.length (Msgpck.to_list pack.texts)
let iteri fn pack =
let of_pck i = function Msgpck.List (id::time::title::authors::topics::[]) ->
let txt_iter_apply fn i = function
| Msgpck.List (id::time::title::authors::topics::extra) ->
let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
| x -> Msgpck.to_uint32 x in
let id = to_id id in
let title = Msgpck.to_string title in
let topics = to_str_list topics in
let authors = to_str_list authors in
fn i id t title authors topics
let references, replies =
try begin match extra with [] -> [], []
| refs::[] -> to_str_list refs, []
| refs::replies::_xs -> to_str_list refs, to_str_list replies
end with e -> prerr_endline "iter ref reps"; raise e
in
fn i id t title authors topics references replies
| _ -> prerr_endline ("\n\nInvalid record structure\n\n")
in List.iteri of_pck (Msgpck.to_list pack.texts);
(*let pack_filename ?(filename="index.pck") archive =*)
(* let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*)*)
(* dir ^ "/" ^ filename*)
let txt_fold_apply fn i m =
(* Printf.eprintf "%s\n%!" @@ Msgpck.show m;*)
match m with
| Msgpck.List (id::time::title::authors::topics::extra) ->
let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
| x -> Msgpck.to_uint32 x in
let id = to_id id in
let title = Msgpck.to_string title in
let topics = try to_str_list topics with _e -> Printf.eprintf "topics %s" title; [] in
let authors = try to_str_list authors with _e -> Printf.eprintf "authors %s" title; [] in
let references, replies = begin match extra with
| [] -> [], []
| refs::[] -> (try to_str_list refs, [] with e -> prerr_endline "fold ref"; raise e)
| refs::replies::_xs -> to_str_list refs, to_str_list replies
end
in
fn i id t title authors topics references replies
| x -> Printf.eprintf "Invalid record structure: %s\n%!" (Msgpck.show x); i
(*let add archive records =*)
(* let fname = pack_filename archive in*)
(* let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in*)
(* match list fname with Error e -> prerr_endline e | Ok published_list ->*)
(* let header_pack = List.fold_left append published_list records in*)
(* let archive = Msgpck.(List [*)
(* Int 0; String archive.File_store.name; persons archive.people]) in*)
(* File_store.file fname @@ Bytes.to_string*)
(* @@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack])*)
let iteri fn pack = List.iteri (txt_iter_apply fn) (Msgpck.to_list pack.texts)
let fold fn init pack = List.fold_left (txt_fold_apply fn) init
(try Msgpck.to_list pack.texts with e -> prerr_string "Pack.fold"; raise e)

View File

@@ -1,6 +1,6 @@
let text_dir = Filename.concat (File_store.txtdir ()) "peers"
type t = { path: string; locations: string list }
type t = { path: string; pack: Header_pack.t }
let fold fn init = match Sys.readdir text_dir with
| exception (Sys_error msg) -> prerr_endline msg; init
@@ -13,7 +13,7 @@ let fold fn init = match Sys.readdir text_dir with
| false -> Printf.eprintf "Missing index.pck for %s\n" path; init
| true -> match Header_pack.of_string (File_store.to_string pack_path) with
| Error s -> Printf.eprintf "%s %s\n" s pack_path; init
| Ok p -> fn init { path; locations = Header_pack.(p.info.locations) }
| Ok pack -> fn init { path; pack }
end else init
in
Array.fold_left read_pack init dirs