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;;
26 let test_slashslash tree k =
28 match k with "*" -> TagSet.remove (Tag.tag "") TagSet.star
29 | s -> TagSet.singleton (Tag.tag k)
31 let attorstring = TagSet.cup TagSet.pcdata TagSet.attribute in
33 if Tree.Binary.is_node t
35 let tag = Tree.Binary.tag t in
36 let l = Tree.Binary.first_child t
37 and r = Tree.Binary.next_sibling t
40 if TagSet.mem tag test
46 let rl = if TagSet.mem tag attorstring then acc else aux l acc
51 let _ = Printf.eprintf "Testing optimal //%s ... " k in
52 let r = time (aux tree ) TS.empty in
53 Printf.eprintf "Result set is %i nodes\n%!" (TS.length r)
56 let test_jump tree k =
57 let ttag = Tag.tag k in
59 let rec loop acc tree =
60 if Tree.Binary.is_node tree
62 let acc = TS.cons tree acc in
63 loop acc (Tree.Binary.tagged_foll tree ttag)
68 let _ = Printf.eprintf "Testing jumping for tag %s ... " k in
69 let r = time (loop TS.empty ) (Tree.Binary.tagged_next tree ttag) in
70 Printf.eprintf "Result set is %i nodes\n%!" (TS.length r)
74 let test_traversal tree k =
75 let ttag = Tag.tag k in
76 let iid t = if Tree.Binary.is_node t then Tree.Binary.id t else -1 in
78 if Tree.Binary.is_node t
80 let tag = Tree.Binary.tag t in
81 let l = Tree.Binary.first_child t
82 and r = Tree.Binary.next_sibling t
84 let _ = Printf.eprintf "Tree with id %i and tag=%s, tagged_desc %s is %i tagged_foll is %i\n%!"
85 (Tree.Binary.id t) (Tag.to_string tag) (k)
86 (iid (Tree.Binary.tagged_desc t ttag))
87 (iid (Tree.Binary.tagged_foll t ttag))
99 let test_count_subtree tree k =
100 let ttag = Tag.tag k in
101 let _ = Printf.eprintf "Counting subtrees with tag %s ... %!" k in
102 let r = time(Tree.Binary.subtree_tags tree) ttag in
103 Printf.eprintf "%i nodes \n%!" r
106 let test_contains tree s =
107 let _ = Printf.eprintf "Fetching DocIds containing %s ... %!" s in
108 time (fun s -> let r = Tree.Binary.contains tree s in
109 Tree.Binary.DocIdSet.iter
110 (fun t -> output_string stderr
111 (Tree.Binary.get_string tree t);
112 output_char stderr '\n') r ) s
115 let test_count_contains tree s =
116 let _ = Printf.eprintf "Counting DocIds containing %s ... %!" s in
117 let r = time (Tree.Binary.count_contains tree) s in
118 Printf.eprintf "%i documents ids\n%!" (r)
120 let test_contains_old tree s =
121 let _ = Printf.eprintf "Fetching (old) DocIds containing %s ... %!" s in
122 let r = time (Tree.Binary.contains_old tree) s in
123 Printf.eprintf "%i documents ids\n%!" (Tree.Binary.DocIdSet.cardinal r)
125 let test_contains_iter tree s =
126 let _ = Printf.eprintf "Fetching (old) DocIds containing %s ... %!" s in
127 let r = time (Tree.Binary.contains_iter tree) s in
128 Printf.eprintf "%i documents ids\n%!" (Tree.Binary.DocIdSet.cardinal r)
132 type t = { mutable table: Tree.Binary.t array;
135 let empty = { table = Array.make 0 (Obj.magic 0);
138 let ls = Array.length s.table in
142 s.table.(s.top) <- e;
148 let a = Array.make (ls * 2 + 1) (Tree.Binary.root e)
150 Array.blit s.table 0 a 0 ls;
152 s.table.(s.top) <- e;
160 if Tree.Binary.is_node t
162 aux (Tree.Binary.right t)( aux (Tree.Binary.left t) (Stack.cons t acc))
164 in let _ = Printf.eprintf "Fast traversal ...%!" in
165 time (aux tree) Stack.empty
168 let rec aux t acc cont =
169 if Tree.Binary.is_node t
170 then aux (Tree.Binary.left t) (Stack.cons t acc) ((Tree.Binary.right t)::cont)
173 | p::r -> aux p acc r
175 let _ = Printf.eprintf "CPS traversal ...%!" in
176 time (aux tree Stack.empty) []
179 let main v query output =
180 let _ = Tag.init (Tree.Binary.tag_pool v) in
181 Printf.eprintf "Parsing query : ";
184 XPath.Parser.parse_string query
186 Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
188 XPath.Ast.print Format.err_formatter query;
189 Format.fprintf Format.err_formatter "\n%!";
190 Printf.eprintf "Compiling query : ";
191 let auto,_ = time XPath.Compile.compile query in
192 Printf.eprintf "Execution time %s : " (if !Options.count_only then "(counting only)" else "");
194 if !Options.count_only then
195 let result = time (BottomUpNew.run_count auto) v
197 Printf.eprintf "Number of nodes in the result set : %i\n" result
199 let result = time (BottomUpNew.run auto) v in
200 Printf.eprintf "Number of nodes in the result set : %i\n" (TS.length result);
205 Printf.eprintf "Serializing results : ";
207 let oc = open_out f in
208 output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
209 TS.iter (fun t -> output_string oc "----------\n";
210 Tree.Binary.print_xml_fast oc t;
211 output_char oc '\n') result) ();
214 Printf.eprintf "Total running time : %fms\n%!" (total_time())
217 Options.parse_cmdline();;
220 if (Filename.check_suffix !Options.input_file ".srx")
223 Printf.eprintf "Loading from file : ";
224 time (Tree.Binary.load ~sample:!Options.sample_factor )
225 (Filename.chop_suffix !Options.input_file ".srx");
229 time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
230 in Printf.eprintf "Parsing document : %!";v
233 if !Options.save_file <> ""
235 Printf.eprintf "Writing file to disk : ";
236 time (Tree.Binary.save v) !Options.save_file;
240 main v !Options.query !Options.output_file;;
244 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
246 Tree.DEBUGTREE.print_stats Format.err_formatter;;