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 (******************************************************************************)
11 let () = init_timer();;
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;
22 let mk_runtime run auto doc arg count print outfile =
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 Logger.print Format.err_formatter "Number of results: %i@\n" (count r);
31 time ~count:1 ~msg:"Serialization time" (print file doc) r
35 let main v query_string output =
36 Tag.init (Tree.tag_operations v);
38 time ~msg:"Parsing query" XPath.parse query_string
40 if !Options.verbose then begin
41 Logger.print Format.err_formatter "Parsed query:@\n%a@\n"
42 XPath.Ast.print query;
45 time ~msg:"Compiling query" Compile.compile query
47 if !Options.verbose then Ata.print Format.err_formatter auto;
52 match !Options.bottom_up, bu_info with
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 None
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
65 (* run the query top_down *)
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
75 mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize None
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
84 let () = Options.parse_cmdline()
88 Printexc.record_backtrace true;
91 if Filename.check_suffix !Options.input_file ".g.bin" ||
92 Filename.check_suffix !Options.input_file ".g"
94 let is_index = Filename.check_suffix !Options.input_file ".g.bin" in
97 time ~msg:"Loading grammar" (Grammar2.load) !Options.input_file
99 let g = time ~msg:"Parsing grammar file" Grammar2.parse !Options.input_file in
100 if !Options.save_file <> "" then
101 time ~msg:"Saving index" (Grammar2.save g) !Options.save_file;
105 (* TODO Factorise with main *)
106 Tag.init (Grammar2.tag_operations g);
108 time ~msg:"Parsing query" XPath.parse !Options.query
110 if !Options.verbose then begin
111 Printf.eprintf "Parsed query:\n%!";
112 XPath.Ast.print Format.err_formatter query;
113 Format.fprintf Format.err_formatter "\n%!"
116 time ~msg:"Compiling query" (Compile.compile) query
118 if !Options.verbose then Ata.print Format.err_formatter auto;
123 if !Options.count_only then
124 let module R = ResJIT.Make(NodeSet.Partial(NodeSet.Count)) in
125 let module M = Runtime.Make(R) in
126 (* mk_runtime run auto doc arg count print outfile *)
127 mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
129 let module R = ResJIT.Mat in
130 let module M = Runtime.Make(R) in
131 (* mk_runtime run auto doc arg count print outfile *)
132 mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
137 else if Filename.check_suffix !Options.input_file ".srx"
142 ~sample:!Options.sample_factor
148 ~msg:"Parsing document"
153 if !Options.save_file <> ""
156 ~msg:"Writing file to disk"
162 main document !Options.query !Options.output_file;
163 if !Options.verbose then
164 Logger.print Format.err_formatter "Maximum resident set size: %s @\n" (read_procmem());
166 Profile.summary Format.err_formatter
168 | Ulexer.Loc.Exc_located ((x,y),e) ->
169 Logger.print Format.err_formatter "character %i-%i %s@\n" x y (Printexc.to_string e);
173 Logger.print Format.err_formatter "BACKTRACE: %s@\n@?" (Printexc.get_backtrace());
174 Logger.print Format.err_formatter "FATAL ERROR: %s@\n@?" (Printexc.to_string e);