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