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