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)(Naive_node_list) in
109 let root = Naive_node_list.create () in
110 let () = Naive_node_list.add (Naive_tree.root doc) root in
112 match !Options.parallel, !Options.compose with
114 compose_parallel Naive.eval auto_list doc root, "parallel/compose"
116 restart_parallel Naive.full_eval auto_list doc root, "parallel/restart"
118 compose_sequential Naive.eval auto_list doc root , "sequential/compose"
120 restart_sequential Naive.eval auto_list doc root, "sequential/restart"
122 time f () (Printf.sprintf "evaluating quer%s in %s mode"
123 (match auto_list with
124 [] | [ _ ] -> "y" | _ -> "ies" )
127 let sl = Naive.stats () in
131 "@[automaton: %d@\ntree size: %d@\ntraversals: %d@\ntransition fetch cache hit ratio: %f@\ntransition eval cache hit ratio: %f@\nNumber of visited nodes per pass: %a@]"
132 (s.auto :> int) s.tree_size s.pass
133 (float s.fetch_trans_cache_hit /. float s.fetch_trans_cache_access)
134 (float s.eval_trans_cache_hit /. float s.eval_trans_cache_access)
136 Pretty.print_list ~sep:"," (fun fmt n -> Format.fprintf fmt "%i: %i" !i n;incr i))
137 s.nodes_per_run)) sl;
140 List.iter (fun results ->
141 output_string output "<xml_result num=\"";
142 output_string output (string_of_int !count);
143 output_string output "\" >\n";
144 if !Options.count then begin
145 output_string output (string_of_int (Naive_node_list.length results));
146 output_char output '\n';
148 Naive_node_list.iter (fun n ->
149 Naive_tree.print_xml output doc n;
150 output_char output '\n'
152 output_string output "</xml_result>\n";
156 if output != stdout then close_out output
158 ) () "serializing results"
165 Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
166 | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
167 | Tree.Parse_error msg ->
168 eprintf "Error: %s, %s\n%!"
169 (match !Options.input_file with
170 Some s -> ("file " ^ s)
171 | None -> "[stdin]") msg; exit 3
172 | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
173 | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128