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 (******************************************************************************)
12 type descr = Nil | Node of node_content |String of string_content
14 val parse_xml_uri : string -> t
15 val parse_xml_string : string -> t
16 val save : t -> string -> unit
17 val load : ?sample:int -> string -> t
18 val tag_pool : t -> Tag.pool
19 val string : t -> string
20 val descr : t -> descr
21 val is_node : t -> bool
24 val first_child : t -> t
25 val next_sibling : t -> t
28 val is_root : t -> bool
31 val print_xml_fast : out_channel -> t -> unit
32 val compare : t -> t -> int
33 val equal : t -> t -> bool
38 with type elt = string_content
39 val string_below : t -> string_content -> bool
40 val contains : t -> string -> DocIdSet.t
41 val contains_old : t -> string -> DocIdSet.t
42 val contains_iter : t -> string -> DocIdSet.t
43 val count_contains : t -> string -> int
44 val count : t -> string -> int
46 val get_string : t -> string_content -> string
47 val has_tagged_desc : t -> Tag.t -> bool
48 val has_tagged_foll : t -> Tag.t -> bool
49 val tagged_desc : t -> Tag.t -> t
50 val tagged_foll : t -> Tag.t -> t
51 val tagged_next : t -> Tag.t -> t
52 val subtree_tags : t -> Tag.t -> int
53 val is_left : t -> bool
61 type node_kind = [`Text | `Tree ]
63 let compare : 'a node -> 'a node -> int = (-)
64 let equal : 'a node -> 'a node -> bool = (==)
66 (* abstract type, values are pointers to a XMLTree C++ object *)
68 external int_of_node : 'a node -> int = "%identity"
70 external parse_xml_uri : string -> int -> bool -> bool -> t = "caml_call_shredder_uri"
71 external parse_xml_string : string -> int -> bool -> bool -> t = "caml_call_shredder_string"
73 external save_tree : t -> string -> unit = "caml_xml_tree_save"
74 external load_tree : string -> int -> t = "caml_xml_tree_load"
79 let equal : [`Text] node -> [`Text] node -> bool = equal
82 external nullt : unit -> [`Text ] node = "caml_xml_tree_nullt"
84 external get_text : t -> [`Text] node -> string = "caml_text_collection_get_text"
87 if equal nil n then ""
91 external is_empty : t -> [`Text ] node -> bool = "caml_text_collection_empty_text"
93 (equal nil n) || is_empty t n
95 external get_cached_text : t -> [`Text ] node -> string = "caml_text_collection_get_cached_text"
99 if (equal nil n) || is_empty t n then ""
100 else get_cached_text t n
102 external size : t -> int = "caml_text_collection_size"
103 external is_contains : t -> string -> bool = "caml_text_collection_is_contains"
104 external count_contains : t -> string -> int = "caml_text_collection_count_contains"
105 external count : t -> string -> int = "caml_text_collection_count"
106 external contains : t -> string -> [`Text ] node array = "caml_text_collection_contains"
113 let equal : [`Tree ] node -> [`Tree] node -> bool = equal
114 external serialize : t -> string -> unit = "caml_xml_tree_serialize"
115 external unserialize : string -> t = "caml_xml_tree_unserialize"
117 external root : t -> [`Tree] node = "caml_xml_tree_root"
118 external nullt : unit -> [`Tree ] node = "caml_xml_tree_nullt"
121 let is_nil x = equal x nil
123 external parent : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
124 external parent_doc : t -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
125 external first_child : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
129 external next_sibling : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
131 external is_leaf : t -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
133 (* external tag : t -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
134 external tag_id : t -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id"
137 let tag_hash = Hashtbl.create 4097
141 Hashtbl.find tag_hash id
144 let tag = tag_id t id in
145 Hashtbl.add tag_hash id tag;tag
149 let is_last t n = equal nil (next_sibling t n)
151 external prev_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
154 external my_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
155 external next_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
157 external text_xml_id : t -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
158 external node_xml_id : t -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
159 external is_ancestor : t -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
160 external tagged_desc : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc"
161 external tagged_foll : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_foll"
162 external tagged_next : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_next"
163 external subtree_tags : t -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags"
168 then Printf.eprintf "#\n"
171 Printf.eprintf "Node %i has tag '%s' DocOrder=%i, DocID of PrevText,MyText,NextText : (%i = %s,%i = %s,%i = %s) parent_doc(my_text)=%i\n%!"
173 (Tag.to_string (tag_id t id))
175 (int_of_node (prev_text t id))
176 (Text.get_text t (prev_text t id))
177 (int_of_node (my_text t id))
178 (Text.get_text t (my_text t id))
179 (int_of_node (next_text t id))
180 (Text.get_text t (next_text t id))
181 (int_of_node(parent_doc t (my_text t id)));
183 aux(first_child t id);
184 aux(next_sibling t id);
194 (* ignore (tag t id);
195 ignore (Text.get_text t (prev_text t id));
197 then ignore (Text.get_text t (my_text t id));
199 then ignore (Text.get_text t (next_text t id)); *)
200 aux (first_child t id);
201 aux (next_sibling t id);
208 module Binary = struct
212 | SC of [`Text ] node * [`Tree ] node
213 type string_content = [ `Text ] node
216 | Node of node_content
217 | String of string_content
221 type t = { doc : doc;
224 let dump { doc=t } = Tree.print_skel t
225 module DocIdSet = struct
226 include Set.Make (struct type t = string_content
227 let compare = (-) end)
230 let is_node = function { node=Node(_) } -> true | _ -> false
232 let get_string t (i:string_content) = Text.get_text t.doc i
234 let node_of_t t = { doc= t;
235 node = Node(NC (root t)) }
238 let parse_xml_uri str = node_of_t
239 (MM((parse_xml_uri str
240 !Options.sample_factor
241 !Options.index_empty_texts
242 !Options.disable_text_collection),__LOCATION__))
244 let parse_xml_string str = node_of_t
245 (MM((parse_xml_string str
246 !Options.sample_factor
247 !Options.index_empty_texts
248 !Options.disable_text_collection),__LOCATION__))
251 let save t str = save_tree t.doc str
253 let load ?(sample=64) str = node_of_t (load_tree str sample)
256 external pool : doc -> Tag.pool = "%identity"
257 let tag_pool t = pool t.doc
259 let compare a b = match a.node,b.node with
260 | Node(NC i),Node(NC j) -> compare i j
261 | _, Node(NC( _ )) -> 1
262 | Node(SC (i,_)),Node(SC (j,_)) -> compare i j
263 | Node(NC( _ )),Node(SC (_,_)) -> -1
264 | _, Node(SC (_,_)) -> 1
265 | String i, String j -> compare i j
266 | Node _ , String _ -> -1
271 let equal a b = (compare a b) == 0
273 let string t = match t.node with
274 | String i -> Text.get_text t.doc i
277 let norm (n : [`Tree ] node ) = if is_nil n then Nil else Node (NC n)
283 | String i -> Printf.sprintf "String %i" i
284 | Node (NC t) -> Printf.sprintf "Node (NC %i)" (int_of_node t)
285 | Node (SC (t,i)) -> Printf.sprintf "Node (SC (%i,%i))" (int_of_node t) (int_of_node i)
288 let root n = { n with node = norm (Tree.root n.doc) }
289 let is_root n = match n.node with
290 | Node(NC t) when (Tree.root n.doc) == t -> true
297 let txt = prev_text n.doc t in
298 if Text.is_empty n.doc txt then
299 Node(NC (Tree.parent n.doc t))
302 | Node(SC(t,_)) -> Node (NC(parent_doc n.doc t))
303 | _ -> failwith "parent"
305 { n with node = node' }
310 | Node (NC t) when is_leaf n.doc t ->
311 let txt = my_text n.doc t in
312 if Text.is_empty n.doc txt
314 else Node(SC (txt,Tree.nil))
316 let fs = first_child n.doc t in
317 let txt = prev_text n.doc fs in
318 if Text.is_empty n.doc txt
320 else Node (SC (txt, fs))
321 | Node(SC (i,_)) -> String i
322 | Nil | String _ -> failwith "first_child"
324 { n with node = node'}
330 | Node (SC (_,ns)) -> norm ns
332 let ns = next_sibling n.doc t in
333 let txt = next_text n.doc t in
334 if Text.is_empty n.doc txt
336 else Node (SC (txt, ns))
337 | Nil | String _ -> failwith "next_sibling"
339 { n with node = node'}
342 let left = first_child
343 let right = next_sibling
346 function { doc=d; node=Node(NC n)} -> node_xml_id d n
347 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id d i
351 function { node=Node(SC _) } -> Tag.pcdata
352 | { doc=d; node=Node(NC n)} -> tag_id d n
353 | _ -> failwith "tag"
356 function { node=Node(SC _) } -> ()
357 | { doc=d; node=Node(NC n)} -> tag_id d n
360 let string_below t id =
361 let strid = parent_doc t.doc id in
364 (Tree.equal i strid) || (is_ancestor t.doc i strid)
365 | Node(SC(i,_)) -> Text.equal i id
369 let tagged_foll t tag =
370 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
372 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
373 | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil }
374 | { doc=d; node=Node(SC (_,n)) } ->
376 if tag_id d n == tag then n
378 let n' = tagged_desc d n tag in
379 if is_nil n' then tagged_foll d n tag
381 in {t with node= norm nnode}
382 | _ -> { t with node=Nil }
385 let tagged_desc t tag =
386 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
388 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
389 | _ -> { t with node=Nil }
392 let tagged_next t tag =
393 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_next"
395 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_next d n tag) }
396 | { doc=d; node=Node(SC (_,n)) } -> { t with node = norm (tagged_next d n tag) }
397 | _ -> { t with node=Nil }
399 let subtree_tags t tag =
401 { doc = d; node = Node(NC n) } -> subtree_tags d n tag
406 let has_tagged_foll t tag = is_node (tagged_foll t tag)
407 let has_tagged_desc t tag = is_node (tagged_desc t tag)
410 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
413 let contains_old t s =
414 let regexp = Str.regexp_string s in
417 let _ = Str.search_forward regexp arg 0;
421 let rec find t acc = match t.node with
424 if matching (string t) then DocIdSet.add i acc else acc
425 | Node(_) -> (find (left t )) ((find (right t)) acc)
427 find t DocIdSet.empty
430 let contains_iter t s =
431 let regexp = Str.regexp_string s in
434 let _ = Str.search_forward regexp arg 0;
438 let size = Text.size t.doc in
440 if n == size then acc
443 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
444 DocIdSet.add (Obj.magic n) acc
447 find DocIdSet.empty 0
452 let count_contains t s = Text.count_contains t.doc s
453 let count t s = Text.count t.doc s
456 let u = left (parent t) in
459 let print_xml_fast outc t =
460 let rec loop ?(print_right=true) t = match t.node with
462 | String (s) -> output_string outc (string t)
463 | Node _ when Tag.equal (tag t) Tag.pcdata ->
465 if print_right then loop (right t)
468 let tg = Tag.to_string (tag t) in
472 output_char outc '<';
473 output_string outc tg;
475 Nil -> output_string outc "/>"
476 | String _ -> assert false
477 | Node(_) when Tag.equal (tag l) Tag.attribute ->
478 (loop_attributes (left l);
479 match (right l).node with
480 | Nil -> output_string outc "/>"
482 output_char outc '>';
484 output_string outc "</";
485 output_string outc tg;
486 output_char outc '>' )
488 output_char outc '>';
490 output_string outc "</";
491 output_string outc tg;
493 );if print_right then loop r
494 and loop_attributes a =
499 match (left a).node with
501 | _ -> string (left(left a))
503 output_char outc ' ';
504 output_string outc (Tag.to_string (tag a));
505 output_string outc "=\"";
506 output_string outc value;
507 output_char outc '"';
508 loop_attributes (right a)
511 loop ~print_right:false t
514 let print_xml_fast outc t =
515 if Tag.to_string (tag t) = "" then
516 print_xml_fast outc (first_child t)
517 else print_xml_fast outc t
519 let traversal t = Tree.traversal t.doc
520 let full_traversal t =
524 | String i -> () (*ignore(Text.get_text t.doc i) *)
531 let print_stats _ = ()
543 let _timings = Hashtbl.create 107
546 let time _ref f arg =
547 let t1 = Unix.gettimeofday () in
549 let t2 = Unix.gettimeofday () in
550 let t = (1000. *.(t2 -. t1)) in
552 let (time,count) = try
553 Hashtbl.find _timings _ref
558 and count = count + 1
560 Hashtbl.replace _timings _ref (time,count);r
565 let first_child_ doc node =
566 time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
567 let next_sibling_ doc node =
568 time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
570 let is_empty_ text node =
571 time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
573 let prev_text_ doc node =
574 time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
576 let my_text_ doc node =
577 time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
579 let next_text_ doc node =
580 time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
582 let is_leaf_ doc node =
583 time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
585 let node_xml_id_ doc node =
586 time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
588 let text_xml_id_ doc node =
589 time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
595 | Node (NC t) when is_leaf_ n.doc t ->
596 let txt = my_text_ n.doc t in
597 if is_empty_ n.doc txt
599 else Node(SC (txt,XML.Tree.nil))
601 let fs = first_child_ n.doc t in
602 let txt = prev_text_ n.doc fs in
603 if is_empty_ n.doc txt
605 else Node (SC (txt, fs))
606 | Node(SC (i,_)) -> String i
607 | Nil | String _ -> failwith "first_child"
609 { n with node = node'}
615 | Node (SC (_,ns)) -> norm ns
617 let ns = next_sibling_ n.doc t in
619 if XML.Tree.is_nil ns then
621 else prev_text_ n.doc ns
623 if is_empty_ n.doc txt
625 else Node (SC (txt, ns))
626 | Nil | String _ -> failwith "next_sibling"
628 { n with node = node'}
631 function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
632 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
635 (* Wrapper around critical function *)
636 let string t = time ("TextCollection.GetText()") (string) t
637 let left = first_child
638 let right = next_sibling
639 let tag t = time ("XMLTree.GetTag()") (tag) t
641 let print_stats ppf =
642 let total_time,total_calls =
643 Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
644 tacc+. t, cacc + c) _timings (0.,0)
648 "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
649 Hashtbl.iter (fun name (time,count) ->
650 Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
653 (100. *. (float_of_int count)/.(float_of_int total_calls))
654 (time /. (float_of_int count))
656 (100. *. time /. total_time)) _timings;
657 Format.fprintf ppf "-------------------------------------------------------------------\n";
658 Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
659 "Total" total_calls 100. total_time 100.
662 let print_xml_fast outc t =
663 let rec loop ?(print_right=true) t = match t.node with
665 | String (s) -> output_string outc (string t)
666 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
669 let tg = Tag.to_string (tag t) in
673 output_char outc '<';
674 output_string outc tg;
676 Nil -> output_string outc "/>"
677 | String _ -> assert false
678 | Node(_) when Tag.equal (tag l) Tag.attribute ->
679 (loop_attributes (left l);
680 match (right l).node with
681 | Nil -> output_string outc "/>"
683 output_char outc '>';
685 output_string outc "</";
686 output_string outc tg;
687 output_char outc '>' )
689 output_char outc '>';
691 output_string outc "</";
692 output_string outc tg;
694 );if print_right then loop r
695 and loop_attributes a =
700 match (left a).node with
702 | _ -> string (left(left a))
704 output_char outc ' ';
705 output_string outc (Tag.to_string (tag a));
706 output_string outc "=\"";
707 output_string outc value;
708 output_char outc '"';
709 loop_attributes (right a)
712 loop ~print_right:false t
715 let print_xml_fast outc t =
716 if Tag.to_string (tag t) = "" then
717 print_xml_fast outc (first_child t)
718 else print_xml_fast outc t
725 module Binary = DEBUGTREE
727 module Binary = XML.Binary
728 END (* IFDEF DEBUG *)