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 tag_pool : t -> Tag.pool
16 val string : t -> string
17 val descr : t -> descr
23 val print_xml_fast : out_channel -> t -> unit
24 val compare : t -> t -> int
25 val equal : t -> t -> bool
26 module DocIdSet : Set.S with type elt = string_content
27 val string_below : t -> string_content -> bool
28 val contains : t -> string -> DocIdSet.t
29 val contains_old : t -> string -> bool
38 type node_kind = [`Text | `Tree ]
40 let compare : 'a node -> 'a node -> int = (-)
41 let equal : 'a node -> 'a node -> bool = (==)
43 (* abstract type, values are pointers to a XMLTree C++ object *)
45 external int_of_node : 'a node -> int = "%identity"
47 external parse_xml_uri : string -> t = "caml_call_shredder_uri"
48 let parse_xml_uri uri = parse_xml_uri uri
50 external parse_xml_string : string -> t = "caml_call_shredder_string"
51 let parse_xml_string uri = parse_xml_string uri
58 external nullt : unit -> [`Text ] node = "caml_xml_tree_nullt"
60 external get_text : t -> [`Text] node -> string = "caml_text_collection_get_text"
63 if equal nil n then ""
66 external is_empty : t -> [`Text ] node -> bool = "caml_text_collection_empty_text"
68 (equal nil n) || is_empty t n
70 external is_contains : t -> string -> bool = "caml_text_collection_is_contains"
71 external count_contains : t -> string -> int = "caml_text_collection_count_contains"
72 external contains : t -> string -> [`Text ] node array = "caml_text_collection_contains"
80 external serialize : t -> string -> unit = "caml_xml_tree_serialize"
81 external unserialize : string -> t = "caml_xml_tree_unserialize"
83 external root : t -> [`Tree] node = "caml_xml_tree_root"
84 external nullt : unit -> [`Tree ] node = "caml_xml_tree_nullt"
87 let is_nil x = equal x nil
89 external parent : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
90 external parent_doc : t -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
91 external first_child : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
95 external next_sibling : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
97 external is_leaf : t -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
99 (* external tag : t -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
100 external tag_id : t -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id"
102 let is_last t n = equal nil (next_sibling t n)
104 external prev_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
107 external my_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
108 external next_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
110 external text_xml_id : t -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
111 external node_xml_id : t -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
112 external is_ancestor : t -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
117 then Printf.eprintf "#\n"
120 Printf.eprintf "Node %i has tag '%s' DocOrder=%i, DocID of PrevText,MyText,NextText : (%i = %s,%i = %s,%i = %s)\n%!"
122 (Tag.to_string (tag_id t id))
124 (int_of_node (prev_text t id))
125 (Text.get_text t (prev_text t id))
126 (int_of_node (my_text t id))
127 (Text.get_text t (my_text t id))
128 (int_of_node (next_text t id))
129 (Text.get_text t (next_text t id));
130 aux(first_child t id);
131 aux(next_sibling t id);
141 (* ignore (tag t id);
142 ignore (Text.get_text t (prev_text t id));
144 then ignore (Text.get_text t (my_text t id));
146 then ignore (Text.get_text t (next_text t id)); *)
147 aux (first_child t id);
148 aux (next_sibling t id);
155 module Binary = struct
159 | SC of [`Text ] node * [`Tree ] node
160 type string_content = [ `Text ] node
163 | Node of node_content
164 | String of string_content
168 type t = { doc : doc;
171 let dump { doc=t } = Tree.print_skel t
172 module DocIdSet = Set.Make (struct type t = string_content
173 let compare = (-) end)
177 let node_of_t t = { doc= t;
178 node = Node(NC (root t)) }
181 let parse_xml_uri str = node_of_t (parse_xml_uri str)
182 let parse_xml_string str = node_of_t (parse_xml_string str)
185 external pool : doc -> Tag.pool = "%identity"
186 let tag_pool t = pool t.doc
188 let compare a b = match a.node,b.node with
189 | Node(NC i),Node(NC j) -> compare i j
190 | _, Node(NC( _ )) -> 1
191 | Node(SC (i,_)),Node(SC (j,_)) -> compare i j
192 | Node(NC( _ )),Node(SC (_,_)) -> -1
193 | _, Node(SC (_,_)) -> 1
194 | String i, String j -> compare i j
195 | Node _ , String _ -> -1
200 let equal a b = (compare a b) == 0
202 let string t = match t.node with
203 | String i -> Text.get_text t.doc i
206 let norm (n : [`Tree ] node ) = if is_nil n then Nil else Node (NC n)
212 | String i -> Printf.sprintf "String %i" i
213 | Node (NC t) -> Printf.sprintf "Node (NC %i)" (int_of_node t)
214 | Node (SC (t,i)) -> Printf.sprintf "Node (SC (%i,%i))" (int_of_node t) (int_of_node i)
220 | Node(NC t) | Node(SC (_,t)) ->
221 if (Tree.root n.doc) == t
223 else Node(NC(Tree.parent n.doc t)) (* A parent node can never be a SC *)
226 { n with node = node' }
231 | Node (NC t) when is_leaf n.doc t ->
232 let txt = my_text n.doc t in
233 if Text.is_empty n.doc txt
235 else Node(SC (txt,Tree.nil))
237 let fs = first_child n.doc t in
238 let txt = prev_text n.doc fs in
239 if Text.is_empty n.doc txt
241 else Node (SC (txt, fs))
242 | Node(SC (i,_)) -> String i
243 | Nil | String _ -> failwith "first_child"
245 { n with node = node'}
251 | Node (SC (_,ns)) -> norm ns
253 let ns = next_sibling n.doc t in
254 let txt = next_text n.doc t in
255 if Text.is_empty n.doc txt
257 else Node (SC (txt, ns))
258 | Nil | String _ -> failwith "next_sibling"
260 { n with node = node'}
263 let left = first_child
264 let right = next_sibling
267 function { doc=d; node=Node(NC n)} -> node_xml_id d n
268 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id d i
272 function { node=Node(SC _) } -> Tag.pcdata
273 | { doc=d; node=Node(NC n)} -> tag_id d n
274 | _ -> failwith "tag"
277 function { node=Node(SC _) } -> ()
278 | { doc=d; node=Node(NC n)} -> tag_id d n
281 let string_below t id =
282 let pid = parent_doc t.doc id in
284 | Node(NC(i)) -> (is_ancestor t.doc i pid)
285 | Node(SC(i,_)) -> (is_ancestor t.doc (parent_doc t.doc i) pid)
289 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
291 let contains_old t s =
292 let regexp = Str.regexp_string s in
295 let _ = Str.search_forward regexp arg 0;
299 let rec find t = match t.node with
301 | String _ -> matching (string t)
302 | Node(_) -> (find (left t )) || (find (right t))
306 let print_xml_fast outc t =
307 let rec loop ?(print_right=true) t = match t.node with
309 | String (s) -> output_string outc (string t)
310 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
313 let tg = Tag.to_string (tag t) in
317 output_char outc '<';
318 output_string outc tg;
320 Nil -> output_string outc "/>"
321 | String _ -> assert false
322 | Node(_) when Tag.equal (tag l) Tag.attribute ->
323 (loop_attributes (left l);
324 match (right l).node with
325 | Nil -> output_string outc "/>"
327 output_char outc '>';
329 output_string outc "</";
330 output_string outc tg;
331 output_char outc '>' )
333 output_char outc '>';
335 output_string outc "</";
336 output_string outc tg;
338 );if print_right then loop r
339 and loop_attributes a =
344 match (left a).node with
346 | _ -> string (left(left a))
348 output_char outc ' ';
349 output_string outc (Tag.to_string (tag a));
350 output_string outc "=\"";
351 output_string outc value;
352 output_char outc '"';
353 loop_attributes (right a)
356 loop ~print_right:false t
359 let print_xml_fast outc t =
360 if Tag.to_string (tag t) = "" then
361 print_xml_fast outc (first_child t)
362 else print_xml_fast outc t
364 let traversal t = Tree.traversal t.doc
365 let full_traversal t =
369 | String i -> () (*ignore(Text.get_text t.doc i) *)
386 let _timings = Hashtbl.create 107
389 let time _ref f arg =
390 let t1 = Unix.gettimeofday () in
392 let t2 = Unix.gettimeofday () in
393 let t = (1000. *.(t2 -. t1)) in
395 let (time,count) = try
396 Hashtbl.find _timings _ref
401 and count = count + 1
403 Hashtbl.replace _timings _ref (time,count);r
408 let first_child_ doc node =
409 time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
410 let next_sibling_ doc node =
411 time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
413 let is_empty_ text node =
414 time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
416 let prev_text_ doc node =
417 time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
419 let my_text_ doc node =
420 time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
422 let next_text_ doc node =
423 time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
425 let is_leaf_ doc node =
426 time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
428 let node_xml_id_ doc node =
429 time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
431 let text_xml_id_ doc node =
432 time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
438 | Node (NC t) when is_leaf_ n.doc t ->
439 let txt = my_text_ n.doc t in
440 if is_empty_ n.doc txt
442 else Node(SC (txt,XML.Tree.nil))
444 let fs = first_child_ n.doc t in
445 let txt = prev_text_ n.doc fs in
446 if is_empty_ n.doc txt
448 else Node (SC (txt, fs))
449 | Node(SC (i,_)) -> String i
450 | Nil | String _ -> failwith "first_child"
452 { n with node = node'}
458 | Node (SC (_,ns)) -> norm ns
460 let ns = next_sibling_ n.doc t in
462 if XML.Tree.is_nil ns then
464 else prev_text_ n.doc ns
466 if is_empty_ n.doc txt
468 else Node (SC (txt, ns))
469 | Nil | String _ -> failwith "next_sibling"
471 { n with node = node'}
474 function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
475 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
478 (* Wrapper around critical function *)
479 let string t = time ("TextCollection.GetText()") (string) t
480 let left = first_child
481 let right = next_sibling
482 let tag t = time ("XMLTree.GetTag()") (tag) t
484 let print_stats ppf =
485 let total_time,total_calls =
486 Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
487 tacc+. t, cacc + c) _timings (0.,0)
491 "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
492 Hashtbl.iter (fun name (time,count) ->
493 Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
496 (100. *. (float_of_int count)/.(float_of_int total_calls))
497 (time /. (float_of_int count))
499 (100. *. time /. total_time)) _timings;
500 Format.fprintf ppf "-------------------------------------------------------------------\n";
501 Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
502 "Total" total_calls 100. total_time 100.
505 let print_xml_fast outc t =
506 let rec loop ?(print_right=true) t = match t.node with
508 | String (s) -> output_string outc (string t)
509 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
512 let tg = Tag.to_string (tag t) in
516 output_char outc '<';
517 output_string outc tg;
519 Nil -> output_string outc "/>"
520 | String _ -> assert false
521 | Node(_) when Tag.equal (tag l) Tag.attribute ->
522 (loop_attributes (left l);
523 match (right l).node with
524 | Nil -> output_string outc "/>"
526 output_char outc '>';
528 output_string outc "</";
529 output_string outc tg;
530 output_char outc '>' )
532 output_char outc '>';
534 output_string outc "</";
535 output_string outc tg;
537 );if print_right then loop r
538 and loop_attributes a =
543 match (left a).node with
545 | _ -> string (left(left a))
547 output_char outc ' ';
548 output_string outc (Tag.to_string (tag a));
549 output_string outc "=\"";
550 output_string outc value;
551 output_char outc '"';
552 loop_attributes (right a)
555 loop ~print_right:false t
558 let print_xml_fast outc t =
559 if Tag.to_string (tag t) = "" then
560 print_xml_fast outc (first_child t)
561 else print_xml_fast outc t
568 module Binary = DEBUGTREE