605c0e6e520f030c8fbdf034852d072774871539
[SXSI/xpathcomp.git] / src / 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 "utils.ml"
8
9 open Ata
10
11 let () = init_timer();;
12
13
14 let default_gc = Gc.get()
15 let tuned_gc = { default_gc with
16   Gc.minor_heap_size = 32*1024*1024;
17   Gc.major_heap_increment = 8*1024*1024;
18   Gc.max_overhead = 1000000;
19   Gc.space_overhead = 100;
20                }
21
22 let mk_runtime run auto doc arg count print outfile =
23   fun () ->
24     if !Options.do_perf then start_perf ();
25     let r = time ~count:1 ~msg:"Execution time" (run auto doc) arg in
26     if !Options.do_perf then stop_perf ();
27     Printf.eprintf "Number of results: %i\n%!" (count r);
28     match outfile with
29         None -> ()
30       | Some file ->
31         time ~count:1 ~msg:"Serialization time" (print file doc) r
32 ;;
33
34
35 let main v query_string output =
36   Tag.init (Tree.tag_operations v);
37   let query =
38     time ~msg:"Parsing query" XPath.parse query_string
39   in
40   if !Options.verbose then begin
41     Printf.eprintf "Parsed query:\n%!";
42     XPath.Ast.print Format.err_formatter query;
43     Format.fprintf Format.err_formatter "\n%!"
44   end;
45   let auto, bu_info =
46     time ~msg:"Compiling query" (Compile.compile) query
47   in
48   if !Options.verbose then Ata.print Format.err_formatter auto;
49   Gc.full_major();
50   Gc.compact();
51   Gc.set (tuned_gc);
52   let runtime =
53   match !Options.bottom_up, bu_info with
54
55     | true, Some [ (query, pattern) ] ->
56       (* let nodes =
57         time
58           ~count:1 ~msg:"Computing full text query"
59           (Tree.full_text_query query v) pattern
60       in
61       let nodes = Array.to_list nodes in *)
62       if !Options.count_only then
63         let module R = ResJIT.Count in
64         let module M = Runtime.Make(R) in
65         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize None
66       else
67         let module R = ResJIT.Mat in
68         let module M = Runtime.Make(R) in
69         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Options.output_file
70
71     | _ ->
72         (* run the query top_down *)
73
74       if !Options.bottom_up then
75         Printf.eprintf "Cannot run the query in bottom-up mode, using top-down evaluator\n%!";
76
77       if !Options.count_only then
78         let module R = ResJIT.Count in
79         let module M = Runtime.Make(R) in
80         (* mk_runtime run auto doc arg count print outfile  *)
81         if !Options.twopass then 
82           mk_runtime M.twopass_top_down_run auto v Tree.root R.NS.length R.NS.serialize None
83         else
84           mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize None
85       else
86         let module R = ResJIT.Mat in
87         let module M = Runtime.Make(R) in
88         mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
89   in
90   runtime ()
91 ;;
92
93 let () = Options.parse_cmdline()
94 ;;
95 let _ =
96   try
97     Printexc.record_backtrace true;
98
99     let document =
100       if Filename.check_suffix !Options.input_file ".g.bin" ||
101         Filename.check_suffix !Options.input_file ".g"
102       then
103         let is_index = Filename.check_suffix !Options.input_file ".g.bin" in
104         let g =
105           if is_index then
106             time ~msg:"Loading grammar" (Grammar2.load) !Options.input_file
107           else
108             let g = time ~msg:"Parsing grammar file" Grammar2.parse !Options.input_file in
109             if !Options.save_file <> "" then
110               time ~msg:"Saving index" (Grammar2.save g) !Options.save_file;
111             g
112         in
113         begin
114       (* Todo Factorise with main *)
115           Tag.init (Grammar2.tag_operations g);
116           let query =
117             time ~msg:"Parsing query" XPath.parse !Options.query
118           in
119           if !Options.verbose then begin
120             Printf.eprintf "Parsed query:\n%!";
121             XPath.Ast.print Format.err_formatter query;
122             Format.fprintf Format.err_formatter "\n%!"
123           end;
124           let auto, bu_info =
125             time ~msg:"Compiling query" (Compile.compile) query
126           in
127           if !Options.verbose then Ata.print Format.err_formatter auto;
128           Gc.full_major();
129           Gc.compact();
130           Gc.set (tuned_gc);
131           let runtime =
132             if !Options.count_only then
133             let module R = ResJIT.Make(NodeSet.Partial(NodeSet.Count)) in
134             let module M = Runtime.Make(R) in
135         (* mk_runtime run auto doc arg count print outfile  *)
136             mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
137             else
138               let module R = ResJIT.Mat in
139               let module M = Runtime.Make(R) in
140             (* mk_runtime run auto doc arg count print outfile  *)
141               mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
142           in
143           runtime ();
144           exit 0
145         end
146       else if Filename.check_suffix !Options.input_file ".srx"
147       then
148         time
149           ~msg:"Loading file"
150           (Tree.load
151              ~sample:!Options.sample_factor
152              ~load_text:true)
153           !Options.input_file
154       else
155         let v =
156           time
157             ~msg:"Parsing document"
158             (Tree.parse_xml_uri)
159             !Options.input_file
160         in
161         let () =
162           if !Options.save_file <> ""
163           then
164             time
165               ~msg:"Writing file to disk"
166               (Tree.save v)
167               !Options.save_file;
168         in
169         v
170     in
171     main document !Options.query !Options.output_file;
172     if !Options.verbose then Printf.eprintf "Maximum resident set size: %s\n" (read_procmem());
173     Gc.full_major();
174     Profile.summary Format.err_formatter
175   with
176   | Ulexer.Loc.Exc_located ((x,y),e) ->
177     Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);
178     exit 1
179
180   | e ->
181     output_string stderr "\n";
182     flush stderr;
183     Printexc.print_backtrace stderr;
184     Printf.eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e);
185     output_string stderr "\n";
186     flush stderr;
187     exit 2
188
189