1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
31 let debug_prerr = if debug then prerr_endline else ignore
33 module HGT = Http_getter_types;;
34 module HG = Http_getter;;
35 module UM = UriManager;;
37 let cache_of_processed_baseuri = Hashtbl.create 1024
39 let one_step_depend suri dbtype dbd =
42 UM.buri_of_uri (UM.uri_of_string suri)
43 with UM.IllFormedUri _ -> suri
45 if Hashtbl.mem cache_of_processed_baseuri buri then
49 Hashtbl.add cache_of_processed_baseuri buri true;
51 let buri = buri ^ "/" in
52 let buri = HSql.escape dbtype dbd buri in
53 let obj_tbl = MetadataTypes.obj_tbl () in
54 if HSql.isMysql dbtype dbd then
55 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
56 ^^ "h_occurrence REGEXP '^%s[^/]*$'") obj_tbl buri
59 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
60 ^^ "REGEXP(h_occurrence, '^%s[^/]*$')") obj_tbl buri
61 (* implementation with vanilla ocaml-sqlite3
62 HLog.debug "Warning SELECT without REGEXP";
64 ("SELECT source, h_occurrence FROM %s WHERE " ^^
65 "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
70 let rc = HSql.exec dbtype dbd query in
74 match row.(0), row.(1) with
75 | Some uri, Some occ when Filename.dirname occ = buri ->
78 let l = List.sort Pervasives.compare !l in
81 exn -> raise exn (* no errors should be accepted *)
84 let safe_buri_of_suri suri =
86 UM.buri_of_uri (UM.uri_of_string suri)
88 UM.IllFormedUri _ -> suri
90 let db_uris_of_baseuri buri =
91 let dbd = LibraryDb.instance () in
93 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
96 let buri = buri ^ "/" in
97 let buri = HSql.escape dbtype dbd buri in
98 let obj_tbl = MetadataTypes.name_tbl () in
99 if HSql.isMysql dbtype dbd then
100 sprintf ("SELECT source FROM %s WHERE "
101 ^^ "source REGEXP '^%s[^/]*$'") obj_tbl buri
104 sprintf ("SELECT source FROM %s WHERE "
105 ^^ "REGEXP(source, '^%s[^/]*$')") obj_tbl buri
106 (* implementation with vanilla ocaml-sqlite3
107 HLog.debug "Warning SELECT without REGEXP";
109 ("SELECT source, h_occurrence FROM %s WHERE " ^^
110 "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
116 let rc = HSql.exec dbtype dbd query in
121 | Some uri when Filename.dirname uri = buri ->
124 let l = List.sort Pervasives.compare !l in
127 exn -> raise exn (* no errors should be accepted *)
130 let close_uri_list uri_to_remove =
131 let dbd = LibraryDb.instance () in
133 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
135 (* to remove an uri you have to remove the whole script *)
138 (List.fast_sort Pervasives.compare
139 (List.map safe_buri_of_suri uri_to_remove))
141 (* cleand the already visided baseuris *)
145 if Hashtbl.mem cache_of_processed_baseuri buri then false
149 (* now calculate the list of objects that belong to these baseuris *)
154 let inhabitants = HG.ls ~local:true (buri ^ "/") in
155 let inhabitants = List.filter
156 (function HGT.Ls_object _ -> true | _ -> false)
159 let inhabitants = List.map
161 | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri
167 with HGT.Invalid_URI u ->
168 HLog.error ("We were listing an invalid buri: " ^ u);
171 let uri_to_remove_from_db =
173 (fun acc buri -> db_uris_of_baseuri buri @ acc
176 let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
178 HExtlib.list_uniq (List.sort Pervasives.compare uri_to_remove) in
179 (* now we want the list of all uri that depend on them *)
182 (fun acc u -> one_step_depend u dbtype dbd @ acc) [] uri_to_remove
185 HExtlib.list_uniq (List.fast_sort Pervasives.compare depend)
187 uri_to_remove, depend
189 let rec close_db uris next =
192 | l -> let uris, next = close_uri_list l in close_db uris next @ uris
194 let cleaned_no = ref 0;;
196 (** TODO repellent code ... *)
197 let moo_root_dir = lazy (
199 List.assoc "cic:/matita/"
203 Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
207 (Helm_registry.get_list Helm_registry.string "getter.prefix"))
209 String.sub url 7 (String.length url - 7) (* remove heading "file:///" *)
212 let clean_baseuris ?(verbose=true) buris =
213 let dbd = LibraryDb.instance () in
215 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
217 Hashtbl.clear cache_of_processed_baseuri;
218 let buris = List.map Http_getter_misc.strip_trailing_slash buris in
219 debug_prerr "clean_baseuris called on:";
221 List.iter debug_prerr buris;
222 let l = close_db [] buris in
223 let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
224 let l = List.map UriManager.uri_of_string l in
225 debug_prerr "clean_baseuri will remove:";
227 List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l;
232 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
234 HExtlib.safe_remove obj_file ;
236 (LibraryMisc.lexicon_file_of_baseuri
237 ~must_exist:false ~writable:true ~baseuri) ;
238 HExtlib.rmdir_descend (Filename.chop_extension obj_file)
239 with Http_getter_types.Key_not_found _ -> ())
240 (HExtlib.list_uniq (List.fast_sort Pervasives.compare
241 (List.map (UriManager.buri_of_uri) l @ buris)));
243 (let last_baseuri = ref "" in
245 let buri = UriManager.buri_of_uri uri in
246 if buri <> !last_baseuri then
248 if Helm_registry.get_bool "matita.bench" then
249 (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
251 HLog.message ("Removing: " ^ buri ^ "/*");
254 LibrarySync.remove_obj uri
256 if HSql.isMysql dbtype dbd then
258 cleaned_no := !cleaned_no + List.length l;
259 if !cleaned_no > 30 && HSql.isMysql dbtype dbd then
264 ignore (HSql.exec dbtype dbd ("OPTIMIZE TABLE " ^ table)))
265 [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
266 MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
267 MetadataTypes.count_tbl()]