Fix C++/OCaml wrappers.
[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
23 let mk_runtime run auto doc arg count print outfile =
24   fun () ->
25     let r = time ~count:1 ~msg:"Execution time" (run auto doc) arg in
26     Printf.eprintf "Number of results: %i\n%!" (count r);
27     match outfile with
28         None -> ()
29       | Some file ->
30         time ~count:1 ~msg:"Serialization time" (print file doc) r
31 ;;
32
33
34 let main v query_string output =
35   Tag.init (Tree.tag_pool v);
36   let query =
37     time ~msg:"Parsing query" XPath.parse query_string
38   in
39   if !Options.verbose then begin
40     Printf.eprintf "Parsed query:\n%!";
41     XPath.Ast.print Format.err_formatter query;
42     Format.fprintf Format.err_formatter "\n%!"
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       (* let nodes =
56         time
57           ~count:1 ~msg:"Computing full text query"
58           (Tree.full_text_query query v) pattern
59       in
60       let nodes = Array.to_list nodes in *)
61       if !Options.count_only then
62         let module R = ResJIT.Count in
63         let module M = Runtime.Make(R) in
64         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize None
65       else
66         let module R = ResJIT.Mat in
67         let module M = Runtime.Make(R) in
68         mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Options.output_file
69
70     | _ ->
71         (* run the query top_down *)
72
73       if !Options.bottom_up then
74         Printf.eprintf "Cannot run the query in bottom-up mode, using top-down evaluator\n%!";
75
76       if !Options.count_only then
77         let module R = ResJIT.Count in
78         let module M = Runtime.Make(R) in
79         (* mk_runtime run auto doc arg count print outfile  *)
80         mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize None
81       else
82         let module R = ResJIT.Mat in
83         let module M = Runtime.Make(R) in
84         mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
85   in
86   runtime ()
87 ;;
88
89 let () = Options.parse_cmdline()
90 ;;
91
92 let document =
93   if Filename.check_suffix !Options.input_file ".srx"
94   then
95     time
96       ~msg:"Loading file"
97       (Tree.load
98          ~sample:!Options.sample_factor
99          ~load_text:true)
100       !Options.input_file
101   else
102     let v =
103       time
104         ~msg:"Parsing document"
105         (Tree.parse_xml_uri)
106         !Options.input_file
107     in
108     let () =
109       if !Options.save_file <> ""
110       then
111         time
112           ~msg:"Writing file to disk"
113           (Tree.save v)
114           !Options.save_file;
115     in
116       v
117 in
118   try
119     (*Printexc.record_backtrace true; *)
120     main document !Options.query !Options.output_file;
121     if !Options.verbose then Printf.eprintf "Maximum resident set size: %s\n" (read_procmem());
122     Gc.full_major();
123     Profile.summary Format.err_formatter
124   with
125     | Ulexer.Loc.Exc_located ((x,y),e) ->
126         Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);
127         exit 1
128
129     | e ->
130         output_string stderr "\n";
131         flush stderr;
132         Printexc.print_backtrace stderr;
133         Printf.eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e);
134         output_string stderr "\n";
135         flush stderr;
136 (*      Ptset.Int.stats(); *)
137         exit 2
138
139