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 (******************************************************************************)
7 (*INCLUDE "debug.ml" *)
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;
103 let update h t sb sa =
108 | Not_found -> Ptset.empty,Ptset.empty
110 Hashtbl.replace h t (Ptset.union sbelow sb, Ptset.union safter sa)
114 let text_size t = text_size t.doc
116 let collect_tags tree =
117 let h = Hashtbl.create 511 in
118 let rec loop id acc =
120 then (Ptset.singleton Tag.pcdata, Ptset.add Tag.pcdata acc)
122 let below2,after2 = loop (tree_next_sibling tree id) acc in
123 let below1,after1 = loop (tree_first_child tree id) after2 in
124 let tag = tree_tag_id tree id in
125 update h tag below1 after2;
126 Ptset.add tag (Ptset.union below1 below2), (Ptset.add tag after1)
128 let b,a = loop (tree_root tree) Ptset.empty in
129 update h Tag.pcdata b a;
136 let contains_array = ref [| |]
137 let contains_index = Hashtbl.create 4096
140 Hashtbl.find contains_index i
144 let init_contains t s =
145 let a = text_contains t.doc s
147 Array.fast_sort (compare) a;
149 Array.iter (fun x -> Hashtbl.add contains_index x true) !contains_array
151 let count_contains t s = text_count_contains t.doc s
152 let unsorted_contains t s = text_unsorted_contains t.doc s
154 let init_naive_contains t s =
155 let i,j = tree_doc_ids t.doc (tree_root t.doc)
157 let regexp = Str.regexp_string s in
160 let _ = Str.search_forward regexp arg 0;
164 let rec loop n acc l =
167 let s = get_cached_text t.doc n
170 then loop (n+1) (n::acc) (l+1)
171 else loop (n+1) acc l
173 let acc,l = loop i [] 0 in
174 let a = Array.create l nil in
175 let _ = List.fold_left (fun cpt e -> a.(cpt) <- e; (cpt-1)) (l-1) acc
181 module DocIdSet = struct
182 include Set.Make (struct type t = [`Text] node
183 let compare = compare_node end)
186 let is_nil t = t.node == Nil
188 let is_node t = t.node != Nil
191 let _ = Tag.init (Obj.magic t) in
192 let table = collect_tags t
195 let _ = Hashtbl.iter (fun t (sb,sa) ->
196 Printf.eprintf "'%s' -> { " (Tag.to_string t);
197 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sb;
198 Printf.eprintf "}\n { ";
199 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sa;
200 Printf.eprintf "} \n----------------------------------\n";
202 let i,j = tree_doc_ids t (tree_root t) in
203 Printf.eprintf "%i docs, range from %i to %i\n%!" (Array.length s) i j;
204 Array.iter (fun i -> print_endline (">>>" ^ i ^ "<<<")) s; *)
206 node = Node(tree_root t);
209 let finalize _ = Printf.eprintf "Release the string list !\n%!"
215 !Options.sample_factor
216 !Options.index_empty_texts
217 !Options.disable_text_collection)
219 let parse_xml_uri str = parse parse_xml_uri str
220 let parse_xml_string str = parse parse_xml_string str
223 external pool : tree -> Tag.pool = "%identity"
225 let save t str = (save_tree t.doc str)
228 let load ?(sample=64) str =
229 node_of_t (load_tree str sample)
234 let tag_pool t = pool t.doc
236 let compare a b = match a.node,b.node with
240 | Node(i),Node(j) -> compare_node i j
241 | Text(i,_), Text(j,_) -> compare_node i j
242 | Node(i), Text(_,j) -> compare_node i j
243 | Text(_,i), Node(j) -> compare_node i j
245 let equal a b = (compare a b) == 0
248 let norm (n : [`Tree ] node ) = if n == -1 then Nil else Node (n)
252 | Text (i,j) -> Printf.sprintf "Text (%i, %i)" i j
253 | Node (i) -> Printf.sprintf "Node (%i)" i
255 let dump_node t = nts t.node
257 let mk_nil t = { t with node = Nil }
258 let root n = { n with node = norm (tree_root n.doc) }
260 let is_root n = match n.node with
261 | Node(t) -> (int_of_node t) == 0
264 let is_left n = match n.node with
265 | Node(t) -> (tree_is_first_child n.doc t) && (equal_node nil (tree_prev_text n.doc t))
266 | Text(_,t) -> tree_is_nil t || tree_is_first_child n.doc t
269 let is_below_right t1 t2 =
270 match (t1.node,t2.node) with
271 | Nil,_ | _,Nil -> false
272 | Node(i1), Node(i2) ->
273 tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2
274 && not (tree_is_ancestor t1.doc i1 i2)
275 | Text(_,i1),Node(i2) -> i1 == i2 ||
276 (tree_is_ancestor t1.doc (tree_parent t1.doc i1) i2 && i1 < i2)
277 | Text(_,i1),Text(i,_) ->
278 let x,y = tree_doc_ids t1.doc i1 in
280 | Node(i1), Text(i,_) ->
281 let i2 = tree_next_sibling t1.doc i1 in
282 let x,y = tree_doc_ids t1.doc i2 in
287 match n.node with (* inlined parent *)
288 | Node(t) when (int_of_node t)== 0 -> Nil
290 let txt = tree_prev_text n.doc t in
291 if text_is_empty n.doc txt then
292 let ps = tree_prev_sibling n.doc t in
295 Node(tree_parent n.doc t)
300 let ps = tree_prev_doc n.doc i in
302 then Node (tree_parent_doc n.doc i)
304 | _ -> failwith "parent"
306 { n with node = node' }
310 | Node i -> { n with node= norm(tree_first_child n.doc i) }
311 | _ -> { n with node = Nil }
315 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
316 | _ -> { n with node = Nil }
318 let node_sibling_ctx n _ =
320 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
321 | _ -> { n with node = Nil }
328 let fs = tree_first_child n.doc t in
331 let txt = tree_my_text n.doc t in
332 if equal_node nil txt
336 let txt = tree_prev_text n.doc fs in
337 if equal_node nil txt
341 | Nil -> failwith "first_child"
343 { n with node = node'}
348 | Text (_,ns) -> norm ns
350 let ns = tree_next_sibling n.doc t in
351 let txt = tree_next_text n.doc t in
352 if equal_node nil txt
355 | Nil -> failwith "next_sibling"
357 { n with node = node'}
359 let next_sibling_ctx n _ = next_sibling n
361 let left = first_child
362 let right = next_sibling
366 | Node(n) -> tree_node_xml_id t.doc n
367 | Text(i,_) -> tree_text_xml_id t.doc i
372 | Text(_) -> Tag.pcdata
373 | Node(n) -> tree_tag_id t.doc n
374 | _ -> failwith "tag"
377 let string_below t id =
378 let strid = parent_doc t.doc id in
381 (Tree.equal i strid) || (is_ancestor t.doc i strid)
382 | Node(SC(i,_)) -> Text.equal i id
386 let tagged_foll t tag =
387 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
389 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
390 | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil }
391 | { doc=d; node=Node(SC (_,n)) } ->
393 if tag_id d n == tag then n
395 let n' = tagged_desc d n tag in
396 if is_nil n' then tagged_foll d n tag
398 in {t with node= norm nnode}
399 | _ -> { t with node=Nil }
402 let tagged_desc t tag =
403 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
405 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
406 | _ -> { t with node=Nil }
409 let select_next tb tf t s =
411 | Node (below) -> begin
414 { t with node = norm (tree_select_next t.doc n (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
415 | Text (i,n) when equal_node nil n ->
416 let p = tree_parent_doc t.doc i in
417 { t with node = norm (tree_select_next t.doc p (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
419 if Ptset.mem (tree_tag_id t.doc n) (Ptset.union tb tf)
420 then { t with node=Node(n) }
422 let vb = Ptset.to_int_vector tb in
423 let vf = Ptset.to_int_vector tf in
425 let dsc = tree_select_below t.doc n vb vf in
426 if equal_node nil dsc
427 then tree_select_next t.doc n vb vf below
430 { t with node = norm node }
431 | _ -> {t with node = Nil }
434 | _ -> { t with node = Nil }
439 let select_foll_only tf t s =
445 { t with node= norm (tree_select_foll_only t.doc n (Ptset.to_int_vector tf) below) }
446 | Text(i,n) when equal_node nil n ->
447 let p = tree_parent_doc t.doc i in
448 { t with node= norm (tree_select_foll_only t.doc p (Ptset.to_int_vector tf) below) }
450 if Ptset.mem (tree_tag_id t.doc n) tf
451 then { t with node=Node(n) }
453 let vf = Ptset.to_int_vector tf in
455 let dsc = tree_select_desc_only t.doc n vf in
457 then tree_select_foll_only t.doc n vf below
460 { t with node = norm node }
461 | _ -> { t with node = Nil }
463 | _ -> {t with node=Nil }
465 let select_below tc td t=
468 let vc = Ptset.to_int_vector tc
470 let vd = Ptset.to_int_vector td
472 { t with node= norm(tree_select_below t.doc n vc vd) }
473 | _ -> { t with node=Nil }
476 let select_desc_only td t =
479 let vd = Ptset.to_int_vector td
481 { t with node = norm(tree_select_desc_only t.doc n vd) }
482 | _ -> { t with node = Nil }
485 let tagged_desc tag t =
488 { t with node = norm(tree_tagged_desc t.doc n tag) }
489 | _ -> { t with node = Nil }
492 let tagged_foll_below tag t s =
498 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
499 | Text(i,n) when equal_node nil n ->
500 let p = tree_prev_doc t.doc i in
501 { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
503 if (tree_tag_id t.doc n) == tag
504 then { t with node=Node(n) }
507 let dsc = tree_tagged_desc t.doc n tag in
509 then tree_tagged_foll_below t.doc n tag below
512 { t with node = norm node }
513 | _ -> { t with node = Nil }
515 | _ -> {t with node=Nil }
519 let array_find a i j =
520 let l = Array.length a in
521 let rec loop idx x y =
522 if x > y || idx >= l then nil
524 if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
525 else loop (idx+1) x y
527 if a.(0) > j || a.(l-1) < i then nil
528 else loop !last_idx i j
533 let l = Array.length !contains_array in
536 let i,j = tree_doc_ids t.doc n in
537 let id = if l == 0 then i else (array_find !contains_array i j)
539 (* Printf.printf "Looking for text below node %i with tag %s in range %i %i, in array : [|\n%!"
540 n (Tag.to_string (tree_tag_id t.doc n)) i j;
541 Array.iter (fun i -> Printf.printf "%i " (int_of_node i )) !contains_array;
542 Printf.printf "|]\nResult is %i\n%!" id; *)
546 { t with node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
547 | _ -> (*Printf.printf "Here\n%!"; *)
548 { t with node = Nil }
550 let text_next t root =
551 let l = Array.length !contains_array in
552 let inf = match t.node with
553 | Node(n) -> snd(tree_doc_ids t.doc n)+1
559 let _,j = tree_doc_ids t.doc n in
560 let id = if l == 0 then if inf > j then nil else inf
561 else array_find !contains_array inf j
563 if id == nil then { t with node= Nil }
565 { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
566 | _ -> { t with node = Nil}
570 let subtree_tags t tag =
572 { doc = d; node = Node(NC n) } ->
576 let select_desc_array = ref [| |]
579 let init_tagged_next t tagid =
580 let l = subtree_tags (root t) tagid
582 tagged_desc_array := Array.create l { t with node= Nil };
585 if is_node t then begin
586 if tag t == tagid then
588 !tagged_desc_array.(!i) <- t;
591 collect (first_child t);
592 collect (next_sibling t)
599 let pr x= Format.fprintf ppf x in
601 { node=Nil } -> pr "NULLT: -1"
602 | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
603 | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
607 (* let tagged_next t tag =
608 if !idx >= Array.length !tagged_desc_array
609 then {t with node=Nil}
611 let r = !tagged_desc_array.(!idx)
617 let has_tagged_foll t tag = is_node (tagged_foll t tag)
618 let has_tagged_desc t tag = is_node (tagged_desc t tag)
621 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
624 let contains_old t s =
625 let regexp = Str.regexp_string s in
628 let _ = Str.search_forward regexp arg 0;
632 let rec find t acc = match t.node with
635 if matching (string t) then DocIdSet.add i acc else acc
636 | Node(_) -> (find (left t )) ((find (right t)) acc)
638 find t DocIdSet.empty
641 let contains_iter t s =
642 let regexp = Str.regexp_string s in
645 let _ = Str.search_forward regexp arg 0;
649 let size = Text.size t.doc in
651 if n == size then acc
654 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
655 DocIdSet.add (Obj.magic n) acc
658 find DocIdSet.empty 0
663 let count_contains t s = Text.count_contains t.doc s
666 let count t s = text_count t.doc s
669 if is_root t then false
671 if tag (parent t) == Tag.pcdata then false
673 let u = left (parent t) in
676 let print_xml_fast outc t =
677 let rec loop ?(print_right=true) t =
680 | Text(i,n) -> output_string outc (get_cached_text t.doc i);
684 let tg = Tag.to_string (tag t) in
688 output_char outc '<';
689 output_string outc tg;
691 Nil -> output_string outc "/>"
692 | Node(_) when Tag.equal (tag l) Tag.attribute ->
693 (loop_attributes (left l);
694 match (right l).node with
695 | Nil -> output_string outc "/>"
697 output_char outc '>';
699 output_string outc "</";
700 output_string outc tg;
701 output_char outc '>' )
703 output_char outc '>';
705 output_string outc "</";
706 output_string outc tg;
708 );if print_right then loop r
709 and loop_attributes a =
713 match (left a).node with
714 | Text(i,_) -> (get_cached_text a.doc i)
717 output_char outc ' ';
718 output_string outc (Tag.to_string (tag a));
719 output_string outc "=\"";
720 output_string outc value;
721 output_char outc '"';
722 loop_attributes (right a)
725 loop ~print_right:false t
728 let print_xml_fast outc t =
729 if Tag.to_string (tag t) = "" then
730 print_xml_fast outc (first_child t)
731 else print_xml_fast outc t
734 let tags_below t tag =
735 fst(Hashtbl.find t.ttable tag)
737 let tags_after t tag =
738 snd(Hashtbl.find t.ttable tag)
740 let tags t tag = Hashtbl.find t.ttable tag
742 let tagged_lowest t tag =
743 let rec loop_lowest i =
744 let j = tree_tagged_desc t.doc i tag in
745 if tree_is_nil j then i else loop_lowest j
749 let j = loop_lowest i in
752 if tree_is_nil j then
753 if (tree_tag_id t.doc i) == tag
761 let tagged_next t tag =
764 let n = tree_tagged_foll_below t.doc i tag (Obj.magic 0)
766 if tree_is_nil n then mk_nil t
768 tagged_lowest { t with node = Node n } tag
772 let rec binary_parent t =
775 | Node(0) -> { t with node = Nil }
777 let j = tree_prev_sibling t.doc i in
778 if tree_is_nil j then
779 let idoc = tree_prev_text t.doc i in
780 if equal_node nil idoc then
781 { t with node = Node (tree_parent t.doc i) }
783 { t with node = Text(idoc,i) }
785 let idoc = tree_prev_text t.doc i in
786 if equal_node nil idoc then
787 { t with node = Node (j) }
788 else { t with node = Text(idoc,i) }
790 if tree_is_nil i then
791 let n = tree_parent_doc t.doc d in
792 let lc = tree_last_child t.doc n in
793 if tree_is_nil lc then {t with node = Node n }
794 else { t with node = Node lc }
796 let j = tree_prev_sibling t.doc i in
797 if tree_is_nil j then
798 { t with node = Node (tree_parent t.doc i) }
799 else { t with node = Node j }
801 in match res.node with
803 if (Array.length !contains_array) != 0
804 then if in_array !contains_array idoc then res
805 else binary_parent res
809 let benchmark_text t =
811 match (root t).node with
812 | Node i -> let _,size = tree_doc_ids doc i in
813 Printf.eprintf "%i will take ~ %i seconds\n%!"
815 let a = Array.create size "" in
818 a.(i) <- text_get_tc_text t.doc (i+1)
822 let doc_ids (t:t) : (int*int) =
825 | Node i -> tree_doc_ids t.doc i
826 | Text (i,_) -> (i,i)