c9885e61ffe425757be3c1d26ac7a21c0abd6507
[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
8 open Ata
9
10 let l = ref [] ;;
11 let time f x =
12   let t1 = Unix.gettimeofday () in
13   let r = f x in
14   let t2 = Unix.gettimeofday () in 
15   let t = (1000. *. (t2 -. t1)) in
16     l:= t::!l;
17     Printf.eprintf "  %fms\n%!" t ;
18     r
19 ;;
20 let total_time () =  List.fold_left (+.) 0. !l;;
21 let enabled_gc = Gc.get()
22 let disabled_gc = { Gc.get() with
23                       Gc.max_overhead = 1000000; 
24                       Gc.space_overhead = 100 }
25
26 let main v query_string output =
27     let _ = Tag.init (Tree.tag_pool v) in
28       Printf.eprintf "Parsing query : ";    
29       let query = try
30         time
31           XPath.Parser.parse_string query_string
32       with
33           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
34       in
35         XPath.Ast.print Format.err_formatter query;
36         Format.fprintf Format.err_formatter "\n%!";
37         Printf.eprintf "Compiling query : ";
38         let auto,ltags,contains = time (XPath.Compile.compile ~querystring:query_string) query in 
39         let _ = Ata.dump Format.err_formatter auto in
40         let _ = Printf.eprintf "%!" in
41         let jump_to = 
42           match contains with
43               None -> (max_int,`NOTHING)
44             | Some s -> 
45                 let r = Tree.count v s 
46                 in
47                   Printf.eprintf "%i documents in the TextCollection\n" (Tree.text_size v);
48                   Printf.eprintf "Global count is %i, using " r;
49                   if r < !Options.tc_threshold then begin                 
50                     Printf.eprintf "TextCollection contains\nCalling global contains : ";
51                     time (Tree.init_contains v) s;
52                   end
53                   else begin
54                     Printf.eprintf "Naive contains\nCalling global contains : ";
55                     time (Tree.init_naive_contains v) s
56                   end;(r,`CONTAINS(s))
57         in
58         let test_list = jump_to in
59         (*
60         let test_list = 
61           if (!Options.backward) then begin
62             Printf.eprintf "Finding min occurences : ";
63             time 
64               ( List.fold_left (fun ((min_occ,kind)as acc)  (tag,_) ->
65                               let numtags = Tree.subtree_tags v tag Tree.root in
66                                 if  ((numtags < min_occ) && numtags >= 2)
67                                 then (numtags,`TAG(tag))
68                                 else acc) jump_to) ltags
69           end
70           else (max_int,`NOTHING)
71         in*)
72         let _ = if (snd test_list) != `NOTHING then
73           let occ,s1,s2 = match test_list with
74             | (x,`TAG (tag)) -> (x, "tag", (Tag.to_string tag))
75             | (x,`CONTAINS(s)) -> (x, "contains()", s)
76             | _ -> assert false
77           in
78             Printf.eprintf "Will jump to %s %s occuring %i time\n%!" s1 s2 occ
79         in
80           Printf.eprintf "Execution time %s : "
81             (if !Options.count_only then "(counting only)" else if !Options.backward then "(bottomup)" else "");
82           begin
83             let _ = Gc.full_major();Gc.compact() in
84             let _ = Printf.eprintf "%!" in
85               (*            let _ = Gc.set (disabled_gc) in  *)
86               if !Options.backward && ((snd test_list) != `NOTHING )then 
87                 
88                 let r = time (bottom_up_count auto v )(snd test_list)  in
89                 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r 
90                 in ()
91               else
92                 let _ = 
93                   if !Options.backward then Printf.eprintf "WARNING: couldn't find a jumping point, running top-down\n" 
94                 in
95                 if !Options.count_only then
96                   let r = time ( top_down_count auto ) v in 
97                   let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
98                   in ()
99                 else      
100                   let result = time (top_down auto) v in          
101                   let rcount = GResult.length result in
102                     Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
103                     Printf.eprintf "\n%!";
104                     begin
105                       match output with
106                         | None -> ()
107                         | Some f ->                   
108                             Printf.eprintf "Serializing results : ";
109                             time( fun () ->
110                                     let oc = open_out f in
111                                       output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";                          
112                                       GResult.iter (fun t -> 
113                                                     Tree.print_xml_fast oc v t;
114                                                     output_char oc '\n';
115
116                                                  ) result) ();
117                     end;
118           end;
119           let _ = Gc.set enabled_gc in
120             Printf.eprintf "Total running time : %fms\n%!" (total_time())
121 ;;
122
123 Options.parse_cmdline();;
124
125 let v = 
126   if (Filename.check_suffix !Options.input_file ".srx")
127   then 
128     begin
129       Printf.eprintf "Loading from file : ";
130       time (Tree.load  ~sample:!Options.sample_factor )
131         !Options.input_file;
132         end
133   else 
134     let v = 
135       time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
136             in Printf.eprintf "Parsing document : %!";v
137            ) () 
138     in
139       if !Options.save_file <> ""
140       then begin
141         Printf.eprintf "Writing file to disk : ";
142         time (Tree.save v) !Options.save_file;
143       end;
144       v
145 in
146   main v !Options.query !Options.output_file;;
147