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 =
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 buri in
53 let obj_tbl = MetadataTypes.obj_tbl () in
55 sprintf ("SELECT source, h_occurrence FROM %s WHERE "
56 ^^ "h_occurrence REGEXP '^%s[^/]*$'") obj_tbl buri
59 HLog.debug "Warning SELECT without REGEXP";
61 ("SELECT source, h_occurrence FROM %s WHERE " ^^
62 "h_occurrence LIKE '%s%%'")
67 let rc = HSql.exec (LibraryDb.instance ()) query in
71 match row.(0), row.(1) with
72 | Some uri, Some occ when Filename.dirname occ = buri ->
75 let l = List.sort Pervasives.compare !l in
78 exn -> raise exn (* no errors should be accepted *)
81 let safe_buri_of_suri suri =
83 UM.buri_of_uri (UM.uri_of_string suri)
85 UM.IllFormedUri _ -> suri
87 let close_uri_list uri_to_remove =
88 (* to remove an uri you have to remove the whole script *)
91 (List.fast_sort Pervasives.compare
92 (List.map safe_buri_of_suri uri_to_remove))
94 (* cleand the already visided baseuris *)
98 if Hashtbl.mem cache_of_processed_baseuri buri then false
102 (* now calculate the list of objects that belong to these baseuris *)
107 let inhabitants = HG.ls (buri ^ "/") in
108 let inhabitants = List.filter
109 (function HGT.Ls_object _ -> true | _ -> false)
112 let inhabitants = List.map
114 | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri
120 with HGT.Invalid_URI u ->
121 HLog.error ("We were listing an invalid buri: " ^ u);
124 (* now we want the list of all uri that depend on them *)
127 (fun acc u -> one_step_depend u @ acc) [] uri_to_remove
130 HExtlib.list_uniq (List.fast_sort Pervasives.compare depend)
132 uri_to_remove, depend
134 let rec close_db uris next =
137 | l -> let uris, next = close_uri_list l in close_db uris next @ uris
139 let cleaned_no = ref 0;;
141 (** TODO repellent code ... *)
142 let moo_root_dir = lazy (
144 List.assoc "cic:/matita/"
148 Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
152 (Helm_registry.get_list Helm_registry.string "getter.prefix"))
154 String.sub url 7 (String.length url - 7) (* remove heading "file:///" *)
157 let close_nodb buris =
158 let rev_deps = Hashtbl.create 97 in
160 HExtlib.find ~test:(fun name -> Filename.check_suffix name ".metadata")
161 (Lazy.force moo_root_dir)
165 let metadata = LibraryNoDb.load_metadata ~fname:path in
166 let baseuri_of_current_metadata =
167 prerr_endline "ERROR, add to the getter reverse lookup";
168 let basedir = "/fake" in
169 let dirname = Filename.dirname path in
170 let basedirlen = String.length basedir in
171 assert (String.sub dirname 0 basedirlen = basedir);
173 String.sub dirname basedirlen (String.length dirname - basedirlen) ^
174 Filename.basename path
178 (function LibraryNoDb.Dependency buri -> Some buri)
182 (fun buri -> Hashtbl.add rev_deps buri baseuri_of_current_metadata) deps)
184 let buris_to_remove =
186 (List.fast_sort Pervasives.compare
187 (List.flatten (List.map (Hashtbl.find_all rev_deps) buris)))
189 let objects_to_remove =
190 let objs_of_buri buri =
193 | Http_getter_types.Ls_object o ->
194 Some (buri ^ "/" ^ o.Http_getter_types.uri)
196 (Http_getter.ls buri)
198 List.flatten (List.map objs_of_buri (buris @ buris_to_remove))
202 let clean_baseuris ?(verbose=true) buris =
203 Hashtbl.clear cache_of_processed_baseuri;
204 let buris = List.map Http_getter_misc.strip_trailing_slash buris in
205 debug_prerr "clean_baseuris called on:";
207 List.iter debug_prerr buris;
209 if Helm_registry.get_bool "db.nodb" then
214 let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
215 let l = List.map UriManager.uri_of_string l in
216 debug_prerr "clean_baseuri will remove:";
218 List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l;
223 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
225 HExtlib.safe_remove obj_file ;
227 (LibraryMisc.metadata_file_of_baseuri
228 ~must_exist:false ~writable:true ~baseuri) ;
230 (LibraryMisc.lexicon_file_of_baseuri
231 ~must_exist:false ~writable:true ~baseuri) ;
232 HExtlib.rmdir_descend (Filename.chop_extension obj_file)
233 with Http_getter_types.Key_not_found _ -> ())
234 (HExtlib.list_uniq (List.fast_sort Pervasives.compare
235 (List.map (UriManager.buri_of_uri) l @ buris)));
237 (let last_baseuri = ref "" in
239 let buri = UriManager.buri_of_uri uri in
240 if buri <> !last_baseuri then
242 if Helm_registry.get_bool "matita.bench" then
243 (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
245 HLog.message ("Removing: " ^ buri ^ "/*");
248 LibrarySync.remove_obj uri
252 cleaned_no := !cleaned_no + List.length l;
253 if !cleaned_no > 30 then
258 ignore (HSql.exec (LibraryDb.instance ()) ("OPTIMIZE TABLE " ^ table)))
259 [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
260 MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
261 MetadataTypes.count_tbl()]