Merge branch 'handle-stdout'
[SXSI/xpathcomp.git] / src / main.ml
index 100d0e4..69bd091 100644 (file)
@@ -17,66 +17,71 @@ let tuned_gc = { default_gc with
   Gc.major_heap_increment = 8*1024*1024;
   Gc.max_overhead = 1000000;
   Gc.space_overhead = 100;
-              }
+}
 
 let mk_runtime run auto doc arg count print outfile =
   fun () ->
-    if !Options.do_perf then start_perf ();
-    let r = time ~count:1 ~msg:"Execution time" (run auto doc) arg in
-    if !Options.do_perf then stop_perf ();
-    Logger.print Format.err_formatter "Number of results: %i@\n" (count r);
+    if !Config.do_perf then start_perf ();
+    let r = Utils.time ~count:!Config.repeat ~msg:"Execution time" (run auto doc) arg in
+    if !Config.do_perf then stop_perf ();
+    Logger.start_msg Format.err_formatter "[Debug] Number of results: ";
+    Logger.msg Format.err_formatter "%i" (count r);
+    Logger.end_msg Format.err_formatter "\n";
     match outfile with
        None -> ()
       | Some file ->
-       time ~count:1 ~msg:"Serialization time" (print file doc) r
+       Utils.time ~count:1 ~msg:"Serialization time" (print file !Config.no_wrap_results doc) r
 ;;
 
-
 let main v query_string output =
   Tag.init (Tree.tag_operations v);
+  if !Config.docstats then Tree.stats v;
   let query =
-    time ~msg:"Parsing query" XPath.parse query_string
+    Utils.time ~msg:"Parsing query" XPath.parse query_string
   in
-  if !Options.verbose then begin
-    Logger.print Format.err_formatter "Parsed query:@\n%a@\n"
-    XPath.Ast.print query;
-  end;
+  Logger.start_msg Format.err_formatter "[Debug]";
+  Logger.msg Format.err_formatter " Parsed query: @\n @[<v 0> {";
+  Logger.msg Format.err_formatter " %a }@]" XPath.Ast.print query;
+  Logger.end_msg Format.err_formatter "\n\n";
   let auto, bu_info =
-    time ~msg:"Compiling query" Compile.compile query
+    Utils.time ~msg:"Compiling query" Compile.compile query
   in
-  if !Options.verbose then Ata.print Format.err_formatter auto;
+  Logger.start_msg Format.err_formatter "[Debug] Automaton: ";
+  Logger.msg Format.err_formatter "@\n     @[<v 0>";
+  Logger.msg Format.err_formatter "%a" Ata.print auto;
+  Logger.end_msg Format.err_formatter "\n\n";
   Gc.full_major();
   Gc.compact();
   Gc.set (tuned_gc);
   let runtime =
-  match !Options.bottom_up, bu_info with
+    match !Config.bottom_up, bu_info with
 
     | true, Some [ (query, pattern) ] ->
-      if !Options.count_only then
-       let module R = ResJIT.Count in
-       let module M = Runtime.Make(R) in
-       mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize None
+      if !Config.count_only then
+        let module R = ResJIT.Count in
+        let module M = Runtime.Make(R) in
+        mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Config.output_file
       else
-       let module R = ResJIT.Mat in
-       let module M = Runtime.Make(R) in
-       mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Options.output_file
+        let module R = ResJIT.Mat in
+        let module M = Runtime.Make(R) in
+        mk_runtime M.bottom_up_run auto v (query, pattern) R.NS.length R.NS.serialize !Config.output_file
 
     | _ ->
-       (* run the query top_down *)
+      (* run the query top_down *)
 
-      if !Options.bottom_up then
-       Logger.print Format.err_formatter "Cannot run the query in bottom-up mode, using top-down evaluator@\n@?";
-      if !Options.count_only then
-       let module R = ResJIT.Count in
-       let module M = Runtime.Make(R) in
-       if !Options.twopass then
-         mk_runtime M.twopass_top_down_run auto v Tree.root R.NS.length R.NS.serialize None
-       else
-         mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize None
+      if !Config.bottom_up then
+        Logger.verbose Format.err_formatter "Cannot run the query in bottom-up mode, using top-down evaluator@\n@?";
+      if !Config.count_only then
+        let module R = ResJIT.Count in
+        let module M = Runtime.Make(R) in
+        if !Config.twopass then
+          mk_runtime M.twopass_top_down_run auto v Tree.root R.NS.length R.NS.serialize None
+        else
+          mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Config.output_file
       else
-       let module R = ResJIT.Mat in
-       let module M = Runtime.Make(R) in
-       mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Options.output_file
+        let module R = ResJIT.Mat in
+        let module M = Runtime.Make(R) in
+        mk_runtime M.top_down_run auto v Tree.root R.NS.length R.NS.serialize !Config.output_file
   in
   runtime ()
 ;;
@@ -86,84 +91,38 @@ let () = Options.parse_cmdline()
 let _ =
   try
     Printexc.record_backtrace true;
-
     let document =
-      if Filename.check_suffix !Options.input_file ".g.bin" ||
-       Filename.check_suffix !Options.input_file ".g"
-      then
-       let is_index = Filename.check_suffix !Options.input_file ".g.bin" in
-       let g =
-         if is_index then
-           time ~msg:"Loading grammar" (Grammar2.load) !Options.input_file
-         else
-           let g = time ~msg:"Parsing grammar file" Grammar2.parse !Options.input_file in
-           if !Options.save_file <> "" then
-             time ~msg:"Saving index" (Grammar2.save g) !Options.save_file;
-           g
-       in
-       begin
-      (* TODO Factorise with main *)
-         Tag.init (Grammar2.tag_operations g);
-         let query =
-           time ~msg:"Parsing query" XPath.parse !Options.query
-         in
-         if !Options.verbose then begin
-           Printf.eprintf "Parsed query:\n%!";
-           XPath.Ast.print Format.err_formatter query;
-           Format.fprintf Format.err_formatter "\n%!"
-         end;
-         let auto, bu_info =
-           time ~msg:"Compiling query" (Compile.compile) query
-         in
-         if !Options.verbose then Ata.print Format.err_formatter auto;
-         Gc.full_major();
-         Gc.compact();
-         Gc.set (tuned_gc);
-         let runtime =
-           if !Options.count_only then
-           let module R = ResJIT.Make(NodeSet.Partial(NodeSet.Count)) in
-           let module M = Runtime.Make(R) in
-       (* mk_runtime run auto doc arg count print outfile  *)
-           mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
-           else
-             let module R = ResJIT.Mat in
-             let module M = Runtime.Make(R) in
-           (* mk_runtime run auto doc arg count print outfile  *)
-             mk_runtime M.grammar_run auto (Obj.magic g) () R.NS.length (Obj.magic R.NS.serialize) None
-         in
-         runtime ();
-         exit 0
-       end
-      else if Filename.check_suffix !Options.input_file ".srx"
+      if Filename.check_suffix !Config.input_file ".srx"
       then
-       time
-         ~msg:"Loading file"
+       Utils.time
+         ~msg:"Loading Index file"
          (Tree.load
-            ~sample:!Options.sample_factor
-            ~load_text:(not !Options.disable_text_collection))
-         !Options.input_file
+            ~sample:!Config.sample_factor
+            ~load_text:(not !Config.disable_text_collection))
+         !Config.input_file
       else
        let v =
-         time
-           ~msg:"Parsing document"
+         Utils.time
+           ~msg:"Loading XML file"
            (Tree.parse_xml_uri)
-           !Options.input_file
+           !Config.input_file
        in
-       let () =
-         if !Options.save_file <> ""
-         then
-           time
+        let () =
+          if !Config.save_file <> ""
+          then
+           Utils.time
              ~msg:"Writing file to disk"
              (Tree.save v)
-             !Options.save_file;
+             !Config.save_file;
        in
        v
     in
-    main document !Options.query !Options.output_file;
-    if !Options.verbose then
-      Logger.print Format.err_formatter "Maximum resident set size: %s @\n" (read_procmem());
-    Gc.full_major();
+    main document !Config.query !Config.output_file;
+IFDEF PROFILE
+  THEN
     Profile.summary Format.err_formatter
+  ELSE ()
+END
   with
   | Ulexer.Loc.Exc_located ((x,y),e) ->
     Logger.print Format.err_formatter "character %i-%i %s@\n" x y (Printexc.to_string e);
@@ -173,5 +132,3 @@ let _ =
     Logger.print Format.err_formatter "BACKTRACE: %s@\n@?" (Printexc.get_backtrace());
     Logger.print Format.err_formatter "FATAL ERROR: %s@\n@?" (Printexc.to_string e);
     exit 2
-
-