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 (******************************************************************************)
13 let t1 = Unix.gettimeofday () in
15 let t2 = Unix.gettimeofday () in
16 let t = (1000. *. (t2 -. t1)) in
18 Printf.eprintf " %fms\n%!" t ;
21 let total_time () = List.fold_left (+.) 0. !l;;
22 let enabled_gc = Gc.get()
23 let disabled_gc = { Gc.get() with
24 Gc.max_overhead = 1000000;
25 Gc.space_overhead = 100 }
27 let main v query output =
28 let _ = Tag.init (Tree.tag_pool v) in
29 Printf.eprintf "Parsing query : ";
32 XPath.Parser.parse_string query
34 Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
36 XPath.Ast.print Format.err_formatter query;
37 Format.fprintf Format.err_formatter "\n%!";
38 Printf.eprintf "Compiling query : ";
39 let auto,ltags,contains = time XPath.Compile.compile query in
40 let _ = Ata.dump Format.err_formatter auto in
41 let _ = Printf.eprintf "%!" in
44 None -> (max_int,`NOTHING)
46 let r = Tree.count v s
48 Printf.eprintf "%i documents in the TextCollection\n" (Tree.text_size v);
49 Printf.eprintf "Global count is %i, using " r;
50 if r < !Options.tc_threshold then begin
51 Printf.eprintf "TextCollection contains\nCalling global contains : ";
52 time (Tree.init_contains v) s;
55 Printf.eprintf "Naive contains\nCalling global contains : ";
56 time (Tree.init_naive_contains v) s
60 if (!Options.backward) then begin
61 Printf.eprintf "Finding min occurences : ";
63 ( List.fold_left (fun ((min_occ,kind)as acc) (tag,_) ->
64 let numtags = Tree.subtree_tags v tag in
65 if ((numtags < min_occ) && numtags >= 2)
66 then (numtags,`TAG(tag))
67 else acc) jump_to) ltags
69 else (max_int,`NOTHING)
71 let _ = if (snd test_list) != `NOTHING then
72 let occ,s1,s2 = match test_list with
73 | (x,`TAG (tag)) -> (x, "tag", (Tag.to_string tag))
74 | (x,`CONTAINS(s)) -> (x, "contains()", s)
77 Printf.eprintf "Will jump to %s %s occuring %i time\n%!" s1 s2 occ
79 Printf.eprintf "Execution time %s : "
80 (if !Options.count_only then "(counting only)" else if !Options.backward then "(bottomup)" else "");
82 let _ = Gc.full_major();Gc.compact() in
83 (* let _ = Gc.set (disabled_gc) in *)
84 if !Options.backward && ((snd test_list) != `NOTHING )then
86 let r = time (bottom_up_count auto v )(snd test_list) in
87 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
91 if !Options.backward then Printf.eprintf "WARNING: couldn't find a jumping point, running top-down\n"
93 if !Options.count_only then
94 let r = time ( top_down_count auto ) v in
95 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
98 let result = time (top_down auto) v in
99 let rcount = IdSet.length result in
100 Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
101 Printf.eprintf "\n%!";
106 Printf.eprintf "Serializing results : ";
108 let oc = open_out f in
109 output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
111 Tree.print_xml_fast oc t;
113 output_string oc "----------\n";
117 let _ = Gc.set enabled_gc in
118 Printf.eprintf "Total running time : %fms\n%!" (total_time())
121 Options.parse_cmdline();;
124 if (Filename.check_suffix !Options.input_file ".srx")
127 Printf.eprintf "Loading from file : ";
128 time (Tree.load ~sample:!Options.sample_factor )
129 (Filename.chop_suffix !Options.input_file ".srx");
133 time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
134 in Printf.eprintf "Parsing document : %!";v
137 if !Options.save_file <> ""
139 Printf.eprintf "Writing file to disk : ";
140 time (Tree.save v) !Options.save_file;
144 main v !Options.query !Options.output_file;;
148 Printf.eprintf "\n=================================================\nDEBUGGING\n%!";
150 Tree.DEBUGTREE.print_stats Format.err_formatter;;