Add serialization functions
[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 Automaton
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     (* 
27   (* Just a trick to allow the C++ code to print debugging stuff first *)  
28        let v = time (fun () -> let v = Tree.Binary.parse_xml_uri filename;
29        in Printf.eprintf "Parsing document : %!";v
30        ) () 
31        in
32     *)
33     let _ = Tag.init (Tree.Binary.tag_pool v) in
34       Printf.eprintf "Parsing query : ";    
35       let query = try
36         time
37           XPath.Parser.parse_string query
38       with
39           Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
40       in      
41         Printf.eprintf "Compiling query : ";
42         let auto = time XPath.Compile.compile  query in
43           XPath.Ast.print Format.err_formatter query;
44           Format.eprintf "\n%!";
45           (*            Format.eprintf "Internal rep of the tree is :\n%!";
46                         Tree.Binary.dump v;                           *)
47           Printf.eprintf "Execution time : ";
48           time (fun v -> ignore (TopDown.accept auto v)) v;
49           Printf.eprintf "Number of nodes in the result set : %i\n" (BST.cardinal auto.result);
50           begin
51             match output with
52               | None -> ()
53               | Some f ->
54                   
55                   Printf.eprintf "Serializing results : ";
56                 time( fun () ->
57                         let oc = open_out f in
58                           output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
59                           BST.iter (fun t -> Tree.Binary.print_xml_fast oc t;
60                                       output_char oc '\n') auto.result) ();
61         end;
62         Printf.eprintf "Total time : %fms\n Coherence : %i\n%!" (total_time())
63 ;;
64                 
65
66 Options.parse_cmdline();;
67
68 let v = 
69   if (Filename.check_suffix !Options.input_file ".srx")
70   then 
71     begin
72       Printf.eprintf "Loading from file : ";
73       time (Tree.Binary.load  ~sample:!Options.sample_factor )
74         (Filename.chop_suffix !Options.input_file ".srx");
75     end
76   else 
77     let v = 
78       time (fun () -> let v = Tree.Binary.parse_xml_uri !Options.input_file;
79             in Printf.eprintf "Parsing document : %!";v
80            ) () 
81     in
82       if !Options.save_file <> ""
83       then begin
84         Printf.eprintf "Writing file to disk : ";
85         time (Tree.Binary.save v) !Options.save_file;
86       end;
87       v
88 in
89   main v !Options.query !Options.output_file;;
90
91 IFDEF DEBUG
92 THEN
93 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
94 Tree.DEBUGTREE.print_stats Format.err_formatter;;
95 Gc.full_major()
96 ENDIF