Also serialize results in counting mode (prints the count in the output file)
[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   if !Options.docstats then Tree.stats v;
37   let query =
38     time ~msg:"Parsing query" XPath.parse query_string
39   in
40   if !Options.verbose then begin
41     Logger.print Format.err_formatter "Parsed query:@\n%a@\n"
42       XPath.Ast.print query;
43   end;
44   let auto, bu_info =
45     time ~msg:"Compiling query" Compile.compile query
46   in
47   if !Options.verbose then Ata.print Format.err_formatter auto;
48   Gc.full_major();
49   Gc.compact();
50   Gc.set (tuned_gc);
51   let runtime =
52     match !Options.bottom_up, bu_info with
53
54     | true, Some [ (query, pattern) ] ->
55       if !Options.count_only then
56         let module R = ResJIT.Count in
57         let module M = Runtime.Make(R) in
58         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Options.output_file
59       else
60         let module R = ResJIT.Mat in
61         let module M = Runtime.Make(R) in
62         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Options.output_file
63
64     | _ ->
65       (* run the query top_down *)
66
67       if !Options.bottom_up then
68         Logger.print Format.err_formatter "Cannot run the query in bottom-up mode, using top-down evaluator@\n@?";
69       if !Options.count_only then
70         let module R = ResJIT.Count in
71         let module M = Runtime.Make(R) in
72         if !Options.twopass then
73           mk_runtime M.twopass_top_down_run auto v Tree.root R.NS.length R.NS.serialize None
74         else
75           mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
76       else
77         let module R = ResJIT.Mat in
78         let module M = Runtime.Make(R) in
79         mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
80   in
81   runtime ()
82 ;;
83
84 let () = Options.parse_cmdline()
85 ;;
86 let _ =
87   try
88     Printexc.record_backtrace true;
89     let document =
90       if Filename.check_suffix !Options.input_file ".srx"
91       then
92         time
93           ~msg:"Loading file"
94           (Tree.load
95              ~sample:!Options.sample_factor
96              ~load_text:(not !Options.disable_text_collection))
97           !Options.input_file
98       else
99         let v =
100           time
101             ~msg:"Parsing document"
102             (Tree.parse_xml_uri)
103             !Options.input_file
104         in
105         let () =
106           if !Options.save_file <> ""
107           then
108             time
109               ~msg:"Writing file to disk"
110               (Tree.save v)
111               !Options.save_file;
112         in
113         v
114     in
115     main document !Options.query !Options.output_file;
116     if !Options.verbose then
117       Logger.print Format.err_formatter "Maximum resident set size: %s @\n" (read_procmem());
118     Gc.full_major();
119     Profile.summary Format.err_formatter
120   with
121   | Ulexer.Loc.Exc_located ((x,y),e) ->
122     Logger.print Format.err_formatter "character %i-%i %s@\n" x y (Printexc.to_string e);
123     exit 1
124
125   | e ->
126     Logger.print Format.err_formatter "BACKTRACE: %s@\n@?" (Printexc.get_backtrace());
127     Logger.print Format.err_formatter "FATAL ERROR: %s@\n@?" (Printexc.to_string e);
128     exit 2