.
[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
12 let l = ref [] ;;
13 let time f x =
14   let t1 = Unix.gettimeofday () in
15   let r = f x in
16   let t2 = Unix.gettimeofday () in 
17   let t = (1000. *.(t2 -. t1)) in
18     l:= t::!l;
19     Printf.eprintf "  %fms\n%!" t ;
20     r
21 ;;
22 let total_time () =  List.fold_left (+.) 0. !l;;
23
24
25 let main v query output =
26     let _ = Tag.init (Tree.Binary.tag_pool v) in
27       Printf.eprintf "Parsing query : ";    
28       let query = try
29         time
30           XPath.Parser.parse_string query
31       with
32           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
33       in      
34         Printf.eprintf "Compiling query : ";
35         let auto = time XPath.Compile.compile  query in
36           XPath.Ast.print Format.err_formatter query;
37           Printf.eprintf "Execution time : ";
38           let result = time (BottomUpNew.run auto) v in
39             Printf.eprintf "Number of nodes in the result set : %i\n" (List.length result);
40             begin
41               match output with
42                 | None -> ()
43                 | Some f ->
44                     
45                     Printf.eprintf "Serializing results : ";
46                     time( fun () ->
47                             let oc = open_out f in
48                               output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
49                               List.iter (fun t -> Tree.Binary.print_xml_fast oc t;
50                                            output_char oc '\n') result) ();
51             end;
52             Printf.eprintf "Total time : %fms\n Coherence : %i\n%!" (total_time())
53 ;;
54                 
55
56 Options.parse_cmdline();;
57
58 let v = 
59   if (Filename.check_suffix !Options.input_file ".srx")
60   then 
61     begin
62       Printf.eprintf "Loading from file : ";
63       time (Tree.Binary.load  ~sample:!Options.sample_factor )
64         (Filename.chop_suffix !Options.input_file ".srx");
65     end
66   else 
67     let v = 
68       time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
69             in Printf.eprintf "Parsing document : %!";v
70            ) () 
71     in
72       if !Options.save_file <> ""
73       then begin
74         Printf.eprintf "Writing file to disk : ";
75         time (Tree.Binary.save v) !Options.save_file;
76       end;
77       v
78 in
79   main v !Options.query !Options.output_file;;
80
81 IFDEF DEBUG
82 THEN
83 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
84 Format.eprintf "\nAutomaton is:\n%!";
85 Ata.dump Format.err_formatter auto;
86 Tree.DEBUGTREE.print_stats Format.err_formatter;;
87 Gc.full_major()
88 ENDIF