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 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%%'")
70 let rc = HSql.exec (LibraryDb.instance ()) 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 =
92 let buri = buri ^ "/" in
93 let buri = HSql.escape buri in
94 let obj_tbl = MetadataTypes.name_tbl () in
96 sprintf ("SELECT source FROM %s WHERE "
97 ^^ "source REGEXP '^%s[^/]*$'") obj_tbl buri
100 sprintf ("SELECT source FROM %s WHERE "
101 ^^ "REGEXP(source, '^%s[^/]*$')") obj_tbl buri
102 (* implementation with vanilla ocaml-sqlite3
103 HLog.debug "Warning SELECT without REGEXP";
105 ("SELECT source, h_occurrence FROM %s WHERE " ^^
106 "h_occurrence LIKE '%s%%'")
112 let rc = HSql.exec (LibraryDb.instance ()) query in
117 | Some uri when Filename.dirname uri = buri ->
120 let l = List.sort Pervasives.compare !l in
123 exn -> raise exn (* no errors should be accepted *)
126 let close_uri_list uri_to_remove =
127 (* to remove an uri you have to remove the whole script *)
130 (List.fast_sort Pervasives.compare
131 (List.map safe_buri_of_suri uri_to_remove))
133 (* cleand the already visided baseuris *)
137 if Hashtbl.mem cache_of_processed_baseuri buri then false
141 (* now calculate the list of objects that belong to these baseuris *)
146 let inhabitants = HG.ls (buri ^ "/") in
147 let inhabitants = List.filter
148 (function HGT.Ls_object _ -> true | _ -> false)
151 let inhabitants = List.map
153 | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri
159 with HGT.Invalid_URI u ->
160 HLog.error ("We were listing an invalid buri: " ^ u);
163 let uri_to_remove_from_db =
165 (fun acc buri -> db_uris_of_baseuri buri @ acc
168 let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
170 HExtlib.list_uniq (List.sort Pervasives.compare uri_to_remove) in
171 (* now we want the list of all uri that depend on them *)
174 (fun acc u -> one_step_depend u @ acc) [] uri_to_remove
177 HExtlib.list_uniq (List.fast_sort Pervasives.compare depend)
179 uri_to_remove, depend
181 let rec close_db uris next =
184 | l -> let uris, next = close_uri_list l in close_db uris next @ uris
186 let cleaned_no = ref 0;;
188 (** TODO repellent code ... *)
189 let moo_root_dir = lazy (
191 List.assoc "cic:/matita/"
195 Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
199 (Helm_registry.get_list Helm_registry.string "getter.prefix"))
201 String.sub url 7 (String.length url - 7) (* remove heading "file:///" *)
204 let close_nodb buris =
205 let rev_deps = Hashtbl.create 97 in
207 HExtlib.find ~test:(fun name -> Filename.check_suffix name ".metadata")
208 (Lazy.force moo_root_dir)
212 let metadata = LibraryNoDb.load_metadata ~fname:path in
213 let baseuri_of_current_metadata =
214 prerr_endline "ERROR, add to the getter reverse lookup";
215 let basedir = "/fake" in
216 let dirname = Filename.dirname path in
217 let basedirlen = String.length basedir in
218 assert (String.sub dirname 0 basedirlen = basedir);
220 String.sub dirname basedirlen (String.length dirname - basedirlen) ^
221 Filename.basename path
225 (function LibraryNoDb.Dependency buri -> Some buri)
229 (fun buri -> Hashtbl.add rev_deps buri baseuri_of_current_metadata) deps)
231 let buris_to_remove =
233 (List.fast_sort Pervasives.compare
234 (List.flatten (List.map (Hashtbl.find_all rev_deps) buris)))
236 let objects_to_remove =
237 let objs_of_buri buri =
240 | Http_getter_types.Ls_object o ->
241 Some (buri ^ "/" ^ o.Http_getter_types.uri)
243 (Http_getter.ls buri)
245 List.flatten (List.map objs_of_buri (buris @ buris_to_remove))
249 let clean_baseuris ?(verbose=true) buris =
250 Hashtbl.clear cache_of_processed_baseuri;
251 let buris = List.map Http_getter_misc.strip_trailing_slash buris in
252 debug_prerr "clean_baseuris called on:";
254 List.iter debug_prerr buris;
256 if Helm_registry.get_bool "db.nodb" then
261 let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
262 let l = List.map UriManager.uri_of_string l in
263 debug_prerr "clean_baseuri will remove:";
265 List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l;
270 LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
272 HExtlib.safe_remove obj_file ;
274 (LibraryMisc.metadata_file_of_baseuri
275 ~must_exist:false ~writable:true ~baseuri) ;
277 (LibraryMisc.lexicon_file_of_baseuri
278 ~must_exist:false ~writable:true ~baseuri) ;
279 HExtlib.rmdir_descend (Filename.chop_extension obj_file)
280 with Http_getter_types.Key_not_found _ -> ())
281 (HExtlib.list_uniq (List.fast_sort Pervasives.compare
282 (List.map (UriManager.buri_of_uri) l @ buris)));
284 (let last_baseuri = ref "" in
286 let buri = UriManager.buri_of_uri uri in
287 if buri <> !last_baseuri then
289 if Helm_registry.get_bool "matita.bench" then
290 (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
292 HLog.message ("Removing: " ^ buri ^ "/*");
295 LibrarySync.remove_obj uri
299 cleaned_no := !cleaned_no + List.length l;
300 if !cleaned_no > 30 then
305 ignore (HSql.exec (LibraryDb.instance ()) ("OPTIMIZE TABLE " ^ table)))
306 [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
307 MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
308 MetadataTypes.count_tbl()]