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 = match t.node with
173 | Node(i) -> equal_node i nil
178 | Node(i) -> not(equal_node i nil)
183 let _ = Tag.init (Obj.magic t) in
184 let table = collect_tags t
187 let _ = Hashtbl.iter (fun t (sb,sa) ->
188 Printf.eprintf "'%s' -> { " (Tag.to_string t);
189 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sb;
190 Printf.eprintf "}\n { ";
191 Ptset.iter (fun i -> Printf.eprintf "'%s' " (Tag.to_string i)) sa;
192 Printf.eprintf "} \n----------------------------------\n";
196 node = Node(tree_root t);
201 let parse_xml_uri str = node_of_t
203 !Options.sample_factor
204 !Options.index_empty_texts
205 !Options.disable_text_collection)
207 let parse_xml_string str = node_of_t
208 (parse_xml_string str
209 !Options.sample_factor
210 !Options.index_empty_texts
211 !Options.disable_text_collection)
213 external pool : tree -> Tag.pool = "%identity"
214 let save t str = save_tree t.doc str
215 let load ?(sample=64) str =
216 node_of_t (load_tree str sample)
221 let tag_pool t = pool t.doc
223 let compare a b = match a.node,b.node with
227 | Node(i),Node(j) -> compare_node i j
228 | Text(i,_), Text(j,_) -> compare_node i j
229 | Node(i), Text(_,j) -> compare_node i j
230 | Text(_,i), Node(j) -> compare_node i j
232 let equal a b = (compare a b) == 0
235 let norm (n : [`Tree ] node ) = if tree_is_nil n then Nil else Node (n)
239 | Text (i,j) -> Printf.sprintf "Text (%i, %i)" i j
240 | Node (i) -> Printf.sprintf "Node (%i)" i
242 let mk_nil t = { t with node = Nil }
243 let root n = { n with node = norm (tree_root n.doc) }
245 let is_root n = match n.node with
246 | Node(t) -> (int_of_node t) == 0
251 match n.node with (* inlined parent *)
252 | Node(t) when (int_of_node t)== 0 -> Nil
254 let txt = tree_prev_text n.doc t in
255 if text_is_empty n.doc txt then
256 let ps = tree_prev_sibling n.doc t in
259 Node(tree_parent n.doc t)
264 let ps = tree_prev_doc n.doc i in
266 then Node (tree_parent_doc n.doc i)
268 | _ -> failwith "parent"
270 { n with node = node' }
274 | Node i -> { n with node= norm(tree_first_child n.doc i) }
275 | _ -> { n with node = Nil }
279 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
280 | _ -> { n with node = Nil }
282 let node_sibling_ctx n _ =
284 | Node i -> { n with node= norm(tree_next_sibling n.doc i) }
285 | _ -> { n with node = Nil }
292 let fs = tree_first_child n.doc t in
295 let txt = tree_my_text n.doc t in
296 if equal_node nil txt
300 let txt = tree_prev_text n.doc fs in
301 if equal_node nil txt
305 | Nil -> failwith "first_child"
307 { n with node = node'}
312 | Text (_,ns) -> norm ns
314 let ns = tree_next_sibling n.doc t in
315 let txt = tree_next_text n.doc t in
316 if equal_node nil txt
319 | Nil -> failwith "next_sibling"
321 { n with node = node'}
323 let next_sibling_ctx n _ = next_sibling n
325 let left = first_child
326 let right = next_sibling
330 | Node(n) -> tree_node_xml_id t.doc n
331 | Text(i,_) -> tree_text_xml_id t.doc i
336 | Text(_) -> Tag.pcdata
337 | Node(n) -> tree_tag_id t.doc n
338 | _ -> failwith "tag"
341 let string_below t id =
342 let strid = parent_doc t.doc id in
345 (Tree.equal i strid) || (is_ancestor t.doc i strid)
346 | Node(SC(i,_)) -> Text.equal i id
350 let tagged_foll t tag =
351 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
353 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
354 | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil }
355 | { doc=d; node=Node(SC (_,n)) } ->
357 if tag_id d n == tag then n
359 let n' = tagged_desc d n tag in
360 if is_nil n' then tagged_foll d n tag
362 in {t with node= norm nnode}
363 | _ -> { t with node=Nil }
366 let tagged_desc t tag =
367 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
369 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
370 | _ -> { t with node=Nil }
373 let select_next tb tf t s =
375 | Node (below) -> begin
378 { t with node = norm (tree_select_next t.doc n (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
379 | Text (i,n) when equal_node nil n ->
380 let p = tree_parent_doc t.doc i in
381 { t with node = norm (tree_select_next t.doc p (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
383 if Ptset.mem (tree_tag_id t.doc n) (Ptset.union tb tf)
384 then { t with node=Node(n) }
386 let vb = Ptset.to_int_vector tb in
387 let vf = Ptset.to_int_vector tf in
389 let dsc = tree_select_below t.doc n vb vf in
390 if equal_node nil dsc
391 then tree_select_next t.doc n vb vf below
394 { t with node = norm node }
395 | _ -> {t with node = Nil }
398 | _ -> { t with node = Nil }
403 let select_foll_only tf t s =
409 { t with node= norm (tree_select_foll_only t.doc n (Ptset.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_foll_only t.doc p (Ptset.to_int_vector tf) below) }
414 if Ptset.mem (tree_tag_id t.doc n) tf
415 then { t with node=Node(n) }
417 let vf = Ptset.to_int_vector tf in
419 let dsc = tree_select_desc_only t.doc n vf in
421 then tree_select_foll_only t.doc n vf below
424 { t with node = norm node }
425 | _ -> { t with node = Nil }
427 | _ -> {t with node=Nil }
429 let select_below tc td t=
432 let vc = Ptset.to_int_vector tc
434 let vd = Ptset.to_int_vector td
436 { t with node= norm(tree_select_below t.doc n vc vd) }
437 | _ -> { t with node=Nil }
440 let select_desc_only td t =
443 let vd = Ptset.to_int_vector td
445 { t with node = norm(tree_select_desc_only t.doc n vd) }
446 | _ -> { t with node = Nil }
449 let tagged_desc tag t =
452 { t with node = norm(tree_tagged_desc t.doc n tag) }
453 | _ -> { t with node = Nil }
456 let tagged_foll_below tag t s =
462 { t with node= norm (tree_tagged_foll_below t.doc n tag below) }
463 | Text(i,n) when equal_node nil n ->
464 let p = tree_prev_doc t.doc i in
465 { t with node= norm (tree_tagged_foll_below t.doc p tag below) }
467 if (tree_tag_id t.doc n) == tag
468 then { t with node=Node(n) }
471 let dsc = tree_tagged_desc t.doc n tag in
473 then tree_tagged_foll_below t.doc n tag below
476 { t with node = norm node }
477 | _ -> { t with node = Nil }
479 | _ -> {t with node=Nil }
483 let array_find a i j =
484 let l = Array.length a in
485 let rec loop idx x y =
486 if x > y || idx >= l then nil
488 if a.(idx) >= x then if a.(idx) > y then nil else (last_idx := idx;a.(idx))
489 else loop (idx+1) x y
491 if a.(0) > j || a.(l-1) < i then nil
492 else loop !last_idx i j
497 let l = Array.length !contains_array in
498 if l = 0 then { t with node=Nil }
502 let i,j = tree_doc_ids t.doc n in
503 let id = array_find !contains_array i j
508 { t with node = Text(id, tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
509 | _ -> { t with node = Nil }
511 let text_next t root =
512 let l = Array.length !contains_array in
513 if l = 0 then { t with node=Nil }
515 let inf = match t.node with
516 | Node(n) -> snd(tree_doc_ids t.doc n)+1
522 let _,j = tree_doc_ids t.doc n in
523 let id = array_find !contains_array inf j
525 if id == nil then { t with node= Nil }
527 { t with node = Text(id,tree_next_sibling t.doc (tree_prev_doc t.doc id)) }
528 | _ -> { t with node = Nil}
532 let subtree_tags t tag =
534 { doc = d; node = Node(NC n) } ->
538 let select_desc_array = ref [| |]
541 let init_tagged_next t tagid =
542 let l = subtree_tags (root t) tagid
544 tagged_desc_array := Array.create l { t with node= Nil };
547 if is_node t then begin
548 if tag t == tagid then
550 !tagged_desc_array.(!i) <- t;
553 collect (first_child t);
554 collect (next_sibling t)
561 let pr x= Format.fprintf ppf x in
563 { node=Nil } -> pr "NULLT: -1"
564 | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
565 | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
569 (* let tagged_next t tag =
570 if !idx >= Array.length !tagged_desc_array
571 then {t with node=Nil}
573 let r = !tagged_desc_array.(!idx)
579 let has_tagged_foll t tag = is_node (tagged_foll t tag)
580 let has_tagged_desc t tag = is_node (tagged_desc t tag)
583 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
586 let contains_old t s =
587 let regexp = Str.regexp_string s in
590 let _ = Str.search_forward regexp arg 0;
594 let rec find t acc = match t.node with
597 if matching (string t) then DocIdSet.add i acc else acc
598 | Node(_) -> (find (left t )) ((find (right t)) acc)
600 find t DocIdSet.empty
603 let contains_iter t s =
604 let regexp = Str.regexp_string s in
607 let _ = Str.search_forward regexp arg 0;
611 let size = Text.size t.doc in
613 if n == size then acc
616 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
617 DocIdSet.add (Obj.magic n) acc
620 find DocIdSet.empty 0
625 let count_contains t s = Text.count_contains t.doc s
628 let count t s = text_count t.doc s
631 if is_root t then false
633 if tag (parent t) == Tag.pcdata then false
635 let u = left (parent t) in
638 let print_xml_fast outc t =
639 let rec loop ?(print_right=true) t =
642 | Text(i,n) -> output_string outc (text_get_text t.doc i);
646 let tg = Tag.to_string (tag t) in
650 output_char outc '<';
651 output_string outc tg;
653 Nil -> output_string outc "/>"
654 | Node(_) when Tag.equal (tag l) Tag.attribute ->
655 (loop_attributes (left l);
656 match (right l).node with
657 | Nil -> output_string outc "/>"
659 output_char outc '>';
661 output_string outc "</";
662 output_string outc tg;
663 output_char outc '>' )
665 output_char outc '>';
667 output_string outc "</";
668 output_string outc tg;
670 );if print_right then loop r
671 and loop_attributes a =
675 match (left a).node with
676 | Text(i,_) -> text_get_text a.doc i
679 output_char outc ' ';
680 output_string outc (Tag.to_string (tag a));
681 output_string outc "=\"";
682 output_string outc value;
683 output_char outc '"';
684 loop_attributes (right a)
687 loop ~print_right:false t
690 let print_xml_fast outc t =
691 if Tag.to_string (tag t) = "" then
692 print_xml_fast outc (first_child t)
693 else print_xml_fast outc t
696 let tags_below t tag =
697 fst(Hashtbl.find t.ttable tag)
699 let tags_after t tag =
700 snd(Hashtbl.find t.ttable tag)
702 let tags t tag = Hashtbl.find t.ttable tag