bottom up run works for text nodes
[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 let enabled_gc = Gc.get()
23 let disabled_gc = { Gc.get() with
24                       Gc.max_overhead = 1000000; 
25                       Gc.space_overhead = 100 }
26
27 let main v query output =
28     let _ = Tag.init (Tree.tag_pool v) in
29       Printf.eprintf "Parsing query : ";    
30       let query = try
31         time
32           XPath.Parser.parse_string query
33       with
34           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
35       in
36         XPath.Ast.print Format.err_formatter query;
37         Format.fprintf Format.err_formatter "\n%!";
38         Printf.eprintf "Compiling query : ";
39         let auto,ltags,contains = time XPath.Compile.compile  query in 
40         let _ = Ata.dump Format.err_formatter auto in
41         let _ = Printf.eprintf "%!" in
42           
43         let do_contains = match contains with
44             None -> false
45           | Some s -> 
46               let r = Tree.count v s 
47               in
48                 Printf.eprintf "%i documents in the TextCollection\n" (Tree.text_size v);
49                 Printf.eprintf "Global count is %i, using " r;
50                 if r < !Options.tc_threshold then begin           
51                   Printf.eprintf "TextCollection contains\nTiming call to raw global contains (1st time): ";
52                   time (Tree.unsorted_contains v) s;              
53                   Printf.eprintf "Calling global contains : ";
54                   time (Tree.init_contains v) s;
55                   Printf.eprintf "Timing call to global count contains : ";
56                   let r = time (Tree.count_contains v) s
57                   in
58                     Printf.eprintf " number of matching nodes %i \n%!" r;
59                     Printf.eprintf "Timing call to raw global contains (2nd time): ";
60                     time (Tree.unsorted_contains v) s;
61                 end
62                 else begin
63                   Printf.eprintf "Naive contains\nCalling global contains : ";
64                   time (Tree.init_naive_contains v) s
65                 end;true
66         in
67           Printf.eprintf "Execution time %s : "
68             (if !Options.count_only then "(counting only)" else if !Options.backward then "(bottomup)" else "");
69           begin
70             let _ = Gc.full_major();Gc.compact() in
71             let _ = Gc.set (disabled_gc) in 
72               if !Options.backward then
73                 let tag,set = List.hd ltags in
74                 let r = if do_contains 
75                 then time (bottom_up_count_contains auto) v
76                 else time (bottom_up_count auto v) tag in
77                 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r 
78                 in ()
79               else
80                 if !Options.count_only then
81                   let r = time ( top_down_count auto ) v in (* not clean *)
82               let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
83               in ()
84             else      
85               let result = time (top_down auto) v in      
86               let rcount = IdSet.length result in
87                 Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
88                 Printf.eprintf "\n%!";
89               begin
90                 match output with
91                   | None -> ()
92                   | Some f ->                 
93                       Printf.eprintf "Serializing results : ";
94                       time( fun () ->
95                               let oc = open_out f in
96                                 output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";                                
97                                 IdSet.iter (fun t -> 
98                                               Tree.print_xml_fast oc t;
99                                               output_char oc '\n';
100                                               output_string oc "----------\n";
101                                            ) result) ();
102               end;
103           end;
104           let _ = Gc.set enabled_gc in
105 (*        let _ = Ata.dump Format.err_formatter auto in *)
106           Printf.eprintf "Total running time : %fms\n%!" (total_time())
107 ;;
108                 
109 Options.parse_cmdline();;
110
111 let v = 
112   if (Filename.check_suffix !Options.input_file ".srx")
113   then 
114     begin
115       Printf.eprintf "Loading from file : ";
116       time (Tree.load  ~sample:!Options.sample_factor )
117     (Filename.chop_suffix !Options.input_file ".srx");
118         end
119   else 
120     let v = 
121       time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
122             in Printf.eprintf "Parsing document : %!";v
123            ) () 
124     in
125       if !Options.save_file <> ""
126       then begin
127         Printf.eprintf "Writing file to disk : ";
128         time (Tree.save v) !Options.save_file;
129       end;
130       v
131 in
132   main v !Options.query !Options.output_file;;
133
134 IFDEF DEBUG
135 THEN
136 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
137
138 Tree.DEBUGTREE.print_stats Format.err_formatter;;
139 Gc.full_major()
140 ENDIF