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
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.Int.hash s1
115 and y = Ptset.Int.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.Int.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.Int.hash s) in
130 | Not_found -> let r = Ptset.Int.add t s in
131 Hashtbl.add h_add k r;r
133 let h = Hashtbl.create 511 in
134 let sing = Ptset.Int.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.Int.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.Int.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
404 let select_next tb tf t s =
406 | Node (below) -> begin
409 { t with node = norm (tree_select_next t.doc n (Ptset.Int.to_int_vector tb) (Ptset.Int.to_int_vector tf) below) }
410 | Text (i,n) when equal_node nil n ->
411 let p = tree_parent_doc t.doc i in
412 { t with node = norm (tree_select_next t.doc p (Ptset.Int.to_int_vector tb) (Ptset.Int.to_int_vector tf) below) }
414 if Ptset.mem (tree_tag_id t.doc n) (Ptset.Int.union tb tf)
415 then { t with node=Node(n) }
417 let vb = Ptset.Int.to_int_vector tb in
418 let vf = Ptset.Int.to_int_vector tf in
420 let dsc = tree_select_below t.doc n vb vf in
421 if equal_node nil dsc
422 then tree_select_next t.doc n vb vf below
425 { t with node = norm node }
426 | _ -> {t with node = Nil }
429 | _ -> { t with node = Nil }
434 let select_foll_only tf t s =
440 { t with node= norm (tree_select_foll_only t.doc n (Ptset.Int.to_int_vector tf) below) }
441 | Text(i,n) when equal_node nil n ->
442 let p = tree_parent_doc t.doc i in
443 { t with node= norm (tree_select_foll_only t.doc p (Ptset.Int.to_int_vector tf) below) }
445 if Ptset.mem (tree_tag_id t.doc n) tf
446 then { t with node=Node(n) }
448 let vf = Ptset.Int.to_int_vector tf in
450 let dsc = tree_select_desc_only t.doc n vf in
452 then tree_select_foll_only t.doc n vf below
455 { t with node = norm node }
456 | _ -> { t with node = Nil }
458 | _ -> {t with node=Nil }
460 let select_below tc td t=
463 let vc = Ptset.Int.to_int_vector tc
465 let vd = Ptset.Int.to_int_vector td
467 { t with node= norm(tree_select_below t.doc n vc vd) }
468 | _ -> { t with node=Nil }
471 let select_desc_only td t =
474 let vd = Ptset.Int.to_int_vector td
476 { t with node = norm(tree_select_desc_only t.doc n vd) }
477 | _ -> { t with node = Nil }
480 let tagged_desc tag t =
483 { t with node = norm(tree_tagged_desc t.doc n tag) }
484 | _ -> { t with node = Nil }
487 let tagged_foll_ctx tag t s =
493 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
494 | Text(i,n) when equal_node nil n ->
495 let p = tree_prev_doc t.doc i in
496 { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
498 if (tree_tag_id t.doc n) == tag
499 then { t with node=Node(n) }
502 let dsc = tree_tagged_desc t.doc n tag in
504 then tree_tagged_foll_below t.doc n tag below
507 { t with node = norm node }
508 | _ -> { t with node = Nil }
510 | _ -> {t with node=Nil }
516 let array_find a i j =
517 let l = Array.length a in
518 let rec loop idx x y =
519 if x > y || idx >= l then nil
521 if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
522 else loop (idx+1) x y
524 if a.(0) > j || a.(l-1) < i then nil
525 else loop !last_idx i j
530 let l = Array.length !contains_array in
533 let i,j = tree_doc_ids t.doc n in
534 let id = if l == 0 then i else (array_find !contains_array i j)
536 (* Printf.printf "Looking for text below node %i with tag %s in range %i %i, in array : [|\n%!"
537 n (Tag.to_string (tree_tag_id t.doc n)) i j;
538 Array.iter (fun i -> Printf.printf "%i " (int_of_node i )) !contains_array;
539 Printf.printf "|]\nResult is %i\n%!" id; *)
543 { t with node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
544 | _ -> (*Printf.printf "Here\n%!"; *)
545 { t with node = Nil }
547 let text_next t root =
548 let l = Array.length !contains_array in
549 let inf = match t.node with
550 | Node(n) -> snd(tree_doc_ids t.doc n)+1
556 let _,j = tree_doc_ids t.doc n in
557 let id = if l == 0 then if inf > j then nil else inf
558 else array_find !contains_array inf j
560 if id == nil then { t with node= Nil }
562 { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
563 | _ -> { t with node = Nil}
567 let subtree_tags t tag =
569 { doc = d; node = Node(NC n) } ->
573 let select_desc_array = ref [| |]
576 let init_tagged_next t tagid =
577 let l = subtree_tags (root t) tagid
579 tagged_desc_array := Array.create l { t with node= Nil };
582 if is_node t then begin
583 if tag t == tagid then
585 !tagged_desc_array.(!i) <- t;
588 collect (first_child t);
589 collect (next_sibling t)
596 let pr x= Format.fprintf ppf x in
598 { node=Nil } -> pr "NULLT: -1"
599 | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
600 | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
604 (* let tagged_next t tag =
605 if !idx >= Array.length !tagged_desc_array
606 then {t with node=Nil}
608 let r = !tagged_desc_array.(!idx)
614 let has_tagged_foll t tag = is_node (tagged_foll t tag)
615 let has_tagged_desc t tag = is_node (tagged_desc t tag)
618 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
621 let contains_old t s =
622 let regexp = Str.regexp_string s in
625 let _ = Str.search_forward regexp arg 0;
629 let rec find t acc = match t.node with
632 if matching (string t) then DocIdSet.add i acc else acc
633 | Node(_) -> (find (left t )) ((find (right t)) acc)
635 find t DocIdSet.empty
638 let contains_iter t s =
639 let regexp = Str.regexp_string s in
642 let _ = Str.search_forward regexp arg 0;
646 let size = Text.size t.doc in
648 if n == size then acc
651 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
652 DocIdSet.add (Obj.magic n) acc
655 find DocIdSet.empty 0
660 let count_contains t s = Text.count_contains t.doc s
663 let count t s = text_count t.doc s
666 if is_root t then false
668 if tag (parent t) == Tag.pcdata then false
670 let u = left (parent t) in
673 let print_xml_fast outc t =
674 let rec loop ?(print_right=true) t =
677 | Text(i,n) -> output_string outc (get_cached_text t.doc i);
681 let tg = Tag.to_string (tag t) in
685 output_char outc '<';
686 output_string outc tg;
688 Nil -> output_string outc "/>"
689 | Node(_) when Tag.equal (tag l) Tag.attribute ->
690 (loop_attributes (left l);
691 match (right l).node with
692 | Nil -> output_string outc "/>"
694 output_char outc '>';
696 output_string outc "</";
697 output_string outc tg;
698 output_char outc '>' )
700 output_char outc '>';
702 output_string outc "</";
703 output_string outc tg;
705 );if print_right then loop r
706 and loop_attributes a =
710 match (left a).node with
711 | Text(i,_) -> (get_cached_text a.doc i)
714 output_char outc ' ';
715 output_string outc (Tag.to_string (tag a));
716 output_string outc "=\"";
717 output_string outc value;
718 output_char outc '"';
719 loop_attributes (right a)
722 loop ~print_right:false t
725 let print_xml_fast outc t =
726 if Tag.to_string (tag t) = "" then
727 print_xml_fast outc (first_child t)
728 else print_xml_fast outc t
731 let tags_below t tag =
732 fst(Hashtbl.find t.ttable tag)
734 let tags_after t tag =
735 snd(Hashtbl.find t.ttable tag)
737 let tags t tag = Hashtbl.find t.ttable tag
739 let tagged_lowest t tag =
740 let rec loop_lowest i =
741 let j = tree_tagged_desc t.doc i tag in
742 if tree_is_nil j then i else loop_lowest j
746 let j = loop_lowest i in
749 if tree_is_nil j then
750 if (tree_tag_id t.doc i) == tag
758 let tagged_next t tag =
761 let n = tree_tagged_foll_below t.doc i tag (Obj.magic 0)
763 if tree_is_nil n then mk_nil t
765 tagged_lowest { t with node = Node n } tag
769 let rec binary_parent t =
772 | Node(0) -> { t with node = Nil }
774 let j = tree_prev_sibling t.doc i in
775 if tree_is_nil j then
776 let idoc = tree_prev_text t.doc i in
777 if equal_node nil idoc then
778 { t with node = Node (tree_parent t.doc i) }
780 { t with node = Text(idoc,i) }
782 let idoc = tree_prev_text t.doc i in
783 if equal_node nil idoc then
784 { t with node = Node (j) }
785 else { t with node = Text(idoc,i) }
787 if tree_is_nil i then
788 let n = tree_parent_doc t.doc d in
789 let lc = tree_last_child t.doc n in
790 if tree_is_nil lc then {t with node = Node n }
791 else { t with node = Node lc }
793 let j = tree_prev_sibling t.doc i in
794 if tree_is_nil j then
795 { t with node = Node (tree_parent t.doc i) }
796 else { t with node = Node j }
798 in match res.node with
800 if (Array.length !contains_array) != 0
801 then if in_array !contains_array idoc then res
802 else binary_parent res
806 let benchmark_text t =
808 match (root t).node with
809 | Node i -> let _,size = tree_doc_ids doc i in
810 Printf.eprintf "%i will take ~ %i seconds\n%!"
812 let a = Array.create size "" in
815 a.(i) <- text_get_tc_text t.doc (i+1)
819 let doc_ids (t:t) : (int*int) =
822 | Node i -> tree_doc_ids t.doc i
823 | Text (i,_) -> (i,i)
827 let subtree_tags t tag = match t.node with
829 | Node(i) -> tree_subtree_tags t.doc i tag
830 | Text(_,i) -> tree_subtree_tags t.doc i tag
832 let get_text t = match t.node with
833 | Text(i,_) -> get_cached_text t.doc i