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
26 val print_xml_fast : out_channel -> t -> unit
27 val compare : t -> t -> int
28 val equal : t -> t -> bool
33 with type elt = string_content
34 val string_below : t -> string_content -> bool
35 val contains : t -> string -> DocIdSet.t
36 val contains_old : t -> string -> bool
38 val get_string : t -> string_content -> string
46 type node_kind = [`Text | `Tree ]
48 let compare : 'a node -> 'a node -> int = (-)
49 let equal : 'a node -> 'a node -> bool = (==)
51 (* abstract type, values are pointers to a XMLTree C++ object *)
53 external int_of_node : 'a node -> int = "%identity"
55 external parse_xml_uri : string -> int -> bool -> bool -> t = "caml_call_shredder_uri"
56 external parse_xml_string : string -> int -> bool -> bool -> t = "caml_call_shredder_string"
58 external save_tree : t -> string -> unit = "caml_xml_tree_save"
59 external load_tree : string -> int -> t = "caml_xml_tree_load"
64 let equal : [`Text] node -> [`Text] node -> bool = equal
67 external nullt : unit -> [`Text ] node = "caml_xml_tree_nullt"
69 external get_text : t -> [`Text] node -> string = "caml_text_collection_get_text"
72 if equal nil n then ""
75 external is_empty : t -> [`Text ] node -> bool = "caml_text_collection_empty_text"
77 (equal nil n) || is_empty t n
79 external is_contains : t -> string -> bool = "caml_text_collection_is_contains"
80 external count_contains : t -> string -> int = "caml_text_collection_count_contains"
81 external contains : t -> string -> [`Text ] node array = "caml_text_collection_contains"
88 let equal : [`Tree ] node -> [`Tree] node -> bool = equal
89 external serialize : t -> string -> unit = "caml_xml_tree_serialize"
90 external unserialize : string -> t = "caml_xml_tree_unserialize"
92 external root : t -> [`Tree] node = "caml_xml_tree_root"
93 external nullt : unit -> [`Tree ] node = "caml_xml_tree_nullt"
96 let is_nil x = equal x nil
98 external parent : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
99 external parent_doc : t -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
100 external first_child : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
104 external next_sibling : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
106 external is_leaf : t -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
108 (* external tag : t -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
109 external tag_id : t -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id"
111 let is_last t n = equal nil (next_sibling t n)
113 external prev_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
116 external my_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
117 external next_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
119 external text_xml_id : t -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
120 external node_xml_id : t -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
121 external is_ancestor : t -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
126 then Printf.eprintf "#\n"
129 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%!"
131 (Tag.to_string (tag_id t id))
133 (int_of_node (prev_text t id))
134 (Text.get_text t (prev_text t id))
135 (int_of_node (my_text t id))
136 (Text.get_text t (my_text t id))
137 (int_of_node (next_text t id))
138 (Text.get_text t (next_text t id))
139 (int_of_node(parent_doc t (my_text t id)));
141 aux(first_child t id);
142 aux(next_sibling t id);
152 (* ignore (tag t id);
153 ignore (Text.get_text t (prev_text t id));
155 then ignore (Text.get_text t (my_text t id));
157 then ignore (Text.get_text t (next_text t id)); *)
158 aux (first_child t id);
159 aux (next_sibling t id);
166 module Binary = struct
170 | SC of [`Text ] node * [`Tree ] node
171 type string_content = [ `Text ] node
174 | Node of node_content
175 | String of string_content
179 type t = { doc : doc;
182 let dump { doc=t } = Tree.print_skel t
183 module DocIdSet = struct
184 include Set.Make (struct type t = string_content
185 let compare = (-) end)
188 let get_string t (i:string_content) = Text.get_text t.doc i
190 let node_of_t t = { doc= t;
191 node = Node(NC (root t)) }
194 let parse_xml_uri str = node_of_t
195 (MM((parse_xml_uri str
196 !Options.sample_factor
197 !Options.index_empty_texts
198 !Options.disable_text_collection),__LOCATION__))
200 let parse_xml_string str = node_of_t
201 (MM((parse_xml_string str
202 !Options.sample_factor
203 !Options.index_empty_texts
204 !Options.disable_text_collection),__LOCATION__))
207 let save t str = save_tree t.doc str
209 let load ?(sample=64) str = node_of_t (load_tree str sample)
212 external pool : doc -> Tag.pool = "%identity"
213 let tag_pool t = pool t.doc
215 let compare a b = match a.node,b.node with
216 | Node(NC i),Node(NC j) -> compare i j
217 | _, Node(NC( _ )) -> 1
218 | Node(SC (i,_)),Node(SC (j,_)) -> compare i j
219 | Node(NC( _ )),Node(SC (_,_)) -> -1
220 | _, Node(SC (_,_)) -> 1
221 | String i, String j -> compare i j
222 | Node _ , String _ -> -1
227 let equal a b = (compare a b) == 0
229 let string t = match t.node with
230 | String i -> Text.get_text t.doc i
233 let norm (n : [`Tree ] node ) = if is_nil n then Nil else Node (NC n)
239 | String i -> Printf.sprintf "String %i" i
240 | Node (NC t) -> Printf.sprintf "Node (NC %i)" (int_of_node t)
241 | Node (SC (t,i)) -> Printf.sprintf "Node (SC (%i,%i))" (int_of_node t) (int_of_node i)
247 | Node(NC t) | Node(SC (_,t)) ->
248 if (Tree.root n.doc) == t
250 else Node(NC(Tree.parent n.doc t)) (* A parent node can never be a SC *)
253 { n with node = node' }
258 | Node (NC t) when is_leaf n.doc t ->
259 let txt = my_text n.doc t in
260 if Text.is_empty n.doc txt
262 else Node(SC (txt,Tree.nil))
264 let fs = first_child n.doc t in
265 let txt = prev_text n.doc fs in
266 if Text.is_empty n.doc txt
268 else Node (SC (txt, fs))
269 | Node(SC (i,_)) -> String i
270 | Nil | String _ -> failwith "first_child"
272 { n with node = node'}
278 | Node (SC (_,ns)) -> norm ns
280 let ns = next_sibling n.doc t in
281 let txt = next_text n.doc t in
282 if Text.is_empty n.doc txt
284 else Node (SC (txt, ns))
285 | Nil | String _ -> failwith "next_sibling"
287 { n with node = node'}
290 let left = first_child
291 let right = next_sibling
294 function { doc=d; node=Node(NC n)} -> node_xml_id d n
295 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id d i
299 function { node=Node(SC _) } -> Tag.pcdata
300 | { doc=d; node=Node(NC n)} -> tag_id d n
301 | _ -> failwith "tag"
304 function { node=Node(SC _) } -> ()
305 | { doc=d; node=Node(NC n)} -> tag_id d n
308 let string_below t id =
309 let strid = parent_doc t.doc id in
312 (Tree.equal i strid) || (is_ancestor t.doc i strid)
313 | Node(SC(i,_)) -> Text.equal i id
318 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
320 let contains_old t s =
321 let regexp = Str.regexp_string s in
324 let _ = Str.search_forward regexp arg 0;
328 let rec find t = match t.node with
330 | String _ -> matching (string t)
331 | Node(_) -> (find (left t )) || (find (right t))
335 let print_xml_fast outc t =
336 let rec loop ?(print_right=true) t = match t.node with
338 | String (s) -> output_string outc (string t)
339 | Node _ when Tag.equal (tag t) Tag.pcdata ->
341 if print_right then loop (right t)
344 let tg = Tag.to_string (tag t) in
348 output_char outc '<';
349 output_string outc tg;
351 Nil -> output_string outc "/>"
352 | String _ -> assert false
353 | Node(_) when Tag.equal (tag l) Tag.attribute ->
354 (loop_attributes (left l);
355 match (right l).node with
356 | Nil -> output_string outc "/>"
358 output_char outc '>';
360 output_string outc "</";
361 output_string outc tg;
362 output_char outc '>' )
364 output_char outc '>';
366 output_string outc "</";
367 output_string outc tg;
369 );if print_right then loop r
370 and loop_attributes a =
375 match (left a).node with
377 | _ -> string (left(left a))
379 output_char outc ' ';
380 output_string outc (Tag.to_string (tag a));
381 output_string outc "=\"";
382 output_string outc value;
383 output_char outc '"';
384 loop_attributes (right a)
387 loop ~print_right:false t
390 let print_xml_fast outc t =
391 if Tag.to_string (tag t) = "" then
392 print_xml_fast outc (first_child t)
393 else print_xml_fast outc t
395 let traversal t = Tree.traversal t.doc
396 let full_traversal t =
400 | String i -> () (*ignore(Text.get_text t.doc i) *)
407 let print_stats _ = ()
419 let _timings = Hashtbl.create 107
422 let time _ref f arg =
423 let t1 = Unix.gettimeofday () in
425 let t2 = Unix.gettimeofday () in
426 let t = (1000. *.(t2 -. t1)) in
428 let (time,count) = try
429 Hashtbl.find _timings _ref
434 and count = count + 1
436 Hashtbl.replace _timings _ref (time,count);r
441 let first_child_ doc node =
442 time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
443 let next_sibling_ doc node =
444 time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
446 let is_empty_ text node =
447 time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
449 let prev_text_ doc node =
450 time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
452 let my_text_ doc node =
453 time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
455 let next_text_ doc node =
456 time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
458 let is_leaf_ doc node =
459 time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
461 let node_xml_id_ doc node =
462 time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
464 let text_xml_id_ doc node =
465 time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
471 | Node (NC t) when is_leaf_ n.doc t ->
472 let txt = my_text_ n.doc t in
473 if is_empty_ n.doc txt
475 else Node(SC (txt,XML.Tree.nil))
477 let fs = first_child_ n.doc t in
478 let txt = prev_text_ n.doc fs in
479 if is_empty_ n.doc txt
481 else Node (SC (txt, fs))
482 | Node(SC (i,_)) -> String i
483 | Nil | String _ -> failwith "first_child"
485 { n with node = node'}
491 | Node (SC (_,ns)) -> norm ns
493 let ns = next_sibling_ n.doc t in
495 if XML.Tree.is_nil ns then
497 else prev_text_ n.doc ns
499 if is_empty_ n.doc txt
501 else Node (SC (txt, ns))
502 | Nil | String _ -> failwith "next_sibling"
504 { n with node = node'}
507 function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
508 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
511 (* Wrapper around critical function *)
512 let string t = time ("TextCollection.GetText()") (string) t
513 let left = first_child
514 let right = next_sibling
515 let tag t = time ("XMLTree.GetTag()") (tag) t
517 let print_stats ppf =
518 let total_time,total_calls =
519 Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
520 tacc+. t, cacc + c) _timings (0.,0)
524 "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
525 Hashtbl.iter (fun name (time,count) ->
526 Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
529 (100. *. (float_of_int count)/.(float_of_int total_calls))
530 (time /. (float_of_int count))
532 (100. *. time /. total_time)) _timings;
533 Format.fprintf ppf "-------------------------------------------------------------------\n";
534 Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
535 "Total" total_calls 100. total_time 100.
538 let print_xml_fast outc t =
539 let rec loop ?(print_right=true) t = match t.node with
541 | String (s) -> output_string outc (string t)
542 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
545 let tg = Tag.to_string (tag t) in
549 output_char outc '<';
550 output_string outc tg;
552 Nil -> output_string outc "/>"
553 | String _ -> assert false
554 | Node(_) when Tag.equal (tag l) Tag.attribute ->
555 (loop_attributes (left l);
556 match (right l).node with
557 | Nil -> output_string outc "/>"
559 output_char outc '>';
561 output_string outc "</";
562 output_string outc tg;
563 output_char outc '>' )
565 output_char outc '>';
567 output_string outc "</";
568 output_string outc tg;
570 );if print_right then loop r
571 and loop_attributes a =
576 match (left a).node with
578 | _ -> string (left(left a))
580 output_char outc ' ';
581 output_string outc (Tag.to_string (tag a));
582 output_string outc "=\"";
583 output_string outc value;
584 output_char outc '"';
585 loop_attributes (right a)
588 loop ~print_right:false t
591 let print_xml_fast outc t =
592 if Tag.to_string (tag t) = "" then
593 print_xml_fast outc (first_child t)
594 else print_xml_fast outc t
601 module Binary = DEBUGTREE
603 module Binary = XML.Binary
604 END (* IFDEF DEBUG *)