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_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
37 external get_cached_text : tree -> [`Text ] node -> string = "caml_text_collection_get_cached_text"
40 let text_get_text t n =
41 if equal_node nil n then ""
42 else get_cached_text t n
44 external text_size : tree -> int = "caml_text_collection_size"
45 external text_is_contains : tree -> string -> bool = "caml_text_collection_is_contains"
46 external text_count_contains : tree -> string -> int = "caml_text_collection_count_contains"
47 external text_count : tree -> string -> int = "caml_text_collection_count"
48 external text_contains : tree -> string -> [`Text ] node array = "caml_text_collection_contains"
51 external tree_serialize : tree -> string -> unit = "caml_xml_tree_serialize"
53 external tree_unserialize : string -> tree = "caml_xml_tree_unserialize"
55 external tree_root : tree -> [`Tree] node = "caml_xml_tree_root"
57 let tree_is_nil x = equal_node x nil
59 external tree_parent : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
60 external tree_parent_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
61 external tree_prev_doc : tree -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_prev_doc"
62 external tree_first_child : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
63 external tree_next_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
64 external tree_prev_sibling : tree -> [`Tree] node -> [`Tree] node = "caml_xml_tree_prev_sibling"
65 external tree_is_leaf : tree -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
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"
70 let tree_is_last t n = equal_node nil (tree_next_sibling t n)
72 external tree_prev_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
74 external tree_my_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
75 external tree_next_text : tree -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
76 external tree_doc_ids : tree -> [`Tree ] node -> [`Text ] node * [`Text ] node = "caml_xml_tree_doc_ids"
77 external tree_text_xml_id : tree -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
78 external tree_node_xml_id : tree -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
79 external tree_is_ancestor : tree -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
80 external tree_tagged_desc : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc"
81 external tree_tagged_foll_below : tree -> [`Tree ] node -> Tag.t -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_below"
82 external tree_subtree_tags : tree -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags"
83 external tree_select_below : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_below"
84 external tree_select_desc_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_select_desc_only"
85 external tree_select_next : tree -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_next"
86 external tree_select_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only"
87 external tree_select_desc_or_foll_only : tree -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_select_foll_only"
91 | Node of [`Tree] node
92 | Text of [`Text] node * [`Tree] node
94 type t = { doc : tree;
96 ttable : (Tag.t,(Ptset.t*Ptset.t)) Hashtbl.t;
100 let update h t sb sa =
105 | Not_found -> Ptset.empty,Ptset.empty
107 Hashtbl.replace h t (Ptset.union sbelow sb, Ptset.union safter sa)
113 let collect_tags tree =
114 let h = Hashtbl.create 511 in
115 let rec loop id acc =
117 then (Ptset.singleton Tag.pcdata, Ptset.add Tag.pcdata acc)
119 let below2,after2 = loop (tree_next_sibling tree id) acc in
120 let below1,after1 = loop (tree_first_child tree id) after2 in
121 let tag = tree_tag_id tree id in
122 update h tag below1 after2;
123 Ptset.add tag (Ptset.union below1 below2), (Ptset.add tag after1)
125 let b,a = loop (tree_root tree) Ptset.empty in
126 update h Tag.pcdata b a;
131 let contains_array = ref [| |]
133 let init_contains t s =
134 let a = text_contains t.doc s
136 Array.fast_sort (compare) a;
139 let init_naive_contains t s =
140 let i,j = tree_doc_ids t.doc (tree_root t.doc)
142 let regexp = Str.regexp_string s in
145 let _ = Str.search_forward regexp arg 0;
149 let rec loop n acc l =
152 let s = text_get_text t.doc n
155 then loop (n+1) (n::acc) (l+1)
156 else loop (n+1) acc l
158 let acc,l = loop i [] 0 in
159 let a = Array.create l nil in
160 let _ = List.fold_left (fun cpt e -> a.(cpt) <- e; (cpt-1)) (l-1) acc
166 module DocIdSet = struct
167 include Set.Make (struct type t = [`Text] node
168 let compare = compare_node end)
171 let is_nil t = t.node == Nil
173 let is_node t = t.node != Nil
176 let _ = Tag.init (Obj.magic t) in
177 let table = collect_tags t
180 let _ = Hashtbl.iter (fun t (sb,sa) ->
181 Printf.eprintf "'%s' -> { " (Tag.to_string t);
182 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sb;
183 Printf.eprintf "}\n { ";
184 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sa;
185 Printf.eprintf "} \n----------------------------------\n";
189 node = Node(tree_root t);
194 let parse_xml_uri str = node_of_t
196 !Options.sample_factor
197 !Options.index_empty_texts
198 !Options.disable_text_collection)
200 let parse_xml_string str = node_of_t
201 (parse_xml_string str
202 !Options.sample_factor
203 !Options.index_empty_texts
204 !Options.disable_text_collection)
206 external pool : tree -> Tag.pool = "%identity"
207 let save t str = save_tree t.doc str
208 let load ?(sample=64) str =
209 node_of_t (load_tree str sample)
214 let tag_pool t = pool t.doc
216 let compare a b = match a.node,b.node with
220 | Node(i),Node(j) -> compare_node i j
221 | Text(i,_), Text(j,_) -> compare_node i j
222 | Node(i), Text(_,j) -> compare_node i j
223 | Text(_,i), Node(j) -> compare_node i j
225 let equal a b = (compare a b) == 0
228 let norm (n : [`Tree ] node ) = if n == -1 then Nil else Node (n)
232 | Text (i,j) -> Printf.sprintf "Text (%i, %i)" i j
233 | Node (i) -> Printf.sprintf "Node (%i)" i
235 let mk_nil t = { t with node = Nil }
236 let root n = { n with node = norm (tree_root n.doc) }
238 let is_root n = match n.node with
239 | Node(t) -> (int_of_node t) == 0
244 match n.node with (* inlined parent *)
245 | Node(t) when (int_of_node t)== 0 -> Nil
247 let txt = tree_prev_text n.doc t in
248 if text_is_empty n.doc txt then
249 let ps = tree_prev_sibling n.doc t in
252 Node(tree_parent n.doc t)
257 let ps = tree_prev_doc n.doc i in
259 then Node (tree_parent_doc n.doc i)
261 | _ -> failwith "parent"
263 { n with node = node' }
267 | Node i -> { n with node= norm(tree_first_child n.doc i) }
268 | _ -> { n with node = Nil }
272 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
273 | _ -> { n with node = Nil }
275 let node_sibling_ctx n _ =
277 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
278 | _ -> { n with node = Nil }
285 let fs = tree_first_child n.doc t in
288 let txt = tree_my_text n.doc t in
289 if equal_node nil txt
293 let txt = tree_prev_text n.doc fs in
294 if equal_node nil txt
298 | Nil -> failwith "first_child"
300 { n with node = node'}
305 | Text (_,ns) -> norm ns
307 let ns = tree_next_sibling n.doc t in
308 let txt = tree_next_text n.doc t in
309 if equal_node nil txt
312 | Nil -> failwith "next_sibling"
314 { n with node = node'}
316 let next_sibling_ctx n _ = next_sibling n
318 let left = first_child
319 let right = next_sibling
323 | Node(n) -> tree_node_xml_id t.doc n
324 | Text(i,_) -> tree_text_xml_id t.doc i
329 | Text(_) -> Tag.pcdata
330 | Node(n) -> tree_tag_id t.doc n
331 | _ -> failwith "tag"
334 let string_below t id =
335 let strid = parent_doc t.doc id in
338 (Tree.equal i strid) || (is_ancestor t.doc i strid)
339 | Node(SC(i,_)) -> Text.equal i id
343 let tagged_foll t tag =
344 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
346 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
347 | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil }
348 | { doc=d; node=Node(SC (_,n)) } ->
350 if tag_id d n == tag then n
352 let n' = tagged_desc d n tag in
353 if is_nil n' then tagged_foll d n tag
355 in {t with node= norm nnode}
356 | _ -> { t with node=Nil }
359 let tagged_desc t tag =
360 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
362 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
363 | _ -> { t with node=Nil }
366 let select_next tb tf t s =
368 | Node (below) -> begin
371 { t with node = norm (tree_select_next t.doc n (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
372 | Text (i,n) when equal_node nil n ->
373 let p = tree_parent_doc t.doc i in
374 { t with node = norm (tree_select_next t.doc p (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
376 if Ptset.mem (tree_tag_id t.doc n) (Ptset.union tb tf)
377 then { t with node=Node(n) }
379 let vb = Ptset.to_int_vector tb in
380 let vf = Ptset.to_int_vector tf in
382 let dsc = tree_select_below t.doc n vb vf in
383 if equal_node nil dsc
384 then tree_select_next t.doc n vb vf below
387 { t with node = norm node }
388 | _ -> {t with node = Nil }
391 | _ -> { t with node = Nil }
396 let select_foll_only tf t s =
402 { t with node= norm (tree_select_foll_only t.doc n (Ptset.to_int_vector tf) below) }
403 | Text(i,n) when equal_node nil n ->
404 let p = tree_parent_doc t.doc i in
405 { t with node= norm (tree_select_foll_only t.doc p (Ptset.to_int_vector tf) below) }
407 if Ptset.mem (tree_tag_id t.doc n) tf
408 then { t with node=Node(n) }
410 let vf = Ptset.to_int_vector tf in
412 let dsc = tree_select_desc_only t.doc n vf in
414 then tree_select_foll_only t.doc n vf below
417 { t with node = norm node }
418 | _ -> { t with node = Nil }
420 | _ -> {t with node=Nil }
422 let select_below tc td t=
425 let vc = Ptset.to_int_vector tc
427 let vd = Ptset.to_int_vector td
429 { t with node= norm(tree_select_below t.doc n vc vd) }
430 | _ -> { t with node=Nil }
433 let select_desc_only td t =
436 let vd = Ptset.to_int_vector td
438 { t with node = norm(tree_select_desc_only t.doc n vd) }
439 | _ -> { t with node = Nil }
442 let tagged_desc tag t =
445 { t with node = norm(tree_tagged_desc t.doc n tag) }
446 | _ -> { t with node = Nil }
449 let tagged_foll_below tag t s =
455 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
456 | Text(i,n) when equal_node nil n ->
457 let p = tree_prev_doc t.doc i in
458 { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
460 if (tree_tag_id t.doc n) == tag
461 then { t with node=Node(n) }
464 let dsc = tree_tagged_desc t.doc n tag in
466 then tree_tagged_foll_below t.doc n tag below
469 { t with node = norm node }
470 | _ -> { t with node = Nil }
472 | _ -> {t with node=Nil }
476 let array_find a i j =
477 let l = Array.length a in
478 let rec loop idx x y =
479 if x > y || idx >= l then nil
481 if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
482 else loop (idx+1) x y
484 if a.(0) > j || a.(l-1) < i then nil
485 else loop !last_idx i j
490 let l = Array.length !contains_array in
491 if l = 0 then { t with node=Nil }
495 let i,j = tree_doc_ids t.doc n in
496 let id = array_find !contains_array i j
501 { t with node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
502 | _ -> { t with node = Nil }
504 let text_next t root =
505 let l = Array.length !contains_array in
506 if l = 0 then { t with node=Nil }
508 let inf = match t.node with
509 | Node(n) -> snd(tree_doc_ids t.doc n)+1
515 let _,j = tree_doc_ids t.doc n in
516 let id = array_find !contains_array inf j
518 if id == nil then { t with node= Nil }
520 { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
521 | _ -> { t with node = Nil}
525 let subtree_tags t tag =
527 { doc = d; node = Node(NC n) } ->
531 let select_desc_array = ref [| |]
534 let init_tagged_next t tagid =
535 let l = subtree_tags (root t) tagid
537 tagged_desc_array := Array.create l { t with node= Nil };
540 if is_node t then begin
541 if tag t == tagid then
543 !tagged_desc_array.(!i) <- t;
546 collect (first_child t);
547 collect (next_sibling t)
554 let pr x= Format.fprintf ppf x in
556 { node=Nil } -> pr "NULLT: -1"
557 | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
558 | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
562 (* let tagged_next t tag =
563 if !idx >= Array.length !tagged_desc_array
564 then {t with node=Nil}
566 let r = !tagged_desc_array.(!idx)
572 let has_tagged_foll t tag = is_node (tagged_foll t tag)
573 let has_tagged_desc t tag = is_node (tagged_desc t tag)
576 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
579 let contains_old t s =
580 let regexp = Str.regexp_string s in
583 let _ = Str.search_forward regexp arg 0;
587 let rec find t acc = match t.node with
590 if matching (string t) then DocIdSet.add i acc else acc
591 | Node(_) -> (find (left t )) ((find (right t)) acc)
593 find t DocIdSet.empty
596 let contains_iter t s =
597 let regexp = Str.regexp_string s in
600 let _ = Str.search_forward regexp arg 0;
604 let size = Text.size t.doc in
606 if n == size then acc
609 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
610 DocIdSet.add (Obj.magic n) acc
613 find DocIdSet.empty 0
618 let count_contains t s = Text.count_contains t.doc s
621 let count t s = text_count t.doc s
624 if is_root t then false
626 if tag (parent t) == Tag.pcdata then false
628 let u = left (parent t) in
631 let print_xml_fast outc t =
632 let rec loop ?(print_right=true) t =
635 | Text(i,n) -> output_string outc (text_get_text t.doc i);
639 let tg = Tag.to_string (tag t) in
643 output_char outc '<';
644 output_string outc tg;
646 Nil -> output_string outc "/>"
647 | Node(_) when Tag.equal (tag l) Tag.attribute ->
648 (loop_attributes (left l);
649 match (right l).node with
650 | Nil -> output_string outc "/>"
652 output_char outc '>';
654 output_string outc "</";
655 output_string outc tg;
656 output_char outc '>' )
658 output_char outc '>';
660 output_string outc "</";
661 output_string outc tg;
663 );if print_right then loop r
664 and loop_attributes a =
668 match (left a).node with
669 | Text(i,_) -> text_get_text a.doc i
672 output_char outc ' ';
673 output_string outc (Tag.to_string (tag a));
674 output_string outc "=\"";
675 output_string outc value;
676 output_char outc '"';
677 loop_attributes (right a)
680 loop ~print_right:false t
683 let print_xml_fast outc t =
684 if Tag.to_string (tag t) = "" then
685 print_xml_fast outc (first_child t)
686 else print_xml_fast outc t
689 let tags_below t tag =
690 fst(Hashtbl.find t.ttable tag)
692 let tags_after t tag =
693 snd(Hashtbl.find t.ttable tag)
695 let tags t tag = Hashtbl.find t.ttable tag