7cc631b0f1e9c6b0191eeeb7728145f438f11007
[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 let poa = TagSet.add Tag.pcdata (TagSet.singleton Tag.attribute)
25
26 let rec fill_hashtag t =
27   if Tree.Binary.is_node t then 
28     begin
29       let tag = Tree.Binary.tag t in
30       let a = 
31         if TagSet.mem tag poa
32         then 0
33         else
34           fill_hashtag (Tree.Binary.first_child t) 
35       in
36       let b = fill_hashtag (Tree.Binary.next_sibling t)
37       in a+b+1
38     end
39   else 0
40   
41
42 let test_slashslash tree k =
43   let test =
44     match k with "*" -> TagSet.remove (Tag.tag "") TagSet.star
45       | s -> TagSet.singleton (Tag.tag k)
46   in
47   let attorstring = TagSet.cup TagSet.pcdata TagSet.attribute in
48   let rec aux t acc =
49     if Tree.Binary.is_node t
50     then      
51       let tag = Tree.Binary.tag t in
52           let l = Tree.Binary.first_child t 
53           and r = Tree.Binary.next_sibling t
54           in
55           let acc = 
56             if TagSet.mem tag test
57             then
58               TS.append t acc
59             else
60               acc
61           in
62           let rl = if TagSet.mem tag attorstring then acc else aux l acc 
63           in aux r rl
64     else
65       acc
66   in
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)
70
71
72 let test_jump tree k =
73   let ttag = Tag.tag k in
74
75   let rec loop acc tree = 
76     if Tree.Binary.is_node tree 
77     then
78       let acc = TS.cons tree acc in
79         loop acc (Tree.Binary.tagged_foll tree ttag)
80     else
81       acc
82     
83   in
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)
87
88
89
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
93   let rec aux t =
94     if Tree.Binary.is_node t
95     then      
96       let tag = Tree.Binary.tag t in
97       let l = Tree.Binary.first_child t 
98       and r = Tree.Binary.next_sibling t
99       in
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))
104       in
105         aux l;
106         aux r;
107
108     else 
109       ()
110   in
111     aux tree
112
113
114   
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
120
121
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 
129     
130
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)
135
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)
140
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)
145
146 module Stack =
147 struct
148   type t = { mutable table: Tree.Binary.t array;
149              mutable top : int }
150
151   let empty = { table = Array.make 0 (Obj.magic 0);
152                 top = 0 }
153   let cons e s =
154     let ls = Array.length s.table in
155     if ls > s.top
156     then
157       begin 
158         s.table.(s.top) <- e;
159         s.top <- s.top + 1;
160         s
161       end
162     else
163
164       let a = Array.make (ls * 2 + 1) (Tree.Binary.root e)
165       in
166         Array.blit s.table 0 a 0 ls;
167         s.table <- a;
168         s.table.(s.top) <- e;
169         s.top <- s.top + 1;
170         s
171           
172 end
173
174 let test_fast tree = 
175   let rec aux t acc = 
176     if Tree.Binary.is_node t 
177     then 
178       aux (Tree.Binary.right t)( aux (Tree.Binary.left t) (Stack.cons t acc))    
179     else acc
180   in let _ = Printf.eprintf "Fast traversal ...%!" in
181     time (aux tree) Stack.empty
182
183 let test_cps tree =
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)
187     else match cont with
188       | [] -> acc
189       | p::r -> aux p acc r
190   in
191   let _ = Printf.eprintf "CPS traversal ...%!" in
192     time (aux tree Stack.empty) []
193
194
195 let main v query output =
196     let _ = Tag.init (Tree.Binary.tag_pool v) in
197       Printf.eprintf "Parsing query : ";    
198       let query = try
199         time
200           XPath.Parser.parse_string query
201       with
202           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
203       in
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;
210 *)
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 "");
214           begin
215             if !Options.count_only then
216               let result = time (BottomUpNew.run_count auto) v
217               in
218                 Printf.eprintf "Number of nodes in the result set : %i\n" result
219             else
220             let result = time (BottomUpNew.run auto) v in         
221               Printf.eprintf "Number of nodes in the result set : %i\n" (TS.length result);
222               begin
223                 match output with
224                   | None -> ()
225                   | Some f ->                 
226                       Printf.eprintf "Serializing results : ";
227                       time( fun () ->
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) ();
233               end;
234           end;
235           Printf.eprintf "Total running time : %fms\n%!" (total_time())
236 ;;
237                 
238 Options.parse_cmdline();;
239
240 let v = 
241   if (Filename.check_suffix !Options.input_file ".srx")
242   then 
243     begin
244       Printf.eprintf "Loading from file : ";
245       time (Tree.Binary.load  ~sample:!Options.sample_factor )
246         (Filename.chop_suffix !Options.input_file ".srx");
247     end
248   else 
249     let v = 
250       time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
251             in Printf.eprintf "Parsing document : %!";v
252            ) () 
253     in
254       if !Options.save_file <> ""
255       then begin
256         Printf.eprintf "Writing file to disk : ";
257         time (Tree.Binary.save v) !Options.save_file;
258       end;
259       v
260 in
261   main v !Options.query !Options.output_file;;
262
263 IFDEF DEBUG
264 THEN
265 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
266
267 Tree.DEBUGTREE.print_stats Format.err_formatter;;
268 Gc.full_major()
269 ENDIF