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