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 descr = Nil | Node of node_content |String of string_content
13 val parse_xml_uri : string -> t
14 val parse_xml_string : string -> t
15 val string : t -> string
16 val descr : t -> descr
22 val print_xml_fast : out_channel -> t -> unit
23 val compare : t -> t -> int
24 val equal : t -> t -> bool
25 module DocIdSet : Set.S with type elt = string_content
26 val string_below : t -> string_content -> bool
27 val contains : t -> string -> DocIdSet.t
28 val contains_old : t -> string -> bool
37 type node_kind = [`Text | `Tree ]
39 let compare : 'a node -> 'a node -> int = (-)
40 let equal : 'a node -> 'a node -> bool = (==)
42 (* abstract type, values are pointers to a XMLTree C++ object *)
44 external int_of_node : 'a node -> int = "%identity"
46 external parse_xml_uri : string -> t = "caml_call_shredder_uri"
47 let parse_xml_uri uri = parse_xml_uri uri
49 external parse_xml_string : string -> t = "caml_call_shredder_string"
50 let parse_xml_string uri = parse_xml_string uri
57 external nullt : unit -> [`Text ] node = "caml_xml_tree_nullt"
59 external get_text : t -> [`Text] node -> string = "caml_text_collection_get_text"
62 if equal nil n then ""
65 external is_empty : t -> [`Text ] node -> bool = "caml_text_collection_empty_text"
67 (equal nil n) || is_empty t n
69 external is_contains : t -> string -> bool = "caml_text_collection_is_contains"
70 external count_contains : t -> string -> int = "caml_text_collection_count_contains"
71 external contains : t -> string -> [`Text ] node array = "caml_text_collection_contains"
79 external serialize : t -> string -> unit = "caml_xml_tree_serialize"
80 external unserialize : string -> t = "caml_xml_tree_unserialize"
82 external root : t -> [`Tree] node = "caml_xml_tree_root"
83 external nullt : unit -> [`Tree ] node = "caml_xml_tree_nullt"
86 let is_nil x = equal x nil
88 external parent : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
89 external parent_doc : t -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
90 external first_child : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
94 external next_sibling : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
96 external is_leaf : t -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
98 external tag : t -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag"
99 external tag_id : t -> [`Tree ] node -> unit = "caml_xml_tree_tag_id"
101 let is_last t n = equal nil (next_sibling t n)
103 external prev_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
106 external my_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
107 external next_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
109 external text_xml_id : t -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
110 external node_xml_id : t -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
111 external is_ancestor : t -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
116 then Printf.eprintf "#\n"
119 Printf.eprintf "Node %i has tag '%s' DocOrder=%i, DocID of PrevText,MyText,NextText : (%i = %s,%i = %s,%i = %s)\n%!"
121 (Tag.to_string (tag t id))
123 (int_of_node (prev_text t id))
124 (Text.get_text t (prev_text t id))
125 (int_of_node (my_text t id))
126 (Text.get_text t (my_text t id))
127 (int_of_node (next_text t id))
128 (Text.get_text t (next_text t id));
129 aux(first_child t id);
130 aux(next_sibling t id);
140 (* ignore (tag t id);
141 ignore (Text.get_text t (prev_text t id));
143 then ignore (Text.get_text t (my_text t id));
145 then ignore (Text.get_text t (next_text t id)); *)
146 aux (first_child t id);
147 aux (next_sibling t id);
154 module Binary = struct
158 | SC of [`Text ] node * [`Tree ] node
159 type string_content = [ `Text ] node
162 | Node of node_content
163 | String of string_content
167 type t = { doc : doc;
170 let dump { doc=t } = Tree.print_skel t
171 module DocIdSet = Set.Make (struct type t = string_content
172 let compare = (-) end)
176 let node_of_t t = { doc= t;
177 node = Node(NC (root t)) }
180 let parse_xml_uri str = node_of_t (parse_xml_uri str)
181 let parse_xml_string str = node_of_t (parse_xml_string str)
183 let compare a b = match a.node,b.node with
184 | Node(NC i),Node(NC j) -> compare i j
185 | _, Node(NC( _ )) -> 1
186 | Node(SC (i,_)),Node(SC (j,_)) -> compare i j
187 | Node(NC( _ )),Node(SC (_,_)) -> -1
188 | _, Node(SC (_,_)) -> 1
189 | String i, String j -> compare i j
190 | Node _ , String _ -> -1
195 let equal a b = (compare a b) == 0
197 let string t = match t.node with
198 | String i -> Text.get_text t.doc i
201 let norm (n : [`Tree ] node ) = if is_nil n then Nil else Node (NC n)
207 | String i -> Printf.sprintf "String %i" i
208 | Node (NC t) -> Printf.sprintf "Node (NC %i)" (int_of_node t)
209 | Node (SC (t,i)) -> Printf.sprintf "Node (SC (%i,%i))" (int_of_node t) (int_of_node i)
215 | Node(NC t) | Node(SC (_,t)) ->
216 if (Tree.root n.doc) == t
218 else Node(NC(Tree.parent n.doc t)) (* A parent node can never be a SC *)
221 { n with node = node' }
226 | Node (NC t) when is_leaf n.doc t ->
227 let txt = my_text n.doc t in
228 if Text.is_empty n.doc txt
230 else Node(SC (txt,Tree.nil))
232 let fs = first_child n.doc t in
233 let txt = prev_text n.doc fs in
234 if Text.is_empty n.doc txt
236 else Node (SC (txt, fs))
237 | Node(SC (i,_)) -> String i
238 | Nil | String _ -> failwith "first_child"
240 { n with node = node'}
246 | Node (SC (_,ns)) -> norm ns
248 let ns = next_sibling n.doc t in
249 let txt = next_text n.doc t in
250 if Text.is_empty n.doc txt
252 else Node (SC (txt, ns))
253 | Nil | String _ -> failwith "next_sibling"
255 { n with node = node'}
258 let left = first_child
259 let right = next_sibling
262 function { doc=d; node=Node(NC n)} -> node_xml_id d n
263 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id d i
267 function { node=Node(SC _) } -> Tag.pcdata
268 | { doc=d; node=Node(NC n)} -> tag d n
269 | _ -> failwith "Tag"
272 function { node=Node(SC _) } -> ()
273 | { doc=d; node=Node(NC n)} -> tag_id d n
276 let string_below t id =
277 let pid = parent_doc t.doc id in
279 | Node(NC(i)) -> (is_ancestor t.doc i pid)
280 | Node(SC(i,_)) -> (is_ancestor t.doc (parent_doc t.doc i) pid)
284 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
286 let contains_old t s =
287 let regexp = Str.regexp_string s in
290 let _ = Str.search_forward regexp arg 0;
294 let rec find t = match t.node with
296 | String _ -> matching (string t)
297 | Node(_) -> (find (left t )) || (find (right t))
301 let print_xml_fast outc t =
302 let rec loop ?(print_right=true) t = match t.node with
304 | String (s) -> output_string outc (string t)
305 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
308 let tg = Tag.to_string (tag t) in
312 output_char outc '<';
313 output_string outc tg;
315 Nil -> output_string outc "/>"
316 | String _ -> assert false
317 | Node(_) when Tag.equal (tag l) Tag.attribute ->
318 (loop_attributes (left l);
319 match (right l).node with
320 | Nil -> output_string outc "/>"
322 output_char outc '>';
324 output_string outc "</";
325 output_string outc tg;
326 output_char outc '>' )
328 output_char outc '>';
330 output_string outc "</";
331 output_string outc tg;
333 );if print_right then loop r
334 and loop_attributes a =
339 match (left a).node with
341 | _ -> string (left(left a))
343 output_char outc ' ';
344 output_string outc (Tag.to_string (tag a));
345 output_string outc "=\"";
346 output_string outc value;
347 output_char outc '"';
348 loop_attributes (right a)
351 loop ~print_right:false t
354 let print_xml_fast outc t =
355 if Tag.to_string (tag t) = "" then
356 print_xml_fast outc (first_child t)
357 else print_xml_fast outc t
359 let traversal t = Tree.traversal t.doc
360 let full_traversal t =
364 | String i -> () (*ignore(Text.get_text t.doc i) *)
381 let _timings = Hashtbl.create 107
384 let time _ref f arg =
385 let t1 = Unix.gettimeofday () in
387 let t2 = Unix.gettimeofday () in
388 let t = (1000. *.(t2 -. t1)) in
390 let (time,count) = try
391 Hashtbl.find _timings _ref
396 and count = count + 1
398 Hashtbl.replace _timings _ref (time,count);r
403 let first_child_ doc node =
404 time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
405 let next_sibling_ doc node =
406 time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
408 let is_empty_ text node =
409 time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
411 let prev_text_ doc node =
412 time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
414 let my_text_ doc node =
415 time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
417 let next_text_ doc node =
418 time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
420 let is_leaf_ doc node =
421 time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
423 let node_xml_id_ doc node =
424 time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
426 let text_xml_id_ doc node =
427 time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
433 | Node (NC t) when is_leaf_ n.doc t ->
434 let txt = my_text_ n.doc t in
435 if is_empty_ n.doc txt
437 else Node(SC (txt,XML.Tree.nil))
439 let fs = first_child_ n.doc t in
440 let txt = prev_text_ n.doc fs in
441 if is_empty_ n.doc txt
443 else Node (SC (txt, fs))
444 | Node(SC (i,_)) -> String i
445 | Nil | String _ -> failwith "first_child"
447 { n with node = node'}
453 | Node (SC (_,ns)) -> norm ns
455 let ns = next_sibling_ n.doc t in
457 if XML.Tree.is_nil ns then
459 else prev_text_ n.doc ns
461 if is_empty_ n.doc txt
463 else Node (SC (txt, ns))
464 | Nil | String _ -> failwith "next_sibling"
466 { n with node = node'}
469 function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
470 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
473 (* Wrapper around critical function *)
474 let string t = time ("TextCollection.GetText()") (string) t
475 let left = first_child
476 let right = next_sibling
477 let tag t = time ("XMLTree.GetTag()") (tag) t
479 let print_stats ppf =
480 let total_time,total_calls =
481 Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
482 tacc+. t, cacc + c) _timings (0.,0)
486 "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
487 Hashtbl.iter (fun name (time,count) ->
488 Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
491 (100. *. (float_of_int count)/.(float_of_int total_calls))
492 (time /. (float_of_int count))
494 (100. *. time /. total_time)) _timings;
495 Format.fprintf ppf "-------------------------------------------------------------------\n";
496 Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
497 "Total" total_calls 100. total_time 100.
500 let print_xml_fast outc t =
501 let rec loop ?(print_right=true) t = match t.node with
503 | String (s) -> output_string outc (string t)
504 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
507 let tg = Tag.to_string (tag t) in
511 output_char outc '<';
512 output_string outc tg;
514 Nil -> output_string outc "/>"
515 | String _ -> assert false
516 | Node(_) when Tag.equal (tag l) Tag.attribute ->
517 (loop_attributes (left l);
518 match (right l).node with
519 | Nil -> output_string outc "/>"
521 output_char outc '>';
523 output_string outc "</";
524 output_string outc tg;
525 output_char outc '>' )
527 output_char outc '>';
529 output_string outc "</";
530 output_string outc tg;
532 );if print_right then loop r
533 and loop_attributes a =
538 match (left a).node with
540 | _ -> string (left(left a))
542 output_char outc ' ';
543 output_string outc (Tag.to_string (tag a));
544 output_string outc "=\"";
545 output_string outc value;
546 output_char outc '"';
547 loop_attributes (right a)
550 loop ~print_right:false t
553 let print_xml_fast outc t =
554 if Tag.to_string (tag t) = "" then
555 print_xml_fast outc (first_child t)
556 else print_xml_fast outc t
563 module Binary = DEBUGTREE