merge from branch stable-succint-jumping
[SXSI/xpathcomp.git] / 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 "debug.ml"
8
9 open Ata
10
11 let l = ref [] ;;
12 let time f x =
13   let t1 = Unix.gettimeofday () in
14   let r = f x in
15   let t2 = Unix.gettimeofday () in 
16   let t = (1000. *.(t2 -. t1)) in
17     l:= t::!l;
18     Printf.eprintf "  %fms\n%!" t ;
19     r
20 ;;
21 let total_time () =  List.fold_left (+.) 0. !l;;
22
23
24 let main v query output =
25     let _ = Tag.init (Tree.Binary.tag_pool v) in
26       Printf.eprintf "Parsing query : ";    
27       let query = try
28         time
29           XPath.Parser.parse_string query
30       with
31           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
32       in
33         XPath.Ast.print Format.err_formatter query;
34         Format.fprintf Format.err_formatter "\n%!";
35 (*      Printf.eprintf "Dummy iteration : ";
36         time (fill_hashtag) v;
37         Printf.eprintf "Dummy iteration (tag access cached) : ";
38         time (fill_hashtag) v;
39 *)
40         Printf.eprintf "Compiling query : ";
41         let auto,ltags,contains = time XPath.Compile.compile  query in 
42         let _ = Ata.dump Format.err_formatter auto in
43         let _ = Printf.eprintf "%!" in
44         let _ = match contains with
45             None -> ()
46           | Some s -> Tree.Binary.init_contains v s
47         in
48           Printf.eprintf "Execution time %s : " (if !Options.count_only then "(counting only)" else "");
49           begin
50             if !Options.count_only then
51               failwith "Count only not implemented in this version"
52             else      
53               let _ = Gc.set ({ Gc.get() with Gc.max_overhead = 1000000; Gc.space_overhead = 100 }) in
54               let result = time (if !Options.time then run_time auto else run auto) v in          
55                 Printf.eprintf "Number of nodes in the result set : %i\n" (TS.length result);
56                 Printf.eprintf "\n%!";
57               begin
58                 match output with
59                   | None -> ()
60                   | Some f ->                 
61                       Printf.eprintf "Serializing results : ";
62                       time( fun () ->
63                               let oc = open_out f in
64                                 output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";                                
65                                 TS.rev_iter (fun t -> output_string oc "----------\n";
66                                            Tree.Binary.print_xml_fast oc t;
67                                            output_char oc '\n') result) ();
68               end;
69           end;
70           Printf.eprintf "Total running time : %fms\n%!" (total_time())
71 ;;
72                 
73 Options.parse_cmdline();;
74
75 let v = 
76   if (Filename.check_suffix !Options.input_file ".srx")
77   then 
78     begin
79       Printf.eprintf "Loading from file : ";
80       time (Tree.Binary.load  ~sample:!Options.sample_factor )
81         (Filename.chop_suffix !Options.input_file ".srx");
82     end
83   else 
84     let v = 
85       time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
86             in Printf.eprintf "Parsing document : %!";v
87            ) () 
88     in
89       if !Options.save_file <> ""
90       then begin
91         Printf.eprintf "Writing file to disk : ";
92         time (Tree.Binary.save v) !Options.save_file;
93       end;
94       v
95 in
96   main v !Options.query !Options.output_file;;
97
98 IFDEF DEBUG
99 THEN
100 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
101
102 Tree.DEBUGTREE.print_stats Format.err_formatter;;
103 Gc.full_major()
104 ENDIF