Removed testing cruft
[SXSI/xpathcomp.git] / main.ml
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 (******************************************************************************)
7 INCLUDE "debug.ml"
8
9 open Ata
10
11
12 let l = ref [] ;;
13 let time f x =
14   let t1 = Unix.gettimeofday () in
15   let r = f x in
16   let t2 = Unix.gettimeofday () in 
17   let t = (1000. *.(t2 -. t1)) in
18     l:= t::!l;
19     Printf.eprintf "  %fms\n%!" t ;
20     r
21 ;;
22 let total_time () =  List.fold_left (+.) 0. !l;;
23
24
25
26 let test_slashslash tree k =
27   let test =
28     match k with "*" -> TagSet.remove (Tag.tag "") TagSet.star
29       | s -> TagSet.singleton (Tag.tag k)
30   in
31   let attorstring = TagSet.cup TagSet.pcdata TagSet.attribute in
32   let rec aux t acc =
33     if Tree.Binary.is_node t
34     then      
35       let tag = Tree.Binary.tag t in
36           let l = Tree.Binary.first_child t 
37           and r = Tree.Binary.next_sibling t
38           in
39           let acc = 
40             if TagSet.mem tag test
41             then
42               TS.append t acc
43             else
44               acc
45           in
46           let rl = if TagSet.mem tag attorstring then acc else aux l acc 
47           in aux r rl
48     else
49       acc
50   in
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)
54
55
56 let test_jump tree k =
57   let ttag = Tag.tag k in
58
59   let rec loop acc tree = 
60     if Tree.Binary.is_node tree 
61     then
62       let acc = TS.cons tree acc in
63         loop acc (Tree.Binary.tagged_foll tree ttag)
64     else
65       acc
66     
67   in
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)
71
72
73
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
77   let rec aux t =
78     if Tree.Binary.is_node t
79     then      
80       let tag = Tree.Binary.tag t in
81       let l = Tree.Binary.first_child t 
82       and r = Tree.Binary.next_sibling t
83       in
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))
88       in
89         aux l;
90         aux r;
91
92     else 
93       ()
94   in
95     aux tree
96
97
98   
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
104
105
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 
113     
114
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)
119
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)
124
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)
129
130 module Stack =
131 struct
132   type t = { mutable table: Tree.Binary.t array;
133              mutable top : int }
134
135   let empty = { table = Array.make 0 (Obj.magic 0);
136                 top = 0 }
137   let cons e s =
138     let ls = Array.length s.table in
139     if ls > s.top
140     then
141       begin 
142         s.table.(s.top) <- e;
143         s.top <- s.top + 1;
144         s
145       end
146     else
147
148       let a = Array.make (ls * 2 + 1) (Tree.Binary.root e)
149       in
150         Array.blit s.table 0 a 0 ls;
151         s.table <- a;
152         s.table.(s.top) <- e;
153         s.top <- s.top + 1;
154         s
155           
156 end
157
158 let test_fast tree = 
159   let rec aux t acc = 
160     if Tree.Binary.is_node t 
161     then 
162       aux (Tree.Binary.right t)( aux (Tree.Binary.left t) (Stack.cons t acc))    
163     else acc
164   in let _ = Printf.eprintf "Fast traversal ...%!" in
165     time (aux tree) Stack.empty
166
167 let test_cps tree =
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)
171     else match cont with
172       | [] -> acc
173       | p::r -> aux p acc r
174   in
175   let _ = Printf.eprintf "CPS traversal ...%!" in
176     time (aux tree Stack.empty) []
177
178
179 let main v query output =
180     let _ = Tag.init (Tree.Binary.tag_pool v) in
181       Printf.eprintf "Parsing query : ";    
182       let query = try
183         time
184           XPath.Parser.parse_string query
185       with
186           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
187       in
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 "");
193           begin
194             if !Options.count_only then
195               let result = time (BottomUpNew.run_count auto) v
196               in
197                 Printf.eprintf "Number of nodes in the result set : %i\n" result
198             else
199             let result = time (BottomUpNew.run auto) v in         
200               Printf.eprintf "Number of nodes in the result set : %i\n" (TS.length result);
201               begin
202                 match output with
203                   | None -> ()
204                   | Some f ->                 
205                       Printf.eprintf "Serializing results : ";
206                       time( fun () ->
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) ();
212               end;
213           end;
214           Printf.eprintf "Total running time : %fms\n%!" (total_time())
215 ;;
216                 
217 Options.parse_cmdline();;
218
219 let v = 
220   if (Filename.check_suffix !Options.input_file ".srx")
221   then 
222     begin
223       Printf.eprintf "Loading from file : ";
224       time (Tree.Binary.load  ~sample:!Options.sample_factor )
225         (Filename.chop_suffix !Options.input_file ".srx");
226     end
227   else 
228     let v = 
229       time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
230             in Printf.eprintf "Parsing document : %!";v
231            ) () 
232     in
233       if !Options.save_file <> ""
234       then begin
235         Printf.eprintf "Writing file to disk : ";
236         time (Tree.Binary.save v) !Options.save_file;
237       end;
238       v
239 in
240   main v !Options.query !Options.output_file;;
241
242 IFDEF DEBUG
243 THEN
244 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
245
246 Tree.DEBUGTREE.print_stats Format.err_formatter;;
247 Gc.full_major()
248 ENDIF