48f2e468e8223f1b113eb8b5c776e24e1ee9df55
[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:!Options.repeat ~msg:"Execution time" (run auto doc) arg in
26     if !Options.do_perf then stop_perf ();
27     Logger.print Format.err_formatter "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 let main v query_string output =
35   Tag.init (Tree.tag_operations v);
36   let query =
37     time ~msg:"Parsing query" XPath.parse query_string
38   in
39   if !Options.verbose then begin
40     Logger.print Format.err_formatter "Parsed query:@\n%a@\n"
41       XPath.Ast.print query;
42   end;
43   let auto, bu_info =
44     time ~msg:"Compiling query" Compile.compile query
45   in
46   if !Options.verbose then Ata.print Format.err_formatter auto;
47   Gc.full_major();
48   Gc.compact();
49   Gc.set (tuned_gc);
50   let runtime =
51     match !Options.bottom_up, bu_info with
52
53     | true, Some [ (query, pattern) ] ->
54       if !Options.count_only then
55         let module R = ResJIT.Count in
56         let module M = Runtime.Make(R) in
57         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize None
58       else
59         let module R = ResJIT.Mat in
60         let module M = Runtime.Make(R) in
61         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Options.output_file
62
63     | _ ->
64       (* run the query top_down *)
65
66       if !Options.bottom_up then
67         Logger.print Format.err_formatter "Cannot run the query in bottom-up mode, using top-down evaluator@\n@?";
68       if !Options.count_only then
69         let module R = ResJIT.Count in
70         let module M = Runtime.Make(R) in
71         if !Options.twopass then
72           mk_runtime M.twopass_top_down_run auto v Tree.root R.NS.length R.NS.serialize None
73         else
74           mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize None
75       else
76         let module R = ResJIT.Mat in
77         let module M = Runtime.Make(R) in
78         mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
79   in
80   runtime ()
81 ;;
82
83 let () = Options.parse_cmdline()
84 ;;
85 let _ =
86   try
87     Printexc.record_backtrace true;
88
89     let document =
90       if Filename.check_suffix !Options.input_file ".g.bin" ||
91         Filename.check_suffix !Options.input_file ".g"
92       then
93         let is_index = Filename.check_suffix !Options.input_file ".g.bin" in
94         let g =
95           if is_index then
96             time ~msg:"Loading grammar" (Grammar2.load) !Options.input_file
97           else
98             let g = time ~msg:"Parsing grammar file" Grammar2.parse !Options.input_file in
99             if !Options.save_file <> "" then
100               time ~msg:"Saving index" (Grammar2.save g) !Options.save_file;
101             g
102         in
103         begin
104           (* TODO Factorise with main *)
105           Tag.init (Grammar2.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             if !Options.count_only then
123               let module R = ResJIT.Make(NodeSet.Partial(NodeSet.Count)) in
124               let module M = Runtime.Make(R) in
125             (* mk_runtime run auto doc arg count print outfile  *)
126               mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
127             else
128               let module R = ResJIT.Mat in
129               let module M = Runtime.Make(R) in
130               (* mk_runtime run auto doc arg count print outfile  *)
131               mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
132           in
133           runtime ();
134           exit 0
135         end
136       else if Filename.check_suffix !Options.input_file ".srx"
137       then
138         time
139           ~msg:"Loading file"
140           (Tree.load
141              ~sample:!Options.sample_factor
142              ~load_text:(not !Options.disable_text_collection))
143           !Options.input_file
144       else
145         let v =
146           time
147             ~msg:"Parsing document"
148             (Tree.parse_xml_uri)
149             !Options.input_file
150         in
151         let () =
152           if !Options.save_file <> ""
153           then
154             time
155               ~msg:"Writing file to disk"
156               (Tree.save v)
157               !Options.save_file;
158         in
159         v
160     in
161     main document !Options.query !Options.output_file;
162     if !Options.verbose then
163       Logger.print Format.err_formatter "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     Logger.print Format.err_formatter "character %i-%i %s@\n" x y (Printexc.to_string e);
169     exit 1
170
171   | e ->
172     Logger.print Format.err_formatter "BACKTRACE: %s@\n@?" (Printexc.get_backtrace());
173     Logger.print Format.err_formatter "FATAL ERROR: %s@\n@?" (Printexc.to_string e);
174     exit 2
175
176