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
55 type t (* pointer to the text collection *)
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 external text_collection : t -> Text.t = "caml_xml_tree_text_collection"
103 let is_last t n = equal nil (next_sibling t n)
105 external prev_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
108 external my_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
109 external next_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
111 external text_xml_id : t -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
112 external node_xml_id : t -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
113 external is_ancestor : t -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
116 let textcol = text_collection t in
119 then Printf.eprintf "#\n"
122 Printf.eprintf "Node %i has tag '%s' DocOrder=%i, DocID of PrevText,MyText,NextText : (%i = %s,%i = %s,%i = %s)\n%!"
124 (Tag.to_string (tag t id))
126 (int_of_node (prev_text t id))
127 (Text.get_text textcol (prev_text t id))
128 (int_of_node (my_text t id))
129 (Text.get_text textcol (my_text t id))
130 (int_of_node (next_text t id))
131 (Text.get_text textcol (next_text t id));
132 aux(first_child t id);
133 aux(next_sibling t id);
139 let textcol = text_collection t in
144 (* ignore (tag t id);
145 ignore (Text.get_text textcol (prev_text t id));
147 then ignore (Text.get_text textcol (my_text t id));
149 then ignore (Text.get_text textcol (next_text t id)); *)
150 aux (first_child t id);
151 aux (next_sibling t id);
158 module Binary = struct
162 | SC of [`Text ] node * [`Tree ] node
163 type string_content = [ `Text ] node
166 | Node of node_content
167 | String of string_content
171 type t = { doc : doc;
175 let dump { doc=t } = Tree.print_skel t
176 module DocIdSet = Set.Make (struct type t = string_content
177 let compare = (-) end)
181 let node_of_t t = { doc= t;
182 text = text_collection t;
183 node = Node(NC (root t)) }
186 let parse_xml_uri str = node_of_t (parse_xml_uri str)
187 let parse_xml_string str = node_of_t (parse_xml_string str)
189 let compare a b = match a.node,b.node with
190 | Node(NC i),Node(NC j) -> compare i j
191 | _, Node(NC( _ )) -> 1
192 | Node(SC (i,_)),Node(SC (j,_)) -> compare i j
193 | Node(NC( _ )),Node(SC (_,_)) -> -1
194 | _, Node(SC (_,_)) -> 1
195 | String i, String j -> compare i j
196 | Node _ , String _ -> -1
201 let equal a b = (compare a b) == 0
203 let string t = match t.node with
204 | String i -> Text.get_text t.text i
207 let norm (n : [`Tree ] node ) = if is_nil n then Nil else Node (NC n)
213 | String i -> Printf.sprintf "String %i" i
214 | Node (NC t) -> Printf.sprintf "Node (NC %i)" (int_of_node t)
215 | Node (SC (t,i)) -> Printf.sprintf "Node (SC (%i,%i))" (int_of_node t) (int_of_node i)
221 | Node(NC t) | Node(SC (_,t)) ->
222 if (Tree.root n.doc) == t
224 else Node(NC(Tree.parent n.doc t)) (* A parent node can never be a SC *)
227 { n with node = node' }
232 | Node (NC t) when is_leaf n.doc t ->
233 let txt = my_text n.doc t in
234 if Text.is_empty n.text txt
236 else Node(SC (txt,Tree.nil))
238 let fs = first_child n.doc t in
239 let txt = prev_text n.doc fs in
240 if Text.is_empty n.text txt
242 else Node (SC (txt, fs))
243 | Node(SC (i,_)) -> String i
244 | Nil | String _ -> failwith "first_child"
246 { n with node = node'}
252 | Node (SC (_,ns)) -> norm ns
254 let ns = next_sibling n.doc t in
255 let txt = next_text n.doc t in
256 if Text.is_empty n.text txt
258 else Node (SC (txt, ns))
259 | Nil | String _ -> failwith "next_sibling"
261 { n with node = node'}
264 let left = first_child
265 let right = next_sibling
268 function { doc=d; node=Node(NC n)} -> node_xml_id d n
269 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id d i
273 function { node=Node(SC _) } -> Tag.pcdata
274 | { doc=d; node=Node(NC n)} -> tag d n
275 | _ -> failwith "Tag"
278 function { node=Node(SC _) } -> ()
279 | { doc=d; node=Node(NC n)} -> tag_id d n
282 let string_below t id =
283 let pid = parent_doc t.doc id in
285 | Node(NC(i)) -> (is_ancestor t.doc i pid)
286 | Node(SC(i,_)) -> (is_ancestor t.doc (parent_doc t.doc i) pid)
290 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.text s)
292 let contains_old t s =
293 let regexp = Str.regexp_string s in
296 let _ = Str.search_forward regexp arg 0;
300 let rec find t = match t.node with
302 | String _ -> matching (string t)
303 | Node(_) -> (find (left t )) || (find (right t))
307 let print_xml_fast outc t =
308 let rec loop ?(print_right=true) t = match t.node with
310 | String (s) -> output_string outc (string t)
311 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
314 let tg = Tag.to_string (tag t) in
318 output_char outc '<';
319 output_string outc tg;
321 Nil -> output_string outc "/>"
322 | String _ -> assert false
323 | Node(_) when Tag.equal (tag l) Tag.attribute ->
324 (loop_attributes (left l);
325 match (right l).node with
326 | Nil -> output_string outc "/>"
328 output_char outc '>';
330 output_string outc "</";
331 output_string outc tg;
332 output_char outc '>' )
334 output_char outc '>';
336 output_string outc "</";
337 output_string outc tg;
339 );if print_right then loop r
340 and loop_attributes a =
345 match (left a).node with
347 | _ -> string (left(left a))
349 output_char outc ' ';
350 output_string outc (Tag.to_string (tag a));
351 output_string outc "=\"";
352 output_string outc value;
353 output_char outc '"';
354 loop_attributes (right a)
357 loop ~print_right:false t
360 let print_xml_fast outc t =
361 if Tag.to_string (tag t) = "" then
362 print_xml_fast outc (first_child t)
363 else print_xml_fast outc t
365 let traversal t = Tree.traversal t.doc
366 let full_traversal t =
370 | String i -> () (*ignore(Text.get_text t.text i) *)
387 let _timings = Hashtbl.create 107
390 let time _ref f arg =
391 let t1 = Unix.gettimeofday () in
393 let t2 = Unix.gettimeofday () in
394 let t = (1000. *.(t2 -. t1)) in
396 let (time,count) = try
397 Hashtbl.find _timings _ref
402 and count = count + 1
404 Hashtbl.replace _timings _ref (time,count);r
409 let first_child_ doc node =
410 time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
411 let next_sibling_ doc node =
412 time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
414 let is_empty_ text node =
415 time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
417 let prev_text_ doc node =
418 time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
420 let my_text_ doc node =
421 time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
423 let next_text_ doc node =
424 time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
426 let is_leaf_ doc node =
427 time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
429 let node_xml_id_ doc node =
430 time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
432 let text_xml_id_ doc node =
433 time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
439 | Node (NC t) when is_leaf_ n.doc t ->
440 let txt = my_text_ n.doc t in
441 if is_empty_ n.text txt
443 else Node(SC (txt,XML.Tree.nil))
445 let fs = first_child_ n.doc t in
446 let txt = prev_text_ n.doc fs in
447 if is_empty_ n.text txt
449 else Node (SC (txt, fs))
450 | Node(SC (i,_)) -> String i
451 | Nil | String _ -> failwith "first_child"
453 { n with node = node'}
459 | Node (SC (_,ns)) -> norm ns
461 let ns = next_sibling_ n.doc t in
462 let txt = next_text_ n.doc t in
463 if is_empty_ n.text txt
465 else Node (SC (txt, ns))
466 | Nil | String _ -> failwith "next_sibling"
468 { n with node = node'}
471 function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
472 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
476 (* Wrapper around critical function *)
477 let string t = time ("TextCollection.GetText()") (string) t
478 let left = first_child
479 let right = next_sibling
480 let tag t = time ("XMLTree.GetTag()") (tag) t
482 let print_stats ppf =
483 let total_time,total_calls =
484 Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
485 tacc+. t, cacc + c) _timings (0.,0)
489 "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
490 Hashtbl.iter (fun name (time,count) ->
491 Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
494 (100. *. (float_of_int count)/.(float_of_int total_calls))
495 (time /. (float_of_int count))
497 (100. *. time /. total_time)) _timings;
498 Format.fprintf ppf "-------------------------------------------------------------------\n";
499 Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
500 "Total" total_calls 100. total_time 100.
503 let print_xml_fast outc t =
504 let rec loop ?(print_right=true) t = match t.node with
506 | String (s) -> output_string outc (string t)
507 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
510 let tg = Tag.to_string (tag t) in
514 output_char outc '<';
515 output_string outc tg;
517 Nil -> output_string outc "/>"
518 | String _ -> assert false
519 | Node(_) when Tag.equal (tag l) Tag.attribute ->
520 (loop_attributes (left l);
521 match (right l).node with
522 | Nil -> output_string outc "/>"
524 output_char outc '>';
526 output_string outc "</";
527 output_string outc tg;
528 output_char outc '>' )
530 output_char outc '>';
532 output_string outc "</";
533 output_string outc tg;
535 );if print_right then loop r
536 and loop_attributes a =
541 match (left a).node with
543 | _ -> string (left(left a))
545 output_char outc ' ';
546 output_string outc (Tag.to_string (tag a));
547 output_string outc "=\"";
548 output_string outc value;
549 output_char outc '"';
550 loop_attributes (right a)
553 loop ~print_right:false t
556 let print_xml_fast outc t =
557 if Tag.to_string (tag t) = "" then
558 print_xml_fast outc (first_child t)
559 else print_xml_fast outc t
566 module Binary = DEBUGTREE