aed4ebbc42585403c873ddd5a5a43a1c61fbedcd
[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.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 -> 
47               let r = Tree.count v s 
48               in
49                 Printf.eprintf "Global count is %i, using " r;
50                 if r < 60000 then begin
51                   Printf.eprintf "TextCollection contains\nCalling global contains : ";
52                   time (Tree.init_contains v) s
53                 end
54                 else begin
55                   Printf.eprintf "Naive contains\nCalling global contains : ";
56                   time (Tree.init_naive_contains v) s
57                 end
58         in
59           Printf.eprintf "Execution time %s : " (if !Options.count_only then "(counting only)" else "");
60           begin
61             if !Options.count_only then
62               let r = time ( run_count auto  )v in
63               let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
64               in ()
65             else      
66 (*            let _ = Gc.set ({ Gc.get() with Gc.max_overhead = 1000000; Gc.space_overhead = 100 }) in  *)
67               let result,rcount = time (if !Options.time then run_time auto else run auto) v in   
68                 Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
69                 Printf.eprintf "\n%!";
70               begin
71                 match output with
72                   | None -> ()
73                   | Some f ->                 
74                       Printf.eprintf "Serializing results : ";
75                       time( fun () ->
76                               let oc = open_out f in
77                                 output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";                                
78                                 TS.iter (fun t -> 
79                                            Tree.print_xml_fast oc t;
80                                            output_char oc '\n') result) ();
81               end;
82           end;
83           let _ = Ata.dump Format.err_formatter auto in
84           Printf.eprintf "Total running time : %fms\n%!" (total_time())
85 ;;
86                 
87 Options.parse_cmdline();;
88
89 let v = 
90   if (Filename.check_suffix !Options.input_file ".srx")
91   then 
92     begin
93       Printf.eprintf "Loading from file : ";
94       time (Tree.load  ~sample:!Options.sample_factor )
95         (Filename.chop_suffix !Options.input_file ".srx");
96     end
97   else 
98     let v = 
99       time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
100             in Printf.eprintf "Parsing document : %!";v
101            ) () 
102     in
103       if !Options.save_file <> ""
104       then begin
105         Printf.eprintf "Writing file to disk : ";
106         time (Tree.save v) !Options.save_file;
107       end;
108       v
109 in
110   main v !Options.query !Options.output_file;;
111
112 IFDEF DEBUG
113 THEN
114 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
115
116 Tree.DEBUGTREE.print_stats Format.err_formatter;;
117 Gc.full_major()
118 ENDIF