1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (* Recherche Scientifique. All rights reserved. This file is *)
10 (* distributed under the terms of the GNU Lesser General Public *)
11 (* License, with the special exception on linking described in file *)
14 (***********************************************************************)
19 let t1 = Unix.gettimeofday () in
21 let t2 = Unix.gettimeofday () in
22 let time = (t2 -. t1) *. 1000. in
23 Logger.msg `STATS "%s: %fms" msg time;
27 let compose_parallel run auto_list tree nodes () =
29 [ auto ] -> [run auto tree nodes]
32 let compose_sequential run auto_list tree nodes () =
33 [ List.fold_left (fun acc auto ->
34 run auto tree acc) nodes auto_list ]
37 let restart_parallel run auto_list tree nodes () =
39 [ auto ] -> List.map snd (run auto tree nodes)
42 let restart_sequential run auto_list tree nodes () =
43 List.map (fun auto -> run auto tree nodes) auto_list
46 let () = Options.parse () in
48 let fd, close_fd = match !Options.input_file with
49 None | Some "-" | Some "/dev/stdin" -> stdin, ignore
51 let fd = open_in input in fd, fun () -> close_in fd
53 let d = time Naive_tree.load_xml_file fd "parsing xml document" in
61 (Ulexing.from_utf8_string q)) l)
63 "parsing XPath queries"
65 (* parallel, compose -> action
66 true, true -> Ata.concat of all automata and single run
67 true, false -> Ata.merge of all automata and single run
68 false, true -> Eval first, then run on results then ...
69 false, false -> Eval first on root, then second on root then ...
74 List.map (fun query -> Xpath.Compile.path query) l)
76 "compiling XPath queries"
79 if !Options.parallel then
83 if !Options.compose then
88 let big_auto = List.fold_left f fst rest in
96 match !Options.output_file with
97 | None | Some "-" | Some "/dev/stdout" -> stdout
98 | Some f -> open_out f
100 if !Options.stats then begin
101 List.iter (fun query ->
102 Logger.msg `STATS "Query: %a " Xpath.Ast.print_path query) queries;
103 List.iter (fun auto ->
104 Logger.msg `STATS "@[Automaton: @\n%a@]" Ata.print auto) auto_list;
107 let module Naive = Run.Make(Naive_tree) in
109 let root = [ Naive_tree.root doc] in
111 match !Options.parallel, !Options.compose with
113 compose_parallel Naive.eval auto_list doc root, "parallel/compose"
115 restart_parallel Naive.full_eval auto_list doc root, "parallel/restart"
117 compose_sequential Naive.eval auto_list doc root , "sequential/compose"
119 restart_sequential Naive.eval auto_list doc root, "sequential/restart"
121 time f () ("evaluating query in " ^ msg ^ " mode")
123 let s = Naive.stats () in
126 "@[tree size: %d@\ntraversals: %d@\ntransition fetch cache hit ratio: %f@\ntransition eval cache hit ratio: %f@]"
128 (float s.fetch_trans_cache_hit /. float s.fetch_trans_cache_access)
129 (float s.eval_trans_cache_hit /. float s.eval_trans_cache_access));
132 List.iter (fun results ->
133 output_string output "<xml_result num=\"";
134 output_string output (string_of_int !count);
135 output_string output "\" >\n";
136 if !Options.count then begin
137 output_string output (string_of_int (List.length results));
138 output_char output '\n';
141 Naive_tree.print_xml output doc n;
142 output_char output '\n'
144 output_string output "</xml_result>\n";
148 if output != stdout then close_out output
150 ) () "serializing results"
157 Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
158 | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
159 | Tree.Parse_error msg ->
160 eprintf "Error: %s, %s\n%!"
161 (match !Options.input_file with
162 Some s -> ("file " ^ s)
163 | None -> "[stdin]") msg; exit 3
164 | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
165 | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128