.
[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
8 open Ata
9 INCLUDE "utils.ml"
10 let () = init_timer();;
11
12 let enabled_gc = Gc.get()
13 let disabled_gc = { Gc.get() with
14                       Gc.max_overhead = 1000000; 
15                       Gc.space_overhead = 100 }
16 let hash x = 131*x/(x-1+1)
17
18 let test_loop tree tag =
19   let t' = Tree.tagged_descendant tree tag  Tree.root in
20   let f = Hashtbl.create 4096
21   in
22   let jump t _ =  Tree.tagged_following_below tree tag t Tree.root in
23   let g t ctx = 
24     if t == Tree.nil then 0
25     else 1+ ((Hashtbl.find f (hash 101)) (jump t ctx) ctx)
26   in
27   Hashtbl.add f (hash 101) g;
28   (Hashtbl.find f (hash 101)) t' Tree.root
29
30 let test_full tree = 
31   let root = Tree.root in
32   let fin = Tree.closing tree root in
33   let rec loop t = if t <= fin then
34   let tag = Tree.tag tree t in
35 (*  let _ = Tag.to_string tag in *)
36   if tag == Tag.pcdata then (ignore (Tree.get_text tree t)); 
37   let t = (Obj.magic ((Obj.magic t) + 1)) in
38   loop t
39   in
40   loop root
41
42
43 let test_loop2 tree tag =
44   let t' = Tree.tagged_descendant tree tag  Tree.root in
45   let f = Hashtbl.create 4096
46   in
47   let jump t _ =  Tree.tagged_following_below tree tag t Tree.root in
48   let rec g t ctx = 
49     if t == Tree.nil then 0
50     else 1+ (match (Hashtbl.find f (hash 101)) with
51                 `Foo ->g (jump t ctx) ctx
52             ) 
53   in
54   Hashtbl.add f (hash 101) `Foo;
55   g t' Tree.root
56
57 let test_text doc = 
58   let _ = Printf.eprintf "Contains(bree)" in
59   let _ = time (Tree.test_contains doc) "bree" in
60   let _ = Printf.eprintf "Contains(brain)" in
61   let _ = time (Tree.test_contains doc) "brain" in
62   let _ = Printf.eprintf "Contains(brain)" in
63   let i = time (Tree.test_contains doc) "brain" in
64   let _ = Printf.eprintf "%i\nContains(Australia)" i in
65   let i = time (Tree.test_contains doc) "AUSTRALIA" in
66   let _ = Printf.eprintf "%i\n Contains(1930)" i in
67   let i = time (Tree.test_contains doc) "1930" in
68   let _ = Printf.eprintf "%i\n startswith(bar)" i in
69   let i = time (Tree.test_prefix doc) "bar" in
70   let _ = Printf.eprintf "%i\n endswith(LAND)" i in
71   let i = time (Tree.test_suffix doc) "LAND" in
72   let _ = Printf.eprintf "%i\n =(2001)" i in
73   let i = time (Tree.test_equals doc) "2001" in
74   let _ = Printf.eprintf "%i\n =(Nguyen)" i in
75   let i = time (Tree.test_equals doc) "Nguyen" in
76   Printf.eprintf "%i\n" i ;
77   ()
78
79 type pointers
80 external build_pointers : Tree.t -> pointers = "caml_build_pointers"
81 external iter_pointers : pointers -> int = "caml_iter_pointers"
82 external free_pointers : pointers -> unit = "caml_free_pointers"
83
84
85 let main v query_string output =
86  
87     let _ = Tag.init (Tree.tag_pool v) in
88       Printf.eprintf "Parsing query : ";    
89       let query = try
90         time
91           XPath.Parser.parse_string query_string
92       with
93           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
94       in
95       let _ = Printf.eprintf "Number of nodes %i\n%!" (Tree.size v) in
96 (*      let _ = test_text v in *)
97 (*      let _ = Tree.stats v in
98       let _ = Printf.eprintf "Timing first_child/next_sibling %!" in
99       let _ = time (Tree.benchmark_fcns)  v in
100       let _ = Printf.eprintf "Timing last_child/prev_sibling %!" in
101       let _ = time (Tree.benchmark_lcps)  v in
102       let _ = Printf.eprintf "Timing jump to a %!" in
103       let _ = time (Tree.benchmark_jump v) (Tag.tag "a")  in      
104       let _ = Printf.eprintf "Timing pointer allocation %!" in
105       let pointers = time (build_pointers) v  in
106       let _ = Printf.eprintf "Timing pointer iteration %!" in
107       let i = time (iter_pointers) pointers  in
108       let _ = Printf.eprintf "Traversed %i pointers\n\nTiming pointer deallocation %!" i in
109       let _  = time (free_pointers) pointers  in *)
110 (*      let _ = Printf.eprintf "Timing //keyword :" in
111       let r = time (test_loop v) (Tag.tag "keyword") in
112       let _ = Printf.eprintf "Count is %i\n%!" r in 
113       let _ = Printf.eprintf "Timing //keyword 2:" in
114       let r = time (test_loop2 v) (Tag.tag "keyword") in
115       let _ = Printf.eprintf "Count is %i\n%!" r in  
116       let _ = Printf.eprintf "Timing //node() :" in
117       let _ = time (test_full)  v in      *)
118       XPath.Ast.print Format.err_formatter query;
119       Format.fprintf Format.err_formatter "\n%!";
120       Printf.eprintf "Compiling query : ";
121       let auto,ltags,contains = time (XPath.Compile.compile ~querystring:query_string) query in 
122       let _ = Ata.dump Format.err_formatter auto in
123       let _ = Printf.eprintf "%!" in
124       let jump_to = 
125         match contains with
126            None -> (max_int,`NOTHING)
127           | Some ((op,s)) -> 
128               let r = Tree.count v s 
129               in
130               Printf.eprintf "%i documents in the TextCollection\n" (Tree.text_size v);
131               Printf.eprintf "Global count is %i, using " r;
132               if r < !Options.tc_threshold then begin             
133                 Printf.eprintf "TextCollection contains\nCalling global contains : ";
134                 time (Tree.init_textfun op v) s;
135               end
136               else begin
137                 Printf.eprintf "Naive contains\nCalling global contains : ";
138                 time (Tree.init_naive_contains v) s
139               end;(r,`CONTAINS(s))
140       in
141       let test_list = jump_to in
142       (*
143         let test_list = 
144         if (!Options.backward) then begin
145         Printf.eprintf "Finding min occurences : ";
146         time 
147         ( List.fold_left (fun ((min_occ,kind)as acc)  (tag,_) ->
148                               let numtags = Tree.subtree_tags v tag Tree.root in
149                                 if  ((numtags < min_occ) && numtags >= 2)
150                                 then (numtags,`TAG(tag))
151                                 else acc) jump_to) ltags
152           end
153           else (max_int,`NOTHING)
154         in*)
155         let _ = if (snd test_list) != `NOTHING then
156           let occ,s1,s2 = match test_list with
157             | (x,`TAG (tag)) -> (x, "tag", (Tag.to_string tag))
158             | (x,`CONTAINS(s)) -> (x, "contains()", s)
159             | _ -> assert false
160           in
161             Printf.eprintf "Will jump to %s %s occuring %i time\n%!" s1 s2 occ
162         in
163           Printf.eprintf "Execution time %s : "
164             (if !Options.count_only then "(counting only)" else if !Options.backward then "(bottomup)" else "");
165           begin
166             let _ = Gc.full_major();Gc.compact() in
167             let _ = Printf.eprintf "%!" in
168 (*          let _ = Gc.set (disabled_gc) in *)
169               if !Options.backward && ((snd test_list) != `NOTHING )then 
170                 if !Options.count_only then
171                 let r = time_mem (bottom_up_count auto v )(snd test_list)  in
172                 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r 
173                 in ()
174                 else begin
175                 let r = time_mem (bottom_up auto v )(snd test_list)  in
176                 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" (IdSet.length r)
177                 in
178                   match output with
179
180                     | None -> ()
181                     | Some f ->               
182                         Printf.eprintf "Serializing results : ";
183                         time( fun () ->
184                                 (*let oc = open_out f in *)
185                                 let oc = Unix.openfile f [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
186                                 (*output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";*)
187                                 IdSet.iter (fun t -> 
188                                               Tree.print_xml_fast3 v t oc;
189                                               (*output_char oc '\n'; *)                         
190                                            ) r) ();
191                 end
192                      
193               else
194                 let _ = 
195                   if !Options.backward then Printf.eprintf "WARNING: couldn't find a jumping point, running top-down\n" 
196                 in
197                 if !Options.count_only then
198                   let r = time_mem ( top_down_count auto ) v in 
199                   let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
200                   in ()
201                 else      
202                 let module GR = Ata.Test(struct let doc = v end) in
203                   let result = time_mem (GR.top_down auto) v in
204                   let _ = Printf.eprintf "Counting results " in
205                   let rcount = time (GR.Results.length) result in
206                     Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
207                     Printf.eprintf "\n%!";
208                     begin
209                       match output with
210                         | None -> ()
211                         | Some f ->                   
212                             Printf.eprintf "Serializing results : ";
213                             time( fun () ->
214                                     (*let oc = open_out f in *)
215                                     let oc = Unix.openfile f [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
216                                       (*output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";*)
217                                     let t1 = ref (Unix.gettimeofday()) in
218                                     let count = ref 1 in
219                                     let old_count = ref 1 in
220                                      GR.Results.iter (fun t -> 
221                                                         incr count;
222                                                         begin
223                                                           if (!count mod 15) == 0
224                                                           then
225                                                           let t2 =  Unix.gettimeofday() in
226                                                           let _ = Printf.eprintf "Printing %i elements in %f ms\n"
227                                                             (!count - !old_count) (1000. *.(t2 -. !t1))
228                                                           in
229                                                           ( old_count := !count; t1 := Unix.gettimeofday())
230                                                         end;
231                                                         Tree.print_xml_fast3 v t oc;
232                                                         (*output_char oc '\n'; *)                               
233                                                      ) result) ();
234                     end;
235           end;
236           let _ = Gc.set enabled_gc in
237             Printf.eprintf "Total running time : %fms\n%!" (total_time())
238 ;;
239
240 Options.parse_cmdline();;
241
242 let v =
243   if (Filename.check_suffix !Options.input_file ".srx")
244   then 
245     begin
246       Printf.eprintf "Loading from file : ";
247       time (Tree.load  ~sample:!Options.sample_factor ~load_text:(not !Options.count_only))
248         !Options.input_file;
249         end
250   else 
251     let v = 
252       time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
253             in Printf.eprintf "Parsing document : %!";v
254            ) () 
255     in
256       if !Options.save_file <> ""
257       then begin
258         Printf.eprintf "Writing file to disk : ";
259         time (Tree.save v) !Options.save_file;
260       end;
261       v
262 in
263   main v !Options.query !Options.output_file;;
264