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 (******************************************************************************)
14 let t1 = Unix.gettimeofday () in
16 let t2 = Unix.gettimeofday () in
17 let t = (1000. *.(t2 -. t1)) in
19 Printf.eprintf " %fms\n%!" t ;
22 let total_time () = List.fold_left (+.) 0. !l;;
24 let poa = TagSet.add Tag.pcdata (TagSet.singleton Tag.attribute)
26 let rec fill_hashtag t =
27 if Tree.Binary.is_node t then
29 let tag = Tree.Binary.tag t in
34 fill_hashtag (Tree.Binary.first_child t)
36 let b = fill_hashtag (Tree.Binary.next_sibling t)
42 let test_slashslash tree k =
44 match k with "*" -> TagSet.remove (Tag.tag "") TagSet.star
45 | s -> TagSet.singleton (Tag.tag k)
47 let attorstring = TagSet.cup TagSet.pcdata TagSet.attribute in
49 if Tree.Binary.is_node t
51 let tag = Tree.Binary.tag t in
52 let l = Tree.Binary.first_child t
53 and r = Tree.Binary.next_sibling t
56 if TagSet.mem tag test
62 let rl = if TagSet.mem tag attorstring then acc else aux l acc
67 let _ = Printf.eprintf "Testing optimal //%s ... " k in
68 let r = time (aux tree ) TS.empty in
69 Printf.eprintf "Result set is %i nodes\n%!" (TS.length r)
72 let test_jump tree k =
73 let ttag = Tag.tag k in
75 let rec loop acc tree =
76 if Tree.Binary.is_node tree
78 let acc = TS.cons tree acc in
79 loop acc (Tree.Binary.tagged_foll tree ttag)
84 let _ = Printf.eprintf "Testing jumping for tag %s ... " k in
85 let r = time (loop TS.empty ) (Tree.Binary.tagged_next tree ttag) in
86 Printf.eprintf "Result set is %i nodes\n%!" (TS.length r)
90 let test_traversal tree k =
91 let ttag = Tag.tag k in
92 let iid t = if Tree.Binary.is_node t then Tree.Binary.id t else -1 in
94 if Tree.Binary.is_node t
96 let tag = Tree.Binary.tag t in
97 let l = Tree.Binary.first_child t
98 and r = Tree.Binary.next_sibling t
100 let _ = Printf.eprintf "Tree with id %i and tag=%s, tagged_desc %s is %i tagged_foll is %i\n%!"
101 (Tree.Binary.id t) (Tag.to_string tag) (k)
102 (iid (Tree.Binary.tagged_desc t ttag))
103 (iid (Tree.Binary.tagged_foll t ttag))
115 let test_count_subtree tree k =
116 let ttag = Tag.tag k in
117 let _ = Printf.eprintf "Counting subtrees with tag %s ... %!" k in
118 let r = time(Tree.Binary.subtree_tags tree) ttag in
119 Printf.eprintf "%i nodes \n%!" r
122 let test_contains tree s =
123 let _ = Printf.eprintf "Fetching DocIds containing %s ... %!" s in
124 time (fun s -> let r = Tree.Binary.contains tree s in
125 Tree.Binary.DocIdSet.iter
126 (fun t -> output_string stderr
127 (Tree.Binary.get_string tree t);
128 output_char stderr '\n') r ) s
131 let test_count_contains tree s =
132 let _ = Printf.eprintf "Counting DocIds containing %s ... %!" s in
133 let r = time (Tree.Binary.count_contains tree) s in
134 Printf.eprintf "%i documents ids\n%!" (r)
136 let test_contains_old tree s =
137 let _ = Printf.eprintf "Fetching (old) DocIds containing %s ... %!" s in
138 let r = time (Tree.Binary.contains_old tree) s in
139 Printf.eprintf "%i documents ids\n%!" (Tree.Binary.DocIdSet.cardinal r)
141 let test_contains_iter tree s =
142 let _ = Printf.eprintf "Fetching (old) DocIds containing %s ... %!" s in
143 let r = time (Tree.Binary.contains_iter tree) s in
144 Printf.eprintf "%i documents ids\n%!" (Tree.Binary.DocIdSet.cardinal r)
148 type t = { mutable table: Tree.Binary.t array;
151 let empty = { table = Array.make 0 (Obj.magic 0);
154 let ls = Array.length s.table in
158 s.table.(s.top) <- e;
164 let a = Array.make (ls * 2 + 1) (Tree.Binary.root e)
166 Array.blit s.table 0 a 0 ls;
168 s.table.(s.top) <- e;
176 if Tree.Binary.is_node t
178 aux (Tree.Binary.right t)( aux (Tree.Binary.left t) (Stack.cons t acc))
180 in let _ = Printf.eprintf "Fast traversal ...%!" in
181 time (aux tree) Stack.empty
184 let rec aux t acc cont =
185 if Tree.Binary.is_node t
186 then aux (Tree.Binary.left t) (Stack.cons t acc) ((Tree.Binary.right t)::cont)
189 | p::r -> aux p acc r
191 let _ = Printf.eprintf "CPS traversal ...%!" in
192 time (aux tree Stack.empty) []
195 let main v query output =
196 let _ = Tag.init (Tree.Binary.tag_pool v) in
197 Printf.eprintf "Parsing query : ";
200 XPath.Parser.parse_string query
202 Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
204 XPath.Ast.print Format.err_formatter query;
205 Format.fprintf Format.err_formatter "\n%!";
206 (* Printf.eprintf "Dummy iteration : ";
207 time (fill_hashtag) v;
208 Printf.eprintf "Dummy iteration (tag access cached) : ";
209 time (fill_hashtag) v;
211 Printf.eprintf "Compiling query : ";
212 let auto,_ = time XPath.Compile.compile query in
213 Printf.eprintf "Execution time %s : " (if !Options.count_only then "(counting only)" else "");
215 if !Options.count_only then
216 let result = time (BottomUpNew.run_count auto) v
218 Printf.eprintf "Number of nodes in the result set : %i\n" result
220 let result = time (BottomUpNew.run auto) v in
221 Printf.eprintf "Number of nodes in the result set : %i\n" (TS.length result);
226 Printf.eprintf "Serializing results : ";
228 let oc = open_out f in
229 output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
230 TS.iter (fun t -> output_string oc "----------\n";
231 Tree.Binary.print_xml_fast oc t;
232 output_char oc '\n') result) ();
235 Printf.eprintf "Total running time : %fms\n%!" (total_time())
238 Options.parse_cmdline();;
241 if (Filename.check_suffix !Options.input_file ".srx")
244 Printf.eprintf "Loading from file : ";
245 time (Tree.Binary.load ~sample:!Options.sample_factor )
246 (Filename.chop_suffix !Options.input_file ".srx");
250 time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
251 in Printf.eprintf "Parsing document : %!";v
254 if !Options.save_file <> ""
256 Printf.eprintf "Writing file to disk : ";
257 time (Tree.Binary.save v) !Options.save_file;
261 main v !Options.query !Options.output_file;;
265 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
267 Tree.DEBUGTREE.print_stats Format.err_formatter;;