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