1 (******************************************************************************)
2 (* SXSI : XPath evaluator *)
3 (* Kim Nguyen (Kim.Nguyen@nicta.com.au) *)
4 (* Copyright NICTA 2008 *)
5 (* Distributed under the terms of the LGPL (see LICENCE) *)
6 (******************************************************************************)
11 type node_kind = [`Text | `Tree ]
13 let compare_node : 'a node -> 'a node -> int = (-)
14 let equal_node : 'a node -> 'a node -> bool = (==)
16 (* abstract type, values are pointers to a XMLTree C++ object *)
18 external int_of_node : 'a node -> int = "%identity"
20 external parse_xml_uri : string -> int -> bool -> bool -> tree = "caml_call_shredder_uri"
21 external parse_xml_string : string -> int -> bool -> bool -> tree = "caml_call_shredder_string"
23 external save_tree : tree -> string -> unit = "caml_xml_tree_save"
24 external load_tree : string -> int -> tree = "caml_xml_tree_load"
26 external nullt : unit -> 'a node = "caml_xml_tree_nullt"
28 let nil : 'a node = Obj.magic (-1)
30 external text_get_tc_text : tree -> [`Text] node -> string = "caml_text_collection_get_text"
32 external text_is_empty : tree -> [`Text ] node -> bool = "caml_text_collection_empty_text"
34 let text_is_empty t n =
35 (equal_node nil n) || text_is_empty t n
39 external text_is_contains : tree -> string -> bool = "caml_text_collection_is_contains"
40 external text_count_contains : tree -> string -> int = "caml_text_collection_count_contains"
41 external text_count : tree -> string -> int = "caml_text_collection_count"
42 external text_contains : tree -> string -> [`Text ] node array = "caml_text_collection_contains"
43 external text_unsorted_contains : tree -> string -> unit = "caml_text_collection_unsorted_contains"
44 external get_cached_text : tree -> [`Text] node -> string = "caml_text_collection_get_cached_text"
47 external tree_serialize : tree -> string -> unit = "caml_xml_tree_serialize"
49 external tree_unserialize : string -> tree = "caml_xml_tree_unserialize"
51 external tree_root : tree -> [`Tree] node = "caml_xml_tree_root"
53 let tree_is_nil x = equal_node x nil
55 external tree_parent : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
56 external tree_parent_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
57 external tree_prev_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_prev_doc"
58 external tree_first_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
59 external tree_next_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
60 external tree_prev_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_prev_sibling"
61 external tree_is_leaf : tree -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
62 external tree_last_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_last_child"
63 external tree_is_first_child : tree -> [`Tree] node -> bool = "caml_xml_tree_is_first_child"
65 (* external tag : tree -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
66 external tree_tag_id : tree -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id"
69 let tree_is_last t n = equal_node nil (tree_next_sibling t n)
71 external tree_prev_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
73 external tree_my_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
74 external tree_next_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
75 external tree_doc_ids : tree -> [`Tree ] node -> [`Text ] node * [`Text ] node = "caml_xml_tree_doc_ids"
77 let text_size tree = int_of_node (snd ( tree_doc_ids tree (Obj.magic 0) ))
79 let get_cached_text t x =
85 external tree_text_xml_id : tree -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
86 external tree_node_xml_id : tree -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
87 external tree_is_ancestor : tree -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
88 external tree_tagged_desc : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc"
89 external tree_tagged_foll_below : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_below"
90 external tree_subtree_tags : tree -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags"
92 external tree_select_below : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_below"
93 external tree_select_desc_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_desc_only"
94 external tree_select_next : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_next"
95 external tree_select_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only"
96 external tree_select_desc_or_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only" *)
100 | Node of [`Tree] node
101 | Text of [`Text] node * [`Tree] node
103 type t = { doc : tree;
105 ttable : (Tag.t,(Ptset.Int.t*Ptset.Int.t)) Hashtbl.t;
108 let text_size t = text_size t.doc
109 module MemUnion = Hashtbl.Make (struct
110 type t = Ptset.Int.t*Ptset.Int.t
111 let equal (x,y) (z,t) = (Ptset.Int.equal x z)&&(Ptset.Int.equal y t)
112 let equal a b = equal a b || equal b a
113 let hash (x,y) = (* commutative hash *)
114 let x = Ptset.Int.hash x
115 and y = Ptset.Int.hash y
117 if x < y then HASHINT2(x,y) else HASHINT2(y,x)
120 let collect_tags tree =
121 let h_union = MemUnion.create BIG_H_SIZE in
124 MemUnion.find h_union (s1,s2)
126 | Not_found -> let s = Ptset.Int.union s1 s2
128 MemUnion.add h_union (s1,s2) s;s
130 let h_add = Hashtbl.create BIG_H_SIZE in
132 let k = HASHINT2(Tag.hash t,Ptset.Int.hash s) in
136 | Not_found -> let r = Ptset.Int.add t s in
137 Hashtbl.add h_add k r;r
139 let h = Hashtbl.create BIG_H_SIZE in
140 let sing = Ptset.Int.singleton Tag.pcdata in
149 Hashtbl.replace h t (pt_cup sbelow sb, pt_cup safter sa)
151 let rec loop id acc =
153 then (Ptset.Int.empty,acc)
155 let below2,after2 = loop (tree_next_sibling tree id) acc in
156 let below1,after1 = loop (tree_first_child tree id) after2 in
157 let tag = tree_tag_id tree id in
158 update tag below1 after2;
159 pt_add tag (pt_cup below1 below2), (pt_add tag after1)
161 let b,a = loop (tree_root tree) Ptset.Int.empty in
162 update Tag.pcdata b a;
169 let contains_array = ref [| |]
170 let contains_index = Hashtbl.create 4096
173 Hashtbl.find contains_index i
177 let init_contains t s =
178 let a = text_contains t.doc s
180 Array.fast_sort (compare) a;
182 Array.iter (fun x -> Hashtbl.add contains_index x true) !contains_array
184 let count_contains t s = text_count_contains t.doc s
185 let unsorted_contains t s = text_unsorted_contains t.doc s
187 let init_naive_contains t s =
188 let i,j = tree_doc_ids t.doc (tree_root t.doc)
190 let regexp = Str.regexp_string s in
193 let _ = Str.search_forward regexp arg 0;
197 let rec loop n acc l =
200 let s = get_cached_text t.doc n
203 then loop (n+1) (n::acc) (l+1)
204 else loop (n+1) acc l
206 let acc,l = loop i [] 0 in
207 let a = Array.create l nil in
208 let _ = List.fold_left (fun cpt e -> a.(cpt) <- e; (cpt-1)) (l-1) acc
214 module DocIdSet = struct
215 include Set.Make (struct type t = [`Text] node
216 let compare = compare_node end)
219 let is_nil t = t.node == Nil
221 let is_node t = t.node != Nil
224 let _ = Tag.init (Obj.magic t) in
225 let table = collect_tags t
228 let _ = Hashtbl.iter (fun t (sb,sa) ->
229 Printf.eprintf "'%s' -> { " (Tag.to_string t);
230 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sb;
231 Printf.eprintf "}\n { ";
232 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sa;
233 Printf.eprintf "} \n----------------------------------\n";
235 let i,j = tree_doc_ids t (tree_root t) in
236 Printf.eprintf "%i docs, range from %i to %i\n%!" (Array.length s) i j;
237 Array.iter (fun i -> print_endline (">>>" ^ i ^ "<<<")) s; *)
239 node = Node(tree_root t);
242 let finalize _ = Printf.eprintf "Release the string list !\n%!"
248 !Options.sample_factor
249 !Options.index_empty_texts
250 !Options.disable_text_collection)
252 let parse_xml_uri str = parse parse_xml_uri str
253 let parse_xml_string str = parse parse_xml_string str
256 external pool : tree -> Tag.pool = "%identity"
258 let save t str = (save_tree t.doc str)
261 let load ?(sample=64) str =
262 node_of_t (load_tree str sample)
267 let tag_pool t = pool t.doc
269 let compare a b = match a.node,b.node with
273 | Node(i),Node(j) -> compare_node i j
274 | Text(i,_), Text(j,_) -> compare_node i j
275 | Node(i), Text(_,j) -> compare_node i j
276 | Text(_,i), Node(j) -> compare_node i j
278 let equal a b = (compare a b) == 0
281 let norm (n : [`Tree ] node ) = if n == -1 then Nil else Node (n)
285 | Text (i,j) -> Printf.sprintf "Text (%i, %i)" i j
286 | Node (i) -> Printf.sprintf "Node (%i)" i
288 let dump_node t = nts t.node
290 let mk_nil t = { t with node = Nil }
291 let root n = { n with node = norm (tree_root n.doc) }
293 let is_root n = match n.node with
294 | Node(t) -> (int_of_node t) == 0
297 let is_left n = match n.node with
298 | Node(t) -> (tree_is_first_child n.doc t) && (equal_node nil (tree_prev_text n.doc t))
299 | Text(_,t) -> tree_is_nil t || tree_is_first_child n.doc t
302 let is_below_right t1 t2 =
303 match (t1.node,t2.node) with
304 | Nil,_ | _,Nil -> false
305 | Node(i1), Node(i2) ->
306 tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2
307 && not (tree_is_ancestor t1.doc i1 i2)
308 | Text(_,i1),Node(i2) -> i1 == i2 ||
309 (tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2 && i1 < i2)
310 | Text(_,i1),Text(i,_) ->
311 let x,y = tree_doc_ids t1.doc i1 in
313 | Node(i1), Text(i,_) ->
314 let i2 = tree_next_sibling t1.doc i1 in
315 let x,y = tree_doc_ids t1.doc i2 in
320 match n.node with (* inlined parent *)
321 | Node(t) when (int_of_node t)== 0 -> Nil
323 let txt = tree_prev_text n.doc t in
324 if text_is_empty n.doc txt then
325 let ps = tree_prev_sibling n.doc t in
328 Node(tree_parent n.doc t)
333 let ps = tree_prev_doc n.doc i in
335 then Node (tree_parent_doc n.doc i)
337 | _ -> failwith "parent"
339 { n with node = node' }
343 | Node i -> { n with node= norm(tree_first_child n.doc i) }
344 | _ -> { n with node = Nil }
348 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
349 | _ -> { n with node = Nil }
351 let node_sibling_ctx n _ =
353 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
354 | _ -> { n with node = Nil }
361 let fs = tree_first_child n.doc t in
364 let txt = tree_my_text n.doc t in
365 if equal_node nil txt
369 let txt = tree_prev_text n.doc fs in
370 if equal_node nil txt
374 | Nil -> failwith "first_child"
376 { n with node = node'}
381 | Text (_,ns) -> norm ns
383 let ns = tree_next_sibling n.doc t in
384 let txt = tree_next_text n.doc t in
385 if equal_node nil txt
388 | Nil -> failwith "next_sibling"
390 { n with node = node'}
392 let next_sibling_ctx n _ = next_sibling n
394 let left = first_child
395 let right = next_sibling
399 | Node(n) -> tree_node_xml_id t.doc n
400 | Text(i,_) -> tree_text_xml_id t.doc i
405 | Text(_) -> Tag.pcdata
406 | Node(n) -> tree_tag_id t.doc n
410 let select_next tb tf t s =
412 | Node (below) -> begin
415 { t with node = norm (tree_select_next t.doc n (Ptset.Int.to_int_vector tb) (Ptset.Int.to_int_vector tf) below) }
416 | Text (i,n) when equal_node nil n ->
417 let p = tree_parent_doc t.doc i in
418 { t with node = norm (tree_select_next t.doc p (Ptset.Int.to_int_vector tb) (Ptset.Int.to_int_vector tf) below) }
420 if Ptset.mem (tree_tag_id t.doc n) (Ptset.Int.union tb tf)
421 then { t with node=Node(n) }
423 let vb = Ptset.Int.to_int_vector tb in
424 let vf = Ptset.Int.to_int_vector tf in
426 let dsc = tree_select_below t.doc n vb vf in
427 if equal_node nil dsc
428 then tree_select_next t.doc n vb vf below
431 { t with node = norm node }
432 | _ -> {t with node = Nil }
435 | _ -> { t with node = Nil }
440 let select_foll_only tf t s =
446 { t with node= norm (tree_select_foll_only t.doc n (Ptset.Int.to_int_vector tf) below) }
447 | Text(i,n) when equal_node nil n ->
448 let p = tree_parent_doc t.doc i in
449 { t with node= norm (tree_select_foll_only t.doc p (Ptset.Int.to_int_vector tf) below) }
451 if Ptset.mem (tree_tag_id t.doc n) tf
452 then { t with node=Node(n) }
454 let vf = Ptset.Int.to_int_vector tf in
456 let dsc = tree_select_desc_only t.doc n vf in
458 then tree_select_foll_only t.doc n vf below
461 { t with node = norm node }
462 | _ -> { t with node = Nil }
464 | _ -> {t with node=Nil }
466 let select_below tc td t=
469 let vc = Ptset.Int.to_int_vector tc
471 let vd = Ptset.Int.to_int_vector td
473 { t with node= norm(tree_select_below t.doc n vc vd) }
474 | _ -> { t with node=Nil }
477 let select_desc_only td t =
480 let vd = Ptset.Int.to_int_vector td
482 { t with node = norm(tree_select_desc_only t.doc n vd) }
483 | _ -> { t with node = Nil }
486 let tagged_desc tag t =
489 { t with node = norm(tree_tagged_desc t.doc n tag) }
490 | _ -> { t with node = Nil }
493 let tagged_foll_ctx tag t s =
499 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
500 | Text(i,n) when equal_node nil n ->
501 let p = tree_prev_doc t.doc i in
502 { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
504 if (tree_tag_id t.doc n) == tag
505 then { t with node=Node(n) }
508 let dsc = tree_tagged_desc t.doc n tag in
510 then tree_tagged_foll_below t.doc n tag below
513 { t with node = norm node }
514 | _ -> { t with node = Nil }
516 | _ -> {t with node=Nil }
522 let array_find a i j =
523 let l = Array.length a in
524 let rec loop idx x y =
525 if x > y || idx >= l then nil
527 if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
528 else loop (idx+1) x y
530 if a.(0) > j || a.(l-1) < i then nil
531 else loop !last_idx i j
536 let l = Array.length !contains_array in
539 let i,j = tree_doc_ids t.doc n in
540 let id = if l == 0 then i else (array_find !contains_array i j)
542 (* Printf.printf "Looking for text below node %i with tag %s in range %i %i, in array : [|\n%!"
543 n (Tag.to_string (tree_tag_id t.doc n)) i j;
544 Array.iter (fun i -> Printf.printf "%i " (int_of_node i )) !contains_array;
545 Printf.printf "|]\nResult is %i\n%!" id; *)
549 { t with node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
550 | _ -> (*Printf.printf "Here\n%!"; *)
551 { t with node = Nil }
553 let text_next t root =
554 let l = Array.length !contains_array in
555 let inf = match t.node with
556 | Node(n) -> snd(tree_doc_ids t.doc n)+1
562 let _,j = tree_doc_ids t.doc n in
563 let id = if l == 0 then if inf > j then nil else inf
564 else array_find !contains_array inf j
566 if id == nil then { t with node= Nil }
568 { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
569 | _ -> { t with node = Nil}
573 let subtree_tags t tag =
575 { doc = d; node = Node(NC n) } ->
579 let select_desc_array = ref [| |]
582 let init_tagged_next t tagid =
583 let l = subtree_tags (root t) tagid
585 tagged_desc_array := Array.create l { t with node= Nil };
588 if is_node t then begin
589 if tag t == tagid then
591 !tagged_desc_array.(!i) <- t;
594 collect (first_child t);
595 collect (next_sibling t)
602 let pr x= Format.fprintf ppf x in
604 { node=Nil } -> pr "NULLT: -1"
605 | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
606 | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
610 (* let tagged_next t tag =
611 if !idx >= Array.length !tagged_desc_array
612 then {t with node=Nil}
614 let r = !tagged_desc_array.(!idx)
620 let has_tagged_foll t tag = is_node (tagged_foll t tag)
621 let has_tagged_desc t tag = is_node (tagged_desc t tag)
624 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
627 let contains_old t s =
628 let regexp = Str.regexp_string s in
631 let _ = Str.search_forward regexp arg 0;
635 let rec find t acc = match t.node with
638 if matching (string t) then DocIdSet.add i acc else acc
639 | Node(_) -> (find (left t )) ((find (right t)) acc)
641 find t DocIdSet.empty
644 let contains_iter t s =
645 let regexp = Str.regexp_string s in
648 let _ = Str.search_forward regexp arg 0;
652 let size = Text.size t.doc in
654 if n == size then acc
657 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
658 DocIdSet.add (Obj.magic n) acc
661 find DocIdSet.empty 0
666 let count_contains t s = Text.count_contains t.doc s
669 let count t s = text_count t.doc s
672 if is_root t then false
674 if tag (parent t) == Tag.pcdata then false
676 let u = left (parent t) in
679 let print_xml_fast outc t =
680 let rec loop ?(print_right=true) t =
683 | Text(i,n) -> output_string outc (get_cached_text t.doc i);
687 let tg = Tag.to_string (tag t) in
691 output_char outc '<';
692 output_string outc tg;
694 Nil -> output_string outc "/>"
695 | Node(_) when Tag.equal (tag l) Tag.attribute ->
696 (loop_attributes (left l);
697 match (right l).node with
698 | Nil -> output_string outc "/>"
700 output_char outc '>';
702 output_string outc "</";
703 output_string outc tg;
704 output_char outc '>' )
706 output_char outc '>';
708 output_string outc "</";
709 output_string outc tg;
711 );if print_right then loop r
712 and loop_attributes a =
716 match (left a).node with
717 | Text(i,_) -> (get_cached_text a.doc i)
720 output_char outc ' ';
721 output_string outc (Tag.to_string (tag a));
722 output_string outc "=\"";
723 output_string outc value;
724 output_char outc '"';
725 loop_attributes (right a)
728 loop ~print_right:false t
731 let print_xml_fast outc t =
732 if Tag.to_string (tag t) = "" then
733 print_xml_fast outc (first_child t)
734 else print_xml_fast outc t
737 let tags_below t tag =
738 fst(Hashtbl.find t.ttable tag)
740 let tags_after t tag =
741 snd(Hashtbl.find t.ttable tag)
743 let tags t tag = Hashtbl.find t.ttable tag
745 let tagged_lowest t tag =
746 let rec loop_lowest i =
747 let j = tree_tagged_desc t.doc i tag in
748 if tree_is_nil j then i else loop_lowest j
752 let j = loop_lowest i in
755 if tree_is_nil j then
756 if (tree_tag_id t.doc i) == tag
764 let tagged_next t tag =
767 let n = tree_tagged_foll_below t.doc i tag (Obj.magic 0)
769 if tree_is_nil n then mk_nil t
771 tagged_lowest { t with node = Node n } tag
775 let rec binary_parent t =
778 | Node(0) -> { t with node = Nil }
780 let j = tree_prev_sibling t.doc i in
781 if tree_is_nil j then
782 let idoc = tree_prev_text t.doc i in
783 if equal_node nil idoc then
784 { t with node = Node (tree_parent t.doc i) }
786 { t with node = Text(idoc,i) }
788 let idoc = tree_prev_text t.doc i in
789 if equal_node nil idoc then
790 { t with node = Node (j) }
791 else { t with node = Text(idoc,i) }
793 if tree_is_nil i then
794 let n = tree_parent_doc t.doc d in
795 let lc = tree_last_child t.doc n in
796 if tree_is_nil lc then {t with node = Node n }
797 else { t with node = Node lc }
799 let j = tree_prev_sibling t.doc i in
800 if tree_is_nil j then
801 { t with node = Node (tree_parent t.doc i) }
802 else { t with node = Node j }
804 in match res.node with
806 if (Array.length !contains_array) != 0
807 then if in_array !contains_array idoc then res
808 else binary_parent res
812 let benchmark_text t =
814 match (root t).node with
815 | Node i -> let _,size = tree_doc_ids doc i in
816 Printf.eprintf "%i will take ~ %i seconds\n%!"
818 let a = Array.create size "" in
821 a.(i) <- text_get_tc_text t.doc (i+1)
825 let doc_ids (t:t) : (int*int) =
828 | Node i -> tree_doc_ids t.doc i
829 | Text (i,_) -> (i,i)
833 let subtree_tags t tag = match t.node with
835 | Node(i) -> tree_subtree_tags t.doc i tag
836 | Text(_,i) -> tree_subtree_tags t.doc i tag
838 let get_text t = match t.node with
839 | Text(i,_) -> get_cached_text t.doc i