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"
45 let get_cached_text t x =
47 else get_cached_text t x
49 external tree_serialize : tree -> string -> unit = "caml_xml_tree_serialize"
51 external tree_unserialize : string -> tree = "caml_xml_tree_unserialize"
53 external tree_root : tree -> [`Tree] node = "caml_xml_tree_root"
55 let tree_is_nil x = equal_node x nil
57 external tree_parent : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
58 external tree_parent_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
59 external tree_prev_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_prev_doc"
60 external tree_first_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
61 external tree_next_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
62 external tree_prev_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_prev_sibling"
63 external tree_is_leaf : tree -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
64 external tree_last_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_last_child"
65 external tree_is_first_child : tree -> [`Tree] node -> bool = "caml_xml_tree_is_first_child"
67 (* external tag : tree -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
68 external tree_tag_id : tree -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id"
71 let tree_is_last t n = equal_node nil (tree_next_sibling t n)
73 external tree_prev_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
75 external tree_my_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
76 external tree_next_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
77 external tree_doc_ids : tree -> [`Tree ] node -> [`Text ] node * [`Text ] node = "caml_xml_tree_doc_ids"
79 let text_size tree = int_of_node (snd ( tree_doc_ids tree (Obj.magic 0) ))
81 external tree_text_xml_id : tree -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
82 external tree_node_xml_id : tree -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
83 external tree_is_ancestor : tree -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
84 external tree_tagged_desc : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc"
85 external tree_tagged_foll_below : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_below"
86 external tree_subtree_tags : tree -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags"
87 external tree_select_below : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_below"
88 external tree_select_desc_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_desc_only"
89 external tree_select_next : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_next"
90 external tree_select_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only"
91 external tree_select_desc_or_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only"
95 | Node of [`Tree] node
96 | Text of [`Text] node * [`Tree] node
98 type t = { doc : tree;
100 ttable : (Tag.t,(Ptset.t*Ptset.t)) Hashtbl.t;
108 let text_size t = text_size t.doc
110 let collect_tags tree =
111 let h_union = Hashtbl.create 511 in
113 (* special case, since this is a union we want hash(s1,s2) = hash(s2,s1) *)
114 let x = Ptset.hash s1
115 and y = Ptset.hash s2 in
116 let h = if x < y then HASHINT2(x,y) else HASHINT2(y,x) in
118 Hashtbl.find h_union h
120 | Not_found -> let s = Ptset.union s1 s2
122 Hashtbl.add h_union h s;s
124 let h_add = Hashtbl.create 511 in
126 let k = HASHINT2(Tag.hash t,Ptset.hash s) in
130 | Not_found -> let r = Ptset.add t s in
131 Hashtbl.add h_add k r;r
133 let h = Hashtbl.create 511 in
134 let sing = Ptset.singleton Tag.pcdata in
143 Hashtbl.replace h t (pt_cup sbelow sb, pt_cup safter sa)
145 let rec loop id acc =
147 then (Ptset.empty,acc)
149 let below2,after2 = loop (tree_next_sibling tree id) acc in
150 let below1,after1 = loop (tree_first_child tree id) after2 in
151 let tag = tree_tag_id tree id in
152 update tag below1 after2;
153 pt_add tag (pt_cup below1 below2), (pt_add tag after1)
155 let b,a = loop (tree_root tree) Ptset.empty in
156 update Tag.pcdata b a;
163 let contains_array = ref [| |]
164 let contains_index = Hashtbl.create 4096
167 Hashtbl.find contains_index i
171 let init_contains t s =
172 let a = text_contains t.doc s
174 Array.fast_sort (compare) a;
176 Array.iter (fun x -> Hashtbl.add contains_index x true) !contains_array
178 let count_contains t s = text_count_contains t.doc s
179 let unsorted_contains t s = text_unsorted_contains t.doc s
181 let init_naive_contains t s =
182 let i,j = tree_doc_ids t.doc (tree_root t.doc)
184 let regexp = Str.regexp_string s in
187 let _ = Str.search_forward regexp arg 0;
191 let rec loop n acc l =
194 let s = get_cached_text t.doc n
197 then loop (n+1) (n::acc) (l+1)
198 else loop (n+1) acc l
200 let acc,l = loop i [] 0 in
201 let a = Array.create l nil in
202 let _ = List.fold_left (fun cpt e -> a.(cpt) <- e; (cpt-1)) (l-1) acc
208 module DocIdSet = struct
209 include Set.Make (struct type t = [`Text] node
210 let compare = compare_node end)
213 let is_nil t = t.node == Nil
215 let is_node t = t.node != Nil
218 let _ = Tag.init (Obj.magic t) in
219 let table = collect_tags t
222 let _ = Hashtbl.iter (fun t (sb,sa) ->
223 Printf.eprintf "'%s' -> { " (Tag.to_string t);
224 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sb;
225 Printf.eprintf "}\n { ";
226 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sa;
227 Printf.eprintf "} \n----------------------------------\n";
229 let i,j = tree_doc_ids t (tree_root t) in
230 Printf.eprintf "%i docs, range from %i to %i\n%!" (Array.length s) i j;
231 Array.iter (fun i -> print_endline (">>>" ^ i ^ "<<<")) s; *)
233 node = Node(tree_root t);
236 let finalize _ = Printf.eprintf "Release the string list !\n%!"
242 !Options.sample_factor
243 !Options.index_empty_texts
244 !Options.disable_text_collection)
246 let parse_xml_uri str = parse parse_xml_uri str
247 let parse_xml_string str = parse parse_xml_string str
250 external pool : tree -> Tag.pool = "%identity"
252 let save t str = (save_tree t.doc str)
255 let load ?(sample=64) str =
256 node_of_t (load_tree str sample)
261 let tag_pool t = pool t.doc
263 let compare a b = match a.node,b.node with
267 | Node(i),Node(j) -> compare_node i j
268 | Text(i,_), Text(j,_) -> compare_node i j
269 | Node(i), Text(_,j) -> compare_node i j
270 | Text(_,i), Node(j) -> compare_node i j
272 let equal a b = (compare a b) == 0
275 let norm (n : [`Tree ] node ) = if n == -1 then Nil else Node (n)
279 | Text (i,j) -> Printf.sprintf "Text (%i, %i)" i j
280 | Node (i) -> Printf.sprintf "Node (%i)" i
282 let dump_node t = nts t.node
284 let mk_nil t = { t with node = Nil }
285 let root n = { n with node = norm (tree_root n.doc) }
287 let is_root n = match n.node with
288 | Node(t) -> (int_of_node t) == 0
291 let is_left n = match n.node with
292 | Node(t) -> (tree_is_first_child n.doc t) && (equal_node nil (tree_prev_text n.doc t))
293 | Text(_,t) -> tree_is_nil t || tree_is_first_child n.doc t
296 let is_below_right t1 t2 =
297 match (t1.node,t2.node) with
298 | Nil,_ | _,Nil -> false
299 | Node(i1), Node(i2) ->
300 tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2
301 && not (tree_is_ancestor t1.doc i1 i2)
302 | Text(_,i1),Node(i2) -> i1 == i2 ||
303 (tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2 && i1 < i2)
304 | Text(_,i1),Text(i,_) ->
305 let x,y = tree_doc_ids t1.doc i1 in
307 | Node(i1), Text(i,_) ->
308 let i2 = tree_next_sibling t1.doc i1 in
309 let x,y = tree_doc_ids t1.doc i2 in
314 match n.node with (* inlined parent *)
315 | Node(t) when (int_of_node t)== 0 -> Nil
317 let txt = tree_prev_text n.doc t in
318 if text_is_empty n.doc txt then
319 let ps = tree_prev_sibling n.doc t in
322 Node(tree_parent n.doc t)
327 let ps = tree_prev_doc n.doc i in
329 then Node (tree_parent_doc n.doc i)
331 | _ -> failwith "parent"
333 { n with node = node' }
337 | Node i -> { n with node= norm(tree_first_child n.doc i) }
338 | _ -> { n with node = Nil }
342 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
343 | _ -> { n with node = Nil }
345 let node_sibling_ctx n _ =
347 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
348 | _ -> { n with node = Nil }
355 let fs = tree_first_child n.doc t in
358 let txt = tree_my_text n.doc t in
359 if equal_node nil txt
363 let txt = tree_prev_text n.doc fs in
364 if equal_node nil txt
368 | Nil -> failwith "first_child"
370 { n with node = node'}
375 | Text (_,ns) -> norm ns
377 let ns = tree_next_sibling n.doc t in
378 let txt = tree_next_text n.doc t in
379 if equal_node nil txt
382 | Nil -> failwith "next_sibling"
384 { n with node = node'}
386 let next_sibling_ctx n _ = next_sibling n
388 let left = first_child
389 let right = next_sibling
393 | Node(n) -> tree_node_xml_id t.doc n
394 | Text(i,_) -> tree_text_xml_id t.doc i
399 | Text(_) -> Tag.pcdata
400 | Node(n) -> tree_tag_id t.doc n
401 | _ -> failwith "tag"
404 let string_below t id =
405 let strid = parent_doc t.doc id in
408 (Tree.equal i strid) || (is_ancestor t.doc i strid)
409 | Node(SC(i,_)) -> Text.equal i id
413 let tagged_foll t tag =
414 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
416 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
417 | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil }
418 | { doc=d; node=Node(SC (_,n)) } ->
420 if tag_id d n == tag then n
422 let n' = tagged_desc d n tag in
423 if is_nil n' then tagged_foll d n tag
425 in {t with node= norm nnode}
426 | _ -> { t with node=Nil }
429 let tagged_desc t tag =
430 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
432 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
433 | _ -> { t with node=Nil }
436 let select_next tb tf t s =
438 | Node (below) -> begin
441 { t with node = norm (tree_select_next t.doc n (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
442 | Text (i,n) when equal_node nil n ->
443 let p = tree_parent_doc t.doc i in
444 { t with node = norm (tree_select_next t.doc p (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
446 if Ptset.mem (tree_tag_id t.doc n) (Ptset.union tb tf)
447 then { t with node=Node(n) }
449 let vb = Ptset.to_int_vector tb in
450 let vf = Ptset.to_int_vector tf in
452 let dsc = tree_select_below t.doc n vb vf in
453 if equal_node nil dsc
454 then tree_select_next t.doc n vb vf below
457 { t with node = norm node }
458 | _ -> {t with node = Nil }
461 | _ -> { t with node = Nil }
466 let select_foll_only tf t s =
472 { t with node= norm (tree_select_foll_only t.doc n (Ptset.to_int_vector tf) below) }
473 | Text(i,n) when equal_node nil n ->
474 let p = tree_parent_doc t.doc i in
475 { t with node= norm (tree_select_foll_only t.doc p (Ptset.to_int_vector tf) below) }
477 if Ptset.mem (tree_tag_id t.doc n) tf
478 then { t with node=Node(n) }
480 let vf = Ptset.to_int_vector tf in
482 let dsc = tree_select_desc_only t.doc n vf in
484 then tree_select_foll_only t.doc n vf below
487 { t with node = norm node }
488 | _ -> { t with node = Nil }
490 | _ -> {t with node=Nil }
492 let select_below tc td t=
495 let vc = Ptset.to_int_vector tc
497 let vd = Ptset.to_int_vector td
499 { t with node= norm(tree_select_below t.doc n vc vd) }
500 | _ -> { t with node=Nil }
503 let select_desc_only td t =
506 let vd = Ptset.to_int_vector td
508 { t with node = norm(tree_select_desc_only t.doc n vd) }
509 | _ -> { t with node = Nil }
512 let tagged_desc tag t =
515 { t with node = norm(tree_tagged_desc t.doc n tag) }
516 | _ -> { t with node = Nil }
519 let tagged_foll_below tag t s =
525 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
526 | Text(i,n) when equal_node nil n ->
527 let p = tree_prev_doc t.doc i in
528 { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
530 if (tree_tag_id t.doc n) == tag
531 then { t with node=Node(n) }
534 let dsc = tree_tagged_desc t.doc n tag in
536 then tree_tagged_foll_below t.doc n tag below
539 { t with node = norm node }
540 | _ -> { t with node = Nil }
542 | _ -> {t with node=Nil }
546 let array_find a i j =
547 let l = Array.length a in
548 let rec loop idx x y =
549 if x > y || idx >= l then nil
551 if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
552 else loop (idx+1) x y
554 if a.(0) > j || a.(l-1) < i then nil
555 else loop !last_idx i j
560 let l = Array.length !contains_array in
563 let i,j = tree_doc_ids t.doc n in
564 let id = if l == 0 then i else (array_find !contains_array i j)
566 (* Printf.printf "Looking for text below node %i with tag %s in range %i %i, in array : [|\n%!"
567 n (Tag.to_string (tree_tag_id t.doc n)) i j;
568 Array.iter (fun i -> Printf.printf "%i " (int_of_node i )) !contains_array;
569 Printf.printf "|]\nResult is %i\n%!" id; *)
573 { t with node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
574 | _ -> (*Printf.printf "Here\n%!"; *)
575 { t with node = Nil }
577 let text_next t root =
578 let l = Array.length !contains_array in
579 let inf = match t.node with
580 | Node(n) -> snd(tree_doc_ids t.doc n)+1
586 let _,j = tree_doc_ids t.doc n in
587 let id = if l == 0 then if inf > j then nil else inf
588 else array_find !contains_array inf j
590 if id == nil then { t with node= Nil }
592 { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
593 | _ -> { t with node = Nil}
597 let subtree_tags t tag =
599 { doc = d; node = Node(NC n) } ->
603 let select_desc_array = ref [| |]
606 let init_tagged_next t tagid =
607 let l = subtree_tags (root t) tagid
609 tagged_desc_array := Array.create l { t with node= Nil };
612 if is_node t then begin
613 if tag t == tagid then
615 !tagged_desc_array.(!i) <- t;
618 collect (first_child t);
619 collect (next_sibling t)
626 let pr x= Format.fprintf ppf x in
628 { node=Nil } -> pr "NULLT: -1"
629 | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
630 | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
634 (* let tagged_next t tag =
635 if !idx >= Array.length !tagged_desc_array
636 then {t with node=Nil}
638 let r = !tagged_desc_array.(!idx)
644 let has_tagged_foll t tag = is_node (tagged_foll t tag)
645 let has_tagged_desc t tag = is_node (tagged_desc t tag)
648 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
651 let contains_old t s =
652 let regexp = Str.regexp_string s in
655 let _ = Str.search_forward regexp arg 0;
659 let rec find t acc = match t.node with
662 if matching (string t) then DocIdSet.add i acc else acc
663 | Node(_) -> (find (left t )) ((find (right t)) acc)
665 find t DocIdSet.empty
668 let contains_iter t s =
669 let regexp = Str.regexp_string s in
672 let _ = Str.search_forward regexp arg 0;
676 let size = Text.size t.doc in
678 if n == size then acc
681 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
682 DocIdSet.add (Obj.magic n) acc
685 find DocIdSet.empty 0
690 let count_contains t s = Text.count_contains t.doc s
693 let count t s = text_count t.doc s
696 if is_root t then false
698 if tag (parent t) == Tag.pcdata then false
700 let u = left (parent t) in
703 let print_xml_fast outc t =
704 let rec loop ?(print_right=true) t =
707 | Text(i,n) -> output_string outc (get_cached_text t.doc i);
711 let tg = Tag.to_string (tag t) in
715 output_char outc '<';
716 output_string outc tg;
718 Nil -> output_string outc "/>"
719 | Node(_) when Tag.equal (tag l) Tag.attribute ->
720 (loop_attributes (left l);
721 match (right l).node with
722 | Nil -> output_string outc "/>"
724 output_char outc '>';
726 output_string outc "</";
727 output_string outc tg;
728 output_char outc '>' )
730 output_char outc '>';
732 output_string outc "</";
733 output_string outc tg;
735 );if print_right then loop r
736 and loop_attributes a =
740 match (left a).node with
741 | Text(i,_) -> (get_cached_text a.doc i)
744 output_char outc ' ';
745 output_string outc (Tag.to_string (tag a));
746 output_string outc "=\"";
747 output_string outc value;
748 output_char outc '"';
749 loop_attributes (right a)
752 loop ~print_right:false t
755 let print_xml_fast outc t =
756 if Tag.to_string (tag t) = "" then
757 print_xml_fast outc (first_child t)
758 else print_xml_fast outc t
761 let tags_below t tag =
762 fst(Hashtbl.find t.ttable tag)
764 let tags_after t tag =
765 snd(Hashtbl.find t.ttable tag)
767 let tags t tag = Hashtbl.find t.ttable tag
769 let tagged_lowest t tag =
770 let rec loop_lowest i =
771 let j = tree_tagged_desc t.doc i tag in
772 if tree_is_nil j then i else loop_lowest j
776 let j = loop_lowest i in
779 if tree_is_nil j then
780 if (tree_tag_id t.doc i) == tag
788 let tagged_next t tag =
791 let n = tree_tagged_foll_below t.doc i tag (Obj.magic 0)
793 if tree_is_nil n then mk_nil t
795 tagged_lowest { t with node = Node n } tag
799 let rec binary_parent t =
802 | Node(0) -> { t with node = Nil }
804 let j = tree_prev_sibling t.doc i in
805 if tree_is_nil j then
806 let idoc = tree_prev_text t.doc i in
807 if equal_node nil idoc then
808 { t with node = Node (tree_parent t.doc i) }
810 { t with node = Text(idoc,i) }
812 let idoc = tree_prev_text t.doc i in
813 if equal_node nil idoc then
814 { t with node = Node (j) }
815 else { t with node = Text(idoc,i) }
817 if tree_is_nil i then
818 let n = tree_parent_doc t.doc d in
819 let lc = tree_last_child t.doc n in
820 if tree_is_nil lc then {t with node = Node n }
821 else { t with node = Node lc }
823 let j = tree_prev_sibling t.doc i in
824 if tree_is_nil j then
825 { t with node = Node (tree_parent t.doc i) }
826 else { t with node = Node j }
828 in match res.node with
830 if (Array.length !contains_array) != 0
831 then if in_array !contains_array idoc then res
832 else binary_parent res
836 let benchmark_text t =
838 match (root t).node with
839 | Node i -> let _,size = tree_doc_ids doc i in
840 Printf.eprintf "%i will take ~ %i seconds\n%!"
842 let a = Array.create size "" in
845 a.(i) <- text_get_tc_text t.doc (i+1)
849 let doc_ids (t:t) : (int*int) =
852 | Node i -> tree_doc_ids t.doc i
853 | Text (i,_) -> (i,i)
857 let subtree_tags t tag = match t.node with
859 | Node(i) -> tree_subtree_tags t.doc i tag
860 | Text(_,i) -> tree_subtree_tags t.doc i tag