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"
136 let is_last t n = equal nil (next_sibling t n)
138 external prev_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
141 external my_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
142 external next_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
144 external text_xml_id : t -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
145 external node_xml_id : t -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
146 external is_ancestor : t -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
147 external tagged_desc : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc"
148 external tagged_foll : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_foll"
149 external tagged_next : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_next"
150 external subtree_tags : t -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags"
155 then Printf.eprintf "#\n"
158 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%!"
160 (Tag.to_string (tag_id t id))
162 (int_of_node (prev_text t id))
163 (Text.get_text t (prev_text t id))
164 (int_of_node (my_text t id))
165 (Text.get_text t (my_text t id))
166 (int_of_node (next_text t id))
167 (Text.get_text t (next_text t id))
168 (int_of_node(parent_doc t (my_text t id)));
170 aux(first_child t id);
171 aux(next_sibling t id);
181 (* ignore (tag t id);
182 ignore (Text.get_text t (prev_text t id));
184 then ignore (Text.get_text t (my_text t id));
186 then ignore (Text.get_text t (next_text t id)); *)
187 aux (first_child t id);
188 aux (next_sibling t id);
195 module Binary = struct
199 | SC of [`Text ] node * [`Tree ] node
200 type string_content = [ `Text ] node
203 | Node of node_content
204 | String of string_content
208 type t = { doc : doc;
211 let dump { doc=t } = Tree.print_skel t
212 module DocIdSet = struct
213 include Set.Make (struct type t = string_content
214 let compare = (-) end)
217 let is_node = function { node=Node(_) } -> true | _ -> false
219 let get_string t (i:string_content) = Text.get_text t.doc i
221 let node_of_t t = { doc= t;
222 node = Node(NC (root t)) }
225 let parse_xml_uri str = node_of_t
226 (MM((parse_xml_uri str
227 !Options.sample_factor
228 !Options.index_empty_texts
229 !Options.disable_text_collection),__LOCATION__))
231 let parse_xml_string str = node_of_t
232 (MM((parse_xml_string str
233 !Options.sample_factor
234 !Options.index_empty_texts
235 !Options.disable_text_collection),__LOCATION__))
238 let save t str = save_tree t.doc str
240 let load ?(sample=64) str = node_of_t (load_tree str sample)
243 external pool : doc -> Tag.pool = "%identity"
244 let tag_pool t = pool t.doc
246 let compare a b = match a.node,b.node with
247 | Node(NC i),Node(NC j) -> compare i j
248 | _, Node(NC( _ )) -> 1
249 | Node(SC (i,_)),Node(SC (j,_)) -> compare i j
250 | Node(NC( _ )),Node(SC (_,_)) -> -1
251 | _, Node(SC (_,_)) -> 1
252 | String i, String j -> compare i j
253 | Node _ , String _ -> -1
258 let equal a b = (compare a b) == 0
260 let string t = match t.node with
261 | String i -> Text.get_text t.doc i
264 let norm (n : [`Tree ] node ) = if is_nil n then Nil else Node (NC n)
270 | String i -> Printf.sprintf "String %i" i
271 | Node (NC t) -> Printf.sprintf "Node (NC %i)" (int_of_node t)
272 | Node (SC (t,i)) -> Printf.sprintf "Node (SC (%i,%i))" (int_of_node t) (int_of_node i)
275 let root n = { n with node = norm (Tree.root n.doc) }
276 let is_root n = match n.node with
277 | Node(NC t) when (Tree.root n.doc) == t -> true
284 let txt = prev_text n.doc t in
285 if Text.is_empty n.doc txt then
286 Node(NC (Tree.parent n.doc t))
289 | Node(SC(t,_)) -> Node (NC(parent_doc n.doc t))
290 | _ -> failwith "parent"
292 { n with node = node' }
297 | Node (NC t) when is_leaf n.doc t ->
298 let txt = my_text n.doc t in
299 if Text.is_empty n.doc txt
301 else Node(SC (txt,Tree.nil))
303 let fs = first_child n.doc t in
304 let txt = prev_text n.doc fs in
305 if Text.is_empty n.doc txt
307 else Node (SC (txt, fs))
308 | Node(SC (i,_)) -> String i
309 | Nil | String _ -> failwith "first_child"
311 { n with node = node'}
317 | Node (SC (_,ns)) -> norm ns
319 let ns = next_sibling n.doc t in
320 let txt = next_text n.doc t in
321 if Text.is_empty n.doc txt
323 else Node (SC (txt, ns))
324 | Nil | String _ -> failwith "next_sibling"
326 { n with node = node'}
329 let left = first_child
330 let right = next_sibling
333 function { doc=d; node=Node(NC n)} -> node_xml_id d n
334 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id d i
338 function { node=Node(SC _) } -> Tag.pcdata
339 | { doc=d; node=Node(NC n)} -> tag_id d n
340 | _ -> failwith "tag"
343 function { node=Node(SC _) } -> ()
344 | { doc=d; node=Node(NC n)} -> tag_id d n
347 let string_below t id =
348 let strid = parent_doc t.doc id in
351 (Tree.equal i strid) || (is_ancestor t.doc i strid)
352 | Node(SC(i,_)) -> Text.equal i id
356 let tagged_foll t tag =
357 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
359 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
360 | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil }
361 | { doc=d; node=Node(SC (_,n)) } ->
363 if tag_id d n == tag then n
365 let n' = tagged_desc d n tag in
366 if is_nil n' then tagged_foll d n tag
368 in {t with node= norm nnode}
369 | _ -> { t with node=Nil }
372 let tagged_desc t tag =
373 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
375 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
376 | _ -> { t with node=Nil }
379 let tagged_next t tag =
380 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_next"
382 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_next d n tag) }
383 | { doc=d; node=Node(SC (_,n)) } -> { t with node = norm (tagged_next d n tag) }
384 | _ -> { t with node=Nil }
386 let subtree_tags t tag =
388 { doc = d; node = Node(NC n) } -> subtree_tags d n tag
393 let has_tagged_foll t tag = is_node (tagged_foll t tag)
394 let has_tagged_desc t tag = is_node (tagged_desc t tag)
397 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
400 let contains_old t s =
401 let regexp = Str.regexp_string s in
404 let _ = Str.search_forward regexp arg 0;
408 let rec find t acc = match t.node with
411 if matching (string t) then DocIdSet.add i acc else acc
412 | Node(_) -> (find (left t )) ((find (right t)) acc)
414 find t DocIdSet.empty
417 let contains_iter t s =
418 let regexp = Str.regexp_string s in
421 let _ = Str.search_forward regexp arg 0;
425 let size = Text.size t.doc in
427 if n == size then acc
430 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
431 DocIdSet.add (Obj.magic n) acc
434 find DocIdSet.empty 0
439 let count_contains t s = Text.count_contains t.doc s
440 let count t s = Text.count t.doc s
443 let u = left (parent t) in
446 let print_xml_fast outc t =
447 let rec loop ?(print_right=true) t = match t.node with
449 | String (s) -> output_string outc (string t)
450 | Node _ when Tag.equal (tag t) Tag.pcdata ->
452 if print_right then loop (right t)
455 let tg = Tag.to_string (tag t) in
459 output_char outc '<';
460 output_string outc tg;
462 Nil -> output_string outc "/>"
463 | String _ -> assert false
464 | Node(_) when Tag.equal (tag l) Tag.attribute ->
465 (loop_attributes (left l);
466 match (right l).node with
467 | Nil -> output_string outc "/>"
469 output_char outc '>';
471 output_string outc "</";
472 output_string outc tg;
473 output_char outc '>' )
475 output_char outc '>';
477 output_string outc "</";
478 output_string outc tg;
480 );if print_right then loop r
481 and loop_attributes a =
486 match (left a).node with
488 | _ -> string (left(left a))
490 output_char outc ' ';
491 output_string outc (Tag.to_string (tag a));
492 output_string outc "=\"";
493 output_string outc value;
494 output_char outc '"';
495 loop_attributes (right a)
498 loop ~print_right:false t
501 let print_xml_fast outc t =
502 if Tag.to_string (tag t) = "" then
503 print_xml_fast outc (first_child t)
504 else print_xml_fast outc t
506 let traversal t = Tree.traversal t.doc
507 let full_traversal t =
511 | String i -> () (*ignore(Text.get_text t.doc i) *)
518 let print_stats _ = ()
530 let _timings = Hashtbl.create 107
533 let time _ref f arg =
534 let t1 = Unix.gettimeofday () in
536 let t2 = Unix.gettimeofday () in
537 let t = (1000. *.(t2 -. t1)) in
539 let (time,count) = try
540 Hashtbl.find _timings _ref
545 and count = count + 1
547 Hashtbl.replace _timings _ref (time,count);r
552 let first_child_ doc node =
553 time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
554 let next_sibling_ doc node =
555 time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
557 let is_empty_ text node =
558 time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
560 let prev_text_ doc node =
561 time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
563 let my_text_ doc node =
564 time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
566 let next_text_ doc node =
567 time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
569 let is_leaf_ doc node =
570 time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
572 let node_xml_id_ doc node =
573 time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
575 let text_xml_id_ doc node =
576 time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
582 | Node (NC t) when is_leaf_ n.doc t ->
583 let txt = my_text_ n.doc t in
584 if is_empty_ n.doc txt
586 else Node(SC (txt,XML.Tree.nil))
588 let fs = first_child_ n.doc t in
589 let txt = prev_text_ n.doc fs in
590 if is_empty_ n.doc txt
592 else Node (SC (txt, fs))
593 | Node(SC (i,_)) -> String i
594 | Nil | String _ -> failwith "first_child"
596 { n with node = node'}
602 | Node (SC (_,ns)) -> norm ns
604 let ns = next_sibling_ n.doc t in
606 if XML.Tree.is_nil ns then
608 else prev_text_ n.doc ns
610 if is_empty_ n.doc txt
612 else Node (SC (txt, ns))
613 | Nil | String _ -> failwith "next_sibling"
615 { n with node = node'}
618 function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
619 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
622 (* Wrapper around critical function *)
623 let string t = time ("TextCollection.GetText()") (string) t
624 let left = first_child
625 let right = next_sibling
626 let tag t = time ("XMLTree.GetTag()") (tag) t
628 let print_stats ppf =
629 let total_time,total_calls =
630 Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
631 tacc+. t, cacc + c) _timings (0.,0)
635 "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
636 Hashtbl.iter (fun name (time,count) ->
637 Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
640 (100. *. (float_of_int count)/.(float_of_int total_calls))
641 (time /. (float_of_int count))
643 (100. *. time /. total_time)) _timings;
644 Format.fprintf ppf "-------------------------------------------------------------------\n";
645 Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
646 "Total" total_calls 100. total_time 100.
649 let print_xml_fast outc t =
650 let rec loop ?(print_right=true) t = match t.node with
652 | String (s) -> output_string outc (string t)
653 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
656 let tg = Tag.to_string (tag t) in
660 output_char outc '<';
661 output_string outc tg;
663 Nil -> output_string outc "/>"
664 | String _ -> assert false
665 | Node(_) when Tag.equal (tag l) Tag.attribute ->
666 (loop_attributes (left l);
667 match (right l).node with
668 | Nil -> output_string outc "/>"
670 output_char outc '>';
672 output_string outc "</";
673 output_string outc tg;
674 output_char outc '>' )
676 output_char outc '>';
678 output_string outc "</";
679 output_string outc tg;
681 );if print_right then loop r
682 and loop_attributes a =
687 match (left a).node with
689 | _ -> string (left(left a))
691 output_char outc ' ';
692 output_string outc (Tag.to_string (tag a));
693 output_string outc "=\"";
694 output_string outc value;
695 output_char outc '"';
696 loop_attributes (right a)
699 loop ~print_right:false t
702 let print_xml_fast outc t =
703 if Tag.to_string (tag t) = "" then
704 print_xml_fast outc (first_child t)
705 else print_xml_fast outc t
712 module Binary = DEBUGTREE
714 module Binary = XML.Binary
715 END (* IFDEF DEBUG *)