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 (******************************************************************************)
10 let () = init_timer();;
12 let enabled_gc = Gc.get()
13 let disabled_gc = { Gc.get() with
14 Gc.max_overhead = 1000000;
15 Gc.space_overhead = 100 }
21 let main v query_string output =
23 let _ = Tag.init (Tree.tag_pool v) in
24 Printf.eprintf "Parsing query : ";
27 XPath.Parser.parse_string query_string
29 Ulexer.Loc.Exc_located ((x,y),e) -> Printf.eprintf "character %i-%i %s\n" x y (Printexc.to_string e);exit 1
31 XPath.Ast.print Format.err_formatter query;
32 Format.fprintf Format.err_formatter "\n%!";
33 Printf.eprintf "Compiling query : ";
34 let auto,ltags,contains = time (XPath.Compile.compile ~querystring:query_string) query in
35 let _ = Ata.dump Format.err_formatter auto in
36 let _ = Printf.eprintf "%!" in
39 None -> (max_int,`NOTHING)
41 let r = Tree.count v s
43 Printf.eprintf "%i documents in the TextCollection\n" (Tree.text_size v);
44 Printf.eprintf "Global count is %i, using " r;
45 if r < !Options.tc_threshold then begin
46 Printf.eprintf "TextCollection contains\nCalling global contains : ";
47 time (Tree.init_contains v) s;
50 Printf.eprintf "Naive contains\nCalling global contains : ";
51 time (Tree.init_naive_contains v) s
54 let test_list = jump_to in
57 if (!Options.backward) then begin
58 Printf.eprintf "Finding min occurences : ";
60 ( List.fold_left (fun ((min_occ,kind)as acc) (tag,_) ->
61 let numtags = Tree.subtree_tags v tag Tree.root in
62 if ((numtags < min_occ) && numtags >= 2)
63 then (numtags,`TAG(tag))
64 else acc) jump_to) ltags
66 else (max_int,`NOTHING)
68 let _ = if (snd test_list) != `NOTHING then
69 let occ,s1,s2 = match test_list with
70 | (x,`TAG (tag)) -> (x, "tag", (Tag.to_string tag))
71 | (x,`CONTAINS(s)) -> (x, "contains()", s)
74 Printf.eprintf "Will jump to %s %s occuring %i time\n%!" s1 s2 occ
76 Printf.eprintf "Execution time %s : "
77 (if !Options.count_only then "(counting only)" else if !Options.backward then "(bottomup)" else "");
79 let _ = Gc.full_major();Gc.compact() in
80 let _ = Printf.eprintf "%!" in
81 (* let _ = Gc.set (disabled_gc) in *)
82 if !Options.backward && ((snd test_list) != `NOTHING )then
84 let r = time (bottom_up_count auto v )(snd test_list) in
85 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
89 if !Options.backward then Printf.eprintf "WARNING: couldn't find a jumping point, running top-down\n"
91 if !Options.count_only then
92 let r = time ( top_down_count auto ) v in
93 let _ = Printf.eprintf "Number of nodes in the result set : %i\n%!" r
96 let result = time (top_down auto) v in
97 let rcount = GResult.length result in
98 Printf.eprintf "Number of nodes in the result set : %i\n" rcount;
99 Printf.eprintf "\n%!";
104 Printf.eprintf "Serializing results : ";
106 let oc = open_out f in
107 output_string oc "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
108 GResult.iter (fun t ->
109 Tree.print_xml_fast oc v t;
115 let _ = Gc.set enabled_gc in
116 Printf.eprintf "Total running time : %fms\n%!" (total_time())
119 Options.parse_cmdline();;
122 if (Filename.check_suffix !Options.input_file ".srx")
125 Printf.eprintf "Loading from file : ";
126 time (Tree.load ~sample:!Options.sample_factor )
131 time (fun () -> let v = Tree.parse_xml_uri !Options.input_file;
132 in Printf.eprintf "Parsing document : %!";v
135 if !Options.save_file <> ""
137 Printf.eprintf "Writing file to disk : ";
138 time (Tree.save v) !Options.save_file;
142 main v !Options.query !Options.output_file;;