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