Defunctorize the ResJIT module.
[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         (* mk_runtime run auto doc arg count print outfile  *)
80         mk_runtime (Runtime.top_down_run {
81           Runtime.empty = R.NS.empty;
82           Runtime.subtree_tags = R.NS.subtree_tags;
83           Runtime.subtree_elements = R.NS.subtree_elements;
84           Runtime.exec = ResJIT.count_exec;
85         })
86           auto v Tree.root R.NS.length R.NS.serialize None
87       else
88         let module R = ResJIT.Mat in
89         mk_runtime
90           (Runtime.top_down_run {
91             Runtime.empty = R.NS.empty;
92             Runtime.subtree_tags = R.NS.subtree_tags;
93             Runtime.subtree_elements = R.NS.subtree_elements;
94             Runtime.exec = ResJIT.mat_exec;
95         })
96           auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
97   in
98   runtime ()
99 ;;
100
101 let () = Options.parse_cmdline()
102 ;;
103 let _ =
104   try
105     Printexc.record_backtrace true;
106
107     let document =
108       if Filename.check_suffix !Options.input_file ".g.bin" ||
109         Filename.check_suffix !Options.input_file ".g"
110       then
111         let is_index = Filename.check_suffix !Options.input_file ".g.bin" in
112         let g =
113           if is_index then
114             time ~msg:"Loading grammar" (Grammar2.load) !Options.input_file
115           else
116             let g = time ~msg:"Parsing grammar file" Grammar2.parse !Options.input_file in
117             if !Options.save_file <> "" then
118               time ~msg:"Saving index" (Grammar2.save g) !Options.save_file;
119             g
120         in
121         begin
122       (* Todo Factorise with main *)
123           Tag.init (Grammar2.tag_operations g);
124           let query =
125             time ~msg:"Parsing query" XPath.parse !Options.query
126           in
127           if !Options.verbose then begin
128             Printf.eprintf "Parsed query:\n%!";
129             XPath.Ast.print Format.err_formatter query;
130             Format.fprintf Format.err_formatter "\n%!"
131           end;
132           let auto, bu_info =
133             time ~msg:"Compiling query" (Compile.compile) query
134           in
135           if !Options.verbose then Ata.print Format.err_formatter auto;
136           Gc.full_major();
137           Gc.compact();
138           Gc.set (tuned_gc);
139           let runtime =
140             if !Options.count_only then
141             let module R = ResJIT.Make(NodeSet.Partial(NodeSet.Count)) in
142             let module M = Runtime.Make(R) in
143         (* mk_runtime run auto doc arg count print outfile  *)
144             mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
145             else
146               let module R = ResJIT.Mat in
147               let module M = Runtime.Make(R) in
148             (* mk_runtime run auto doc arg count print outfile  *)
149               mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
150           in
151           runtime ();
152           exit 0
153         end
154       else if Filename.check_suffix !Options.input_file ".srx"
155       then
156         time
157           ~msg:"Loading file"
158           (Tree.load
159              ~sample:!Options.sample_factor
160              ~load_text:true)
161           !Options.input_file
162       else
163         let v =
164           time
165             ~msg:"Parsing document"
166             (Tree.parse_xml_uri)
167             !Options.input_file
168         in
169         let () =
170           if !Options.save_file <> ""
171           then
172             time
173               ~msg:"Writing file to disk"
174               (Tree.save v)
175               !Options.save_file;
176         in
177         v
178     in
179     main document !Options.query !Options.output_file;
180     if !Options.verbose then Printf.eprintf "Maximum resident set size: %s\n" (read_procmem());
181     Gc.full_major();
182     Profile.summary Format.err_formatter
183   with
184   | Ulexer.Loc.Exc_located ((x,y),e) ->
185     Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);
186     exit 1
187
188   | e ->
189     output_string stderr "\n";
190     flush stderr;
191     Printexc.print_backtrace stderr;
192     Printf.eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e);
193     output_string stderr "\n";
194     flush stderr;
195     exit 2
196
197