Merge branch 'local-ocamlbuild' into local-trunk
[SXSI/xpathcomp.git] / src / tree.ml
diff --git a/src/tree.ml b/src/tree.ml
new file mode 100644 (file)
index 0000000..27ceed6
--- /dev/null
@@ -0,0 +1,618 @@
+(******************************************************************************)
+(*  SXSI : XPath evaluator                                                    *)
+(*  Kim Nguyen (Kim.Nguyen@nicta.com.au)                                      *)
+(*  Copyright NICTA 2008                                                      *)
+(*  Distributed under the terms of the LGPL (see LICENCE)                     *)
+(******************************************************************************)
+INCLUDE "debug.ml"
+INCLUDE "utils.ml"
+
+
+external init_lib : unit -> unit = "caml_init_lib"
+
+exception CPlusPlusError of string
+
+let () = Callback.register_exception "CPlusPlusError" (CPlusPlusError "")
+
+let () =  init_lib ()
+
+type node = [ `Tree ] Node.t
+
+type tree
+
+type bit_vector = string
+
+external bool_of_int : int -> bool = "%identity"
+
+let bit_vector_unsafe_get v i =
+  bool_of_int
+    (((Char.code (String.unsafe_get v (i lsr 3))) lsr (i land 7)) land 1)
+
+type t = {
+  doc : tree;
+  elements: Ptset.Int.t;
+  attributes: Ptset.Int.t;
+  attribute_array : Tag.t array;
+  children : Ptset.Int.t array;
+  siblings : Ptset.Int.t array;
+  descendants: Ptset.Int.t array;
+  followings: Ptset.Int.t array;
+}
+
+
+
+external parse_xml_uri : string -> int -> bool -> bool -> int -> tree = "caml_call_shredder_uri"
+external parse_xml_string :  string -> int -> bool -> bool -> int -> tree = "caml_call_shredder_string"
+external tree_print_xml_fast3 : tree -> [`Tree ] Node.t -> Unix.file_descr -> unit = "caml_xml_tree_print"
+let print_xml t n fd =
+  tree_print_xml_fast3 t.doc n fd
+
+
+external tree_save : tree -> Unix.file_descr -> string -> unit = "caml_xml_tree_save"
+external tree_load : Unix.file_descr -> string -> bool -> int -> tree = "caml_xml_tree_load"
+
+external nullt : unit -> 'a Node.t = "caml_xml_tree_nullt"
+
+let nil : [`Tree ] Node.t = Node.nil
+let root : [`Tree ] Node.t = Node.null
+
+type unordered_set
+
+external unordered_set_alloc : int -> unordered_set = "caml_unordered_set_alloc"
+external unordered_set_length : unordered_set -> int = "caml_unordered_set_length"
+external unordered_set_insert : unordered_set -> int -> unit = "caml_unordered_set_set" "noalloc"
+
+module HPtset = Hashtbl.Make(Ptset.Int)
+
+let vector_htbl = HPtset.create MED_H_SIZE
+
+let unordered_set_of_set s =
+  try
+    HPtset.find vector_htbl s
+  with
+      Not_found ->
+       let v = unordered_set_alloc (Ptset.Int.cardinal s) in
+       let _ = Ptset.Int.iter (fun e -> unordered_set_insert v e) s in
+         HPtset.add vector_htbl s v; v
+
+let ptset_to_vector = unordered_set_of_set
+
+(** tree interface *)
+
+external tree_root : tree -> [`Tree] Node.t = "caml_xml_tree_root"  "noalloc"
+
+
+external tree_first_child : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_first_child" "noalloc"
+let first_child t n = tree_first_child t.doc n
+
+external tree_first_element : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_first_element" "noalloc"
+let first_element t n = tree_first_element t.doc n
+
+external tree_tagged_child : tree -> [`Tree] Node.t -> Tag.t -> [`Tree] Node.t = "caml_xml_tree_tagged_child" "noalloc"
+let tagged_child t n tag = tree_tagged_child t.doc n tag
+
+external tree_select_child : tree -> [`Tree ] Node.t -> unordered_set -> [`Tree] Node.t = "caml_xml_tree_select_child" "noalloc"
+let select_child t n tag_set = tree_select_child t.doc n tag_set
+
+external tree_last_child : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_last_child" "noalloc"
+let last_child t n = tree_last_child t.doc n
+
+
+
+external tree_next_sibling : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_next_sibling"  "noalloc"
+let next_sibling t n = tree_next_sibling t.doc n
+
+external tree_next_element : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_next_element"  "noalloc"
+let next_element t n = tree_next_element t.doc n
+
+external tree_tagged_following_sibling : tree -> [`Tree] Node.t -> Tag.t -> [`Tree] Node.t = "caml_xml_tree_tagged_following_sibling" "noalloc"
+let tagged_following_sibling t n tag = tree_tagged_following_sibling t.doc n tag
+
+external tree_select_following_sibling : tree -> [`Tree ] Node.t -> unordered_set -> [`Tree] Node.t = "caml_xml_tree_select_following_sibling" "noalloc"
+let select_following_sibling t n tag_set = tree_select_following_sibling t.doc n tag_set
+
+external tree_prev_sibling : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_prev_sibling" "noalloc"
+let prev_sibling t n = tree_prev_sibling t.doc n
+
+
+
+external tree_tagged_descendant : tree -> [`Tree ] Node.t -> Tag.t -> [`Tree ] Node.t = "caml_xml_tree_tagged_descendant" "noalloc"
+let tagged_descendant t n tag = tree_tagged_descendant t.doc n tag
+
+external tree_select_descendant : tree -> [`Tree ] Node.t -> unordered_set -> [`Tree] Node.t = "caml_xml_tree_select_descendant" "noalloc"
+let select_descendant t n tag_set = tree_select_descendant t.doc n tag_set
+
+external tree_tagged_following_before : tree -> [`Tree ] Node.t -> Tag.t -> [`Tree ] Node.t -> [`Tree ] Node.t = "caml_xml_tree_tagged_following_before" "noalloc"
+let tagged_following_before t n tag ctx = tree_tagged_following_before t.doc n tag ctx
+
+external tree_select_following_before : tree -> [`Tree ] Node.t -> unordered_set -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_select_following_before" "noalloc"
+let select_following_before t n tag_set ctx = tree_select_following_before t.doc n tag_set ctx
+
+external tree_parent : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_parent" "noalloc"
+let parent t n = tree_parent t.doc n
+
+external tree_binary_parent : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_binary_parent"
+  "noalloc"
+let binary_parent t n = tree_binary_parent t.doc n
+
+
+external tree_tag : tree -> [`Tree] Node.t -> Tag.t = "caml_xml_tree_tag" "noalloc"
+let tag t n = tree_tag t.doc n
+
+external tree_is_first_child : tree -> [ `Tree ] Node.t -> bool = "caml_xml_tree_is_first_child" "noalloc"
+let is_first_child t n = tree_is_first_child t.doc n
+
+external tree_is_right_descendant : tree -> [ `Tree ] Node.t -> [`Tree] Node.t -> bool =
+  "caml_xml_tree_is_right_descendant" "noalloc"
+
+let is_right_descendant t n1 n2 = tree_is_right_descendant t.doc n1 n2
+;;
+
+let node_tags t = Ptset.Int.add Tag.document_node t.descendants.(Tag.document_node)
+
+let attribute_tags t = t.attributes
+
+let element_tags t = t.elements
+
+let tags t tag =
+  t.children.(tag), t.descendants.(tag), t.siblings.(tag), t.followings.(tag)
+
+open Format
+let dump_tag_table t =
+  eprintf "Child tags:\n%!";
+  Array.iteri
+    (fun tag set -> eprintf "%s: %a\n%!"
+      (Tag.to_string tag) TagSet.print (TagSet.inj_positive set))
+    t.children;
+  eprintf "-----------------------------\n%!";
+  eprintf "Descendant tags:\n%!";
+  Array.iteri
+    (fun tag set -> eprintf "%s: %a\n%!"
+      (Tag.to_string tag) TagSet.print (TagSet.inj_positive set))
+    t.descendants;
+  eprintf "-----------------------------\n%!";
+  eprintf "Sibling tags:\n%!";
+  Array.iteri
+    (fun tag set -> eprintf "%s: %a\n%!"
+      (Tag.to_string tag) TagSet.print (TagSet.inj_positive set))
+    t.siblings;
+  eprintf "-----------------------------\n%!";
+  eprintf "Following tags:\n%!";
+  Array.iteri
+    (fun tag set -> eprintf "%s: %a\n%!"
+      (Tag.to_string tag) TagSet.print (TagSet.inj_positive set))
+    t.followings;
+   eprintf "-----------------------------\n%!"
+
+
+external tree_subtree_tags : tree -> [`Tree] Node.t -> Tag.t -> int = "caml_xml_tree_subtree_tags" "noalloc"
+let subtree_tags t n tag = tree_subtree_tags t.doc n tag
+
+external tree_subtree_size : tree -> [`Tree] Node.t -> int = "caml_xml_tree_subtree_size" "noalloc"
+let subtree_size t n = tree_subtree_size t.doc n
+
+let subtree_elements t node =
+  let size = tree_subtree_size t.doc node - 1 in
+    if size == 0 then 0
+    else let size = size - (tree_subtree_tags t.doc node Tag.pcdata) in
+      if size < 2 then size else
+       let acc = ref size in
+         for i = 0 to Array.length t.attribute_array - 1 do
+           acc := !acc - tree_subtree_tags t.doc node t.attribute_array.(i)
+         done;
+         !acc
+
+external tree_closing : tree -> [`Tree] Node.t -> [`Tree] Node.t = "caml_xml_tree_closing" "noalloc"
+let closing t n = tree_closing t.doc n
+
+external tree_num_tags : tree -> int = "caml_xml_tree_num_tags" "noalloc"
+let num_tags t = tree_num_tags t.doc
+
+external tree_size : tree -> int = "caml_xml_tree_size" "noalloc"
+let size t = tree_size t.doc
+
+
+let stats t =
+  let tree = t.doc in
+  let rec loop left node acc_d total_d num_leaves =
+    if node == nil then
+    (acc_d+total_d,if left then num_leaves+1 else num_leaves)
+    else
+    let d,td = loop true (tree_first_child tree node) (acc_d+1) total_d num_leaves in
+    loop false (tree_next_sibling tree  node) (acc_d)  d td
+  in
+  let a,b = loop true root 0 0 0
+  in
+  Printf.eprintf "Average depth: %f, number of leaves %i\n%!" ((float_of_int a)/. (float_of_int b)) b
+;;
+
+module TagS =
+  struct
+    include Ptset.Make (
+      struct type t = int
+            type data = t
+            external hash : t -> int = "%identity"
+            external uid : t -> Uid.t = "%identity"
+            external equal : t -> t -> bool = "%eq"
+            external make : t -> int = "%identity"
+            external node : t -> int = "%identity"
+            external stats : unit -> unit = "%identity"
+      end
+    )
+    let to_ptset s = fold (Ptset.Int.add) s Ptset.Int.empty
+  end
+
+module TSTSCache =
+  Hashtbl.Make(struct type t = TagS.t * TagS.t
+                     let hash (x, y) =
+                       HASHINT2(Uid.to_int x.TagS.Node.id,
+                                Uid.to_int y.TagS.Node.id)
+                     let equal u v =
+                       let u1,u2 = u
+                       and v1,v2 = v in
+                         u1 == v1 && u2 == v2
+              end)
+module TagTSCache =
+  Hashtbl.Make(struct type t = Tag.t * TagS.t
+                     let hash (x, y) =
+                       HASHINT2(x, Uid.to_int y.TagS.Node.id)
+                     let equal u v =
+                       let u1,u2 = u
+                       and v1,v2 = v in
+                         u1 == v1 && u2 == v2
+              end)
+
+let add_cache = TagTSCache.create 1023
+let union_cache = TSTSCache.create 1023
+let subset_cache = TSTSCache.create 1023
+
+let clear_cache () =
+  TSTSCache.clear union_cache;
+  TSTSCache.clear subset_cache;
+  TagTSCache.clear add_cache
+
+let _subset x y =
+  (x == y) || (x == TagS.empty) ||
+    if y == TagS.empty then false
+    else
+      let key = (x, y) in
+       try
+         TSTSCache.find subset_cache key
+       with
+         | Not_found ->
+             let z = TagS.subset x y in
+               TSTSCache.add subset_cache key z; z
+
+let order ((x, y) as z) =
+  if x.TagS.Node.id <= y.TagS.Node.id then z
+  else (y, x)
+
+let _union x y =
+  if _subset x y then y
+  else if _subset y x then x
+  else
+   let key = order (x, y) in
+     try
+       TSTSCache.find union_cache key
+    with
+      | Not_found ->
+         let z = TagS.union x y in
+           TSTSCache.add union_cache key z; z
+
+let _add t s =
+  let key = (t,s) in
+    try
+      TagTSCache.find add_cache key
+    with
+      | Not_found ->
+         let z = TagS.add t s in
+           TagTSCache.add add_cache key z;z
+
+let child_sibling_labels tree =
+  let table_c = Array.create (tree_num_tags tree) TagS.empty in
+  let table_n = Array.copy table_c in
+  let rec loop node =
+    if node == nil then TagS.empty
+    else
+      let children = loop (tree_first_child tree node) in
+      let tag = tree_tag tree node in
+      let () =
+       let tc = table_c.(tag) in
+       if _subset children tc then ()
+       else table_c.(tag) <-  _union tc children
+      in
+      let siblings = loop (tree_next_sibling tree node) in
+      let () =
+       let tn = table_n.(tag) in
+         if _subset siblings tn then ()
+         else table_n.(tag) <- _union tn siblings
+      in
+       _add tag siblings
+  in
+    ignore (loop root);
+    table_c, table_n
+
+let descendant_labels tree =
+  let table_d = Array.create (tree_num_tags tree) TagS.empty in
+  let rec loop node =
+    if node == nil then  TagS.empty else
+      let d1 = loop (tree_first_child tree node) in
+      let d2 = loop (tree_next_sibling tree node) in
+      let tag = tree_tag tree node in
+      let () =
+       let td = table_d.(tag) in
+         if _subset d1 td then ()
+         else table_d.(tag) <- _union td d1;
+      in
+       _add tag (_union d1 d2)
+  in
+    ignore (loop root);
+    table_d
+
+let collect_labels tree =
+  let table_f = Array.create (tree_num_tags tree) TagS.empty in
+  let table_n = Array.copy table_f in
+  let table_c = Array.copy table_f in
+  let table_d = Array.copy table_f in
+  let rec loop node foll_siblings descendants followings =
+    if node == nil then foll_siblings, descendants, followings else
+      let tag = tree_tag tree node in
+      let () =
+       let tf = table_f.(tag) in
+         if _subset followings tf then ()
+         else table_f.(tag) <- _union tf followings in
+      let () =
+       let tn = table_n.(tag) in
+         if _subset foll_siblings tn then ()
+         else table_n.(tag) <- _union tn foll_siblings in
+      let children, n_descendants, n_followings =
+       loop (tree_last_child tree node) TagS.empty TagS.empty followings
+      in
+      let () =
+       let tc = table_c.(tag) in
+         if _subset children tc then ()
+         else table_c.(tag) <- _union tc children
+      in
+      let () =
+       let td = table_d.(tag) in
+         if _subset n_descendants td then ()
+         else table_d.(tag) <- _union td n_descendants
+      in
+       loop (tree_prev_sibling tree node)
+         (_add tag foll_siblings)
+         (_add tag (_union n_descendants descendants))
+         (_add tag n_followings)
+  in
+    ignore (loop root TagS.empty TagS.empty TagS.empty);
+    table_f, table_n, table_c, table_d
+
+
+let is_nil t = t == nil
+let is_node t = t != nil
+let is_root t = t == root
+
+let node_of_t t  =
+  let _ = Tag.init (Obj.magic t) in
+  let f, n, c, d = time collect_labels t ~msg:"Building tag relationship table" in
+  let c = Array.map TagS.to_ptset c in
+  let n = Array.map TagS.to_ptset n in
+  let f = Array.map TagS.to_ptset f in
+  let d = Array.map TagS.to_ptset d in
+  let () = clear_cache () in
+  let attributes = Ptset.Int.add Tag.attribute d.(Tag.attribute) in
+  let elements = Ptset.Int.add Tag.document_node
+    (Ptset.Int.remove Tag.pcdata
+       (Ptset.Int.diff d.(Tag.document_node) attributes))
+  in
+    { doc= t;
+      attributes = attributes;
+      attribute_array = Array.of_list (Ptset.Int.elements attributes);
+      elements = elements;
+      children = c;
+      siblings = n;
+      descendants = d;
+      followings = f
+
+    }
+
+let parse f str =
+  node_of_t
+    (f str !Options.sample_factor
+       !Options.index_empty_texts
+       !Options.disable_text_collection
+       !Options.text_index_type
+    )
+
+let parse_xml_uri str = parse parse_xml_uri str
+let parse_xml_string str =  parse parse_xml_string str
+
+let size t = tree_size t.doc;;
+
+external pool : tree -> Tag.pool = "%identity"
+
+let magic_string = "SXSI_INDEX"
+let version_string = "3"
+
+let pos fd =
+  Unix.lseek fd 0  Unix.SEEK_CUR
+
+let pr_pos fd = Printf.eprintf "At position %i\n%!" (pos fd)
+
+let write fd s =
+  let sl = String.length s in
+  let ssl = Printf.sprintf "%020i" sl in
+    ignore (Unix.write fd ssl 0 20);
+    ignore (Unix.write fd s 0 (String.length s))
+
+let rec really_read fd buffer start length =
+  if length <= 0 then () else
+    match Unix.read fd buffer start length with
+       0 -> raise End_of_file
+      | r -> really_read fd buffer (start + r) (length - r);;
+
+let read fd =
+  let buffer = String.create 20 in
+  let _ =  really_read fd buffer 0 20 in
+  let size = int_of_string buffer in
+  let buffer = String.create size in
+  let _ =  really_read fd buffer 0 size in
+    buffer
+
+let save_tag_table channel t =
+  let t = Array.map (fun s -> Array.of_list (Ptset.Int.elements s)) t in
+    Marshal.to_channel channel t []
+
+let save t str =
+  let fd = Unix.openfile str [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
+  let out_c = Unix.out_channel_of_descr fd in
+  let _ = set_binary_mode_out out_c true in
+    output_string out_c magic_string;
+    output_char out_c '\n';
+    output_string out_c version_string;
+    output_char out_c '\n';
+    save_tag_table out_c t.children;
+    save_tag_table out_c t.siblings;
+    save_tag_table out_c t.descendants;
+    save_tag_table out_c t.followings;
+    (* we need to move the fd to the correct position *)
+    flush out_c;
+    ignore (Unix.lseek fd (pos_out out_c) Unix.SEEK_SET);
+    tree_save t.doc fd str;
+    close_out out_c
+;;
+let load_tag_table channel =
+  let table : int array array = Marshal.from_channel channel in
+    Array.map (fun a -> Ptset.Int.from_list (Array.to_list a)) table
+
+let load ?(sample=64) ?(load_text=true) str =
+  let fd = Unix.openfile str [ Unix.O_RDONLY ] 0o644 in
+  let in_c = Unix.in_channel_of_descr fd in
+  let _ = set_binary_mode_in in_c true in
+  let load_table () =
+    (let ms = input_line in_c in if ms <> magic_string then failwith "Invalid index file");
+    (let vs = input_line in_c in if vs <> version_string then failwith "Invalid version file");
+    let c = load_tag_table in_c in
+    let s = load_tag_table in_c in
+    let d = load_tag_table in_c in
+    let f = load_tag_table in_c in
+      c,s,d,f
+  in
+  let c, s, d, f = time ~msg:"Loading tag table"(load_table) () in
+  ignore(Unix.lseek fd (pos_in in_c) Unix.SEEK_SET);
+    let xml_tree = tree_load fd str load_text sample in
+    let () = Tag.init (Obj.magic xml_tree) in
+    let attributes = Ptset.Int.add Tag.attribute d.(Tag.attribute) in
+    let elements = Ptset.Int.add Tag.document_node
+      (Ptset.Int.remove Tag.pcdata
+        (Ptset.Int.diff d.(Tag.document_node) attributes))
+    in
+    let tree = { doc = xml_tree;
+                attributes = attributes;
+                attribute_array = Array.of_list (Ptset.Int.elements attributes);
+                elements = elements;
+                children = c;
+                siblings = s;
+                descendants = d;
+                followings = f
+            }
+  in close_in in_c;
+  tree
+
+
+
+
+let tag_pool t = pool t.doc
+
+let equal a b = a == b
+
+let nts = function
+    -1 -> "Nil"
+  | i -> Printf.sprintf "Node (%i)"  i
+
+let dump_node t = nts (Node.to_int t)
+
+
+
+type query_result = { bv : bit_vector;
+                     pos : node array;
+                   }
+
+external tree_flush : tree -> Unix.file_descr -> unit = "caml_xml_tree_flush"
+let flush t fd = tree_flush t.doc fd
+
+external text_prefix : tree -> string -> query_result = "caml_text_collection_prefix_bv"
+let text_prefix t s = text_prefix t.doc s
+
+external text_suffix : tree -> string -> query_result = "caml_text_collection_suffix_bv"
+let text_suffix t s = text_suffix t.doc s
+
+external text_equals : tree -> string -> query_result = "caml_text_collection_equals_bv"
+let text_equals t s = text_equals t.doc s
+
+external text_contains : tree -> string -> query_result = "caml_text_collection_contains_bv"
+let text_contains t s = text_contains t.doc s
+
+
+module Predicate = Hcons.Make (
+  struct
+    type _t = t
+    type t = (_t -> node -> bool) ref
+    let hash t = Hashtbl.hash t
+    let equal t1 t2 = t1 == t2
+end)
+
+let string_of_query query =
+    match query with
+      | `Prefix -> "starts-with"
+      | `Suffix -> "ends-with"
+      | `Equals -> "equals"
+      | `Contains -> "contains"
+;;
+
+let query_fun = function
+      | `Prefix -> text_prefix
+      | `Suffix -> text_suffix
+      | `Equals -> text_equals
+      | `Contains -> text_contains
+;;
+
+let _pred_cache = Hashtbl.create 17
+;;
+let mk_pred query s =
+  let f = query_fun query in
+  let memo = ref (fun _ _ -> failwith "Undefined") in
+  memo := begin fun tree node ->
+    let results =
+      try Hashtbl.find _pred_cache (query,s) with
+         Not_found ->
+           time ~count:1 ~msg:(Printf.sprintf "Computing text query %s(%s)"
+                                 (string_of_query query) s)
+             (f tree) s
+    in
+    let bv = results.bv in
+    memo := begin fun _ n ->
+      let b =
+       bit_vector_unsafe_get bv (Node.to_int n)
+      in
+      D_TRACE_(Printf.eprintf "Result of memoized call to query %s is %b for node %i\n" s b (Node.to_int n));
+      b
+    end;
+    let b = bit_vector_unsafe_get bv (Node.to_int node) in
+    D_TRACE_(Printf.eprintf "Result is %b for node %i\n" b (Node.to_int node));
+    b
+  end;
+  Predicate.make memo
+
+
+let full_text_prefix t s = (text_prefix t s).pos
+
+let full_text_suffix t s = (text_suffix t s).pos
+
+let full_text_equals t s = (text_equals t s).pos
+
+let full_text_contains t s = (text_contains t s).pos
+
+let full_text_query q t s =
+  let res = (query_fun q) t s in
+  Hashtbl.replace _pred_cache (q,s) res;
+  res.pos