26b445e786a72b3d36cdd0cee3fbcb4f4e810dd5
[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 type pointers
58 external build_pointers : Tree.t -> pointers = "caml_build_pointers"
59 external iter_pointers : pointers -> int = "caml_iter_pointers"
60 external free_pointers : pointers -> unit = "caml_free_pointers"
61
62
63 let main v query_string output =
64  
65     let _ = Tag.init (Tree.tag_pool v) in
66       Printf.eprintf "Parsing query : ";    
67       let query = try
68         time
69           XPath.Parser.parse_string query_string
70       with
71           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
72       in
73       let _ = Printf.eprintf "Number of nodes %i\n%!" (Tree.size v) in
74       let _ = Tree.stats v in
75       let _ = Printf.eprintf "Timing first_child/next_sibling %!" in
76       let _ = time (Tree.benchmark_fcns)  v in
77       let _ = Printf.eprintf "Timing last_child/prev_sibling %!" in
78       let _ = time (Tree.benchmark_lcps)  v in
79       let _ = Printf.eprintf "Timing jump to a %!" in
80       let _ = time (Tree.benchmark_jump v) (Tag.tag "a")  in
81       
82 (*      let _ = Printf.eprintf "Timing pointer allocation %!" in
83       let pointers = time (build_pointers) v  in
84       let _ = Printf.eprintf "Timing pointer iteration %!" in
85       let i = time (iter_pointers) pointers  in
86       let _ = Printf.eprintf "Traversed %i pointers\n\nTiming pointer deallocation %!" i in
87       let _  = time (free_pointers) pointers  in *)
88 (*      let _ = Printf.eprintf "Timing //keyword :" in
89       let r = time (test_loop v) (Tag.tag "keyword") in
90       let _ = Printf.eprintf "Count is %i\n%!" r in 
91       let _ = Printf.eprintf "Timing //keyword 2:" in
92       let r = time (test_loop2 v) (Tag.tag "keyword") in
93       let _ = Printf.eprintf "Count is %i\n%!" r in  
94       let _ = Printf.eprintf "Timing //node() :" in
95       let _ = time (test_full)  v in      *)
96       XPath.Ast.print Format.err_formatter query;
97       Format.fprintf Format.err_formatter "\n%!";
98       Printf.eprintf "Compiling query : ";
99       let auto,ltags,contains = time (XPath.Compile.compile ~querystring:query_string) query in 
100       let _ = Ata.dump Format.err_formatter auto in
101       let _ = Printf.eprintf "%!" in
102       let jump_to = 
103         match contains with
104            None -> (max_int,`NOTHING)
105           | Some ((op,s)) -> 
106               let r = Tree.count v s 
107               in
108               Printf.eprintf "%i documents in the TextCollection\n" (Tree.text_size v);
109               Printf.eprintf "Global count is %i, using " r;
110               if r < !Options.tc_threshold then begin             
111                 Printf.eprintf "TextCollection contains\nCalling global contains : ";
112                 time (Tree.init_textfun op v) s;
113               end
114               else begin
115                 Printf.eprintf "Naive contains\nCalling global contains : ";
116                 time (Tree.init_naive_contains v) s
117               end;(r,`CONTAINS(s))
118       in
119       let test_list = jump_to in
120       (*
121         let test_list = 
122         if (!Options.backward) then begin
123         Printf.eprintf "Finding min occurences : ";
124         time 
125         ( List.fold_left (fun ((min_occ,kind)as acc)  (tag,_) ->
126                               let numtags = Tree.subtree_tags v tag Tree.root in
127                                 if  ((numtags < min_occ) && numtags >= 2)
128                                 then (numtags,`TAG(tag))
129                                 else acc) jump_to) ltags
130           end
131           else (max_int,`NOTHING)
132         in*)
133         let _ = if (snd test_list) != `NOTHING then
134           let occ,s1,s2 = match test_list with
135             | (x,`TAG (tag)) -> (x, "tag", (Tag.to_string tag))
136             | (x,`CONTAINS(s)) -> (x, "contains()", s)
137             | _ -> assert false
138           in
139             Printf.eprintf "Will jump to %s %s occuring %i time\n%!" s1 s2 occ
140         in
141           Printf.eprintf "Execution time %s : "
142             (if !Options.count_only then "(counting only)" else if !Options.backward then "(bottomup)" else "");
143           begin
144             let _ = Gc.full_major();Gc.compact() in
145             let _ = Printf.eprintf "%!" in
146 (*          let _ = Gc.set (disabled_gc) in *)
147               if !Options.backward && ((snd test_list) != `NOTHING )then 
148                 if !Options.count_only then
149                 let r = time_mem (bottom_up_count auto v )(snd test_list)  in
150                 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r 
151                 in ()
152                 else begin
153                 let r = time_mem (bottom_up auto v )(snd test_list)  in
154                 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" (IdSet.length r)
155                 in
156                   match output with
157
158                     | None -> ()
159                     | Some f ->               
160                         Printf.eprintf "Serializing results : ";
161                         time( fun () ->
162                                 (*let oc = open_out f in *)
163                                 let oc = Unix.openfile f [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
164                                 (*output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";*)
165                                 IdSet.iter (fun t -> 
166                                               Tree.print_xml_fast3 v t oc;
167                                               (*output_char oc '\n'; *)                         
168                                            ) r) ();
169                 end
170                      
171               else
172                 let _ = 
173                   if !Options.backward then Printf.eprintf "WARNING: couldn't find a jumping point, running top-down\n" 
174                 in
175                 if !Options.count_only then
176                   let r = time_mem ( top_down_count auto ) v in 
177                   let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
178                   in ()
179                 else      
180                 let module GR = Ata.Test(struct let doc = v end) in
181                   let result = time_mem (GR.top_down auto) v in
182                   let _ = Printf.eprintf "Counting results " in
183                   let rcount = time (GR.Results.length) result in
184                     Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
185                     Printf.eprintf "\n%!";
186                     begin
187                       match output with
188                         | None -> ()
189                         | Some f ->                   
190                             Printf.eprintf "Serializing results : ";
191                             time( fun () ->
192                                     (*let oc = open_out f in *)
193                                     let oc = Unix.openfile f [ Unix.O_WRONLY;Unix.O_TRUNC;Unix.O_CREAT] 0o644 in
194                                       (*output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";*)
195                                     let t1 = ref (Unix.gettimeofday()) in
196                                     let count = ref 1 in
197                                     let old_count = ref 1 in
198                                      GR.Results.iter (fun t -> 
199                                                         incr count;
200                                                         begin
201                                                           if (!count mod 15) == 0
202                                                           then
203                                                           let t2 =  Unix.gettimeofday() in
204                                                           let _ = Printf.eprintf "Printing %i elements in %f ms\n"
205                                                             (!count - !old_count) (1000. *.(t2 -. !t1))
206                                                           in
207                                                           ( old_count := !count; t1 := Unix.gettimeofday())
208                                                         end;
209                                                         Tree.print_xml_fast3 v t oc;
210                                                         (*output_char oc '\n'; *)                               
211                                                      ) result) ();
212                     end;
213           end;
214           let _ = Gc.set enabled_gc in
215             Printf.eprintf "Total running time : %fms\n%!" (total_time())
216 ;;
217
218 Options.parse_cmdline();;
219
220 let v =
221   if (Filename.check_suffix !Options.input_file ".srx")
222   then 
223     begin
224       Printf.eprintf "Loading from file : ";
225       time (Tree.load  ~sample:!Options.sample_factor ~load_text:(not !Options.count_only))
226         !Options.input_file;
227         end
228   else 
229     let v = 
230       time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
231             in Printf.eprintf "Parsing document : %!";v
232            ) () 
233     in
234       if !Options.save_file <> ""
235       then begin
236         Printf.eprintf "Writing file to disk : ";
237         time (Tree.save v) !Options.save_file;
238       end;
239       v
240 in
241   main v !Options.query !Options.output_file;;
242