]> matita.cs.unibo.it Git - helm.git/blob - components/library/libraryClean.ml
maxipatch for support of multiple DBs.
[helm.git] / components / library / libraryClean.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 open Printf
29
30 let debug = false
31 let debug_prerr = if debug then prerr_endline else ignore
32
33 module HGT = Http_getter_types;;
34 module HG = Http_getter;;
35 module UM = UriManager;;
36
37 let cache_of_processed_baseuri = Hashtbl.create 1024
38
39 let one_step_depend suri dbtype dbd =
40   let buri =
41     try
42       UM.buri_of_uri (UM.uri_of_string suri)
43     with UM.IllFormedUri _ -> suri
44   in
45   if Hashtbl.mem cache_of_processed_baseuri buri then 
46     []
47   else
48     begin
49       Hashtbl.add cache_of_processed_baseuri buri true;
50       let query = 
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
57         else
58                 begin
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";
63           sprintf
64             ("SELECT source, h_occurrence FROM %s WHERE " ^^ 
65              "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
66                   obj_tbl buri*)
67                 end
68       in
69       try 
70         let rc = HSql.exec dbtype dbd query in
71         let l = ref [] in
72         HSql.iter rc (
73           fun row -> 
74             match row.(0), row.(1) with 
75             | Some uri, Some occ when Filename.dirname occ = buri -> 
76                 l := uri :: !l
77             | _ -> ());
78         let l = List.sort Pervasives.compare !l in
79         HExtlib.list_uniq l
80       with
81         exn -> raise exn (* no errors should be accepted *)
82     end
83     
84 let safe_buri_of_suri suri =
85   try
86     UM.buri_of_uri (UM.uri_of_string suri)
87   with
88     UM.IllFormedUri _ -> suri
89
90 let db_uris_of_baseuri buri =
91  let dbd = LibraryDb.instance () in
92  let dbtype = 
93    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
94  in
95  let query = 
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
102   else
103    begin
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";
108     sprintf
109     ("SELECT source, h_occurrence FROM %s WHERE " ^^ 
110     "h_occurrence LIKE '%s%%' " ^^ HSql.escape_string_for_like)
111     obj_tbl buri
112     *)
113    end
114  in
115  try 
116   let rc = HSql.exec dbtype dbd query in
117   let l = ref [] in
118   HSql.iter rc (
119     fun row -> 
120       match row.(0) with 
121       | Some uri when Filename.dirname uri = buri -> 
122           l := uri :: !l
123       | _ -> ());
124   let l = List.sort Pervasives.compare !l in
125   HExtlib.list_uniq l
126  with
127   exn -> raise exn (* no errors should be accepted *)
128 ;;
129
130 let close_uri_list uri_to_remove =
131   let dbd = LibraryDb.instance () in
132   let dbtype = 
133     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
134   in
135   (* to remove an uri you have to remove the whole script *)
136   let buri_to_remove = 
137     HExtlib.list_uniq 
138       (List.fast_sort Pervasives.compare 
139         (List.map safe_buri_of_suri uri_to_remove))
140   in
141   (* cleand the already visided baseuris *)
142   let buri_to_remove = 
143     List.filter 
144       (fun buri -> 
145         if Hashtbl.mem cache_of_processed_baseuri buri then false
146         else true)
147       buri_to_remove
148   in
149   (* now calculate the list of objects that belong to these baseuris *)
150   let uri_to_remove = 
151     try
152       List.fold_left 
153         (fun acc buri ->
154           let inhabitants = HG.ls ~local:true (buri ^ "/") in
155           let inhabitants = List.filter 
156               (function HGT.Ls_object _ -> true | _ -> false) 
157             inhabitants
158           in
159           let inhabitants = List.map 
160               (function 
161                | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri 
162                | _ -> assert false)
163             inhabitants
164           in
165           inhabitants @ acc)
166       [] buri_to_remove 
167     with HGT.Invalid_URI u -> 
168       HLog.error ("We were listing an invalid buri: " ^ u);
169       exit 1
170   in
171   let uri_to_remove_from_db =
172    List.fold_left 
173      (fun acc buri -> db_uris_of_baseuri buri @ acc
174      ) [] buri_to_remove 
175   in
176   let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
177   let uri_to_remove =
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 *) 
180   let depend = 
181     List.fold_left
182     (fun acc u -> one_step_depend u dbtype dbd @ acc) [] uri_to_remove
183   in
184   let depend = 
185     HExtlib.list_uniq (List.fast_sort Pervasives.compare depend) 
186   in
187   uri_to_remove, depend
188
189 let rec close_db uris next =
190   match next with
191   | [] -> uris
192   | l -> let uris, next = close_uri_list l in close_db uris next @ uris
193   
194 let cleaned_no = ref 0;;
195
196   (** TODO repellent code ... *)
197 let moo_root_dir = lazy (
198   let url =
199     List.assoc "cic:/matita/"
200       (List.map
201         (fun pair ->
202           match
203             Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
204           with
205           | a::b::_ -> a, b
206           | _ -> assert false)
207         (Helm_registry.get_list Helm_registry.string "getter.prefix"))
208   in
209   String.sub url 7 (String.length url - 7)  (* remove heading "file:///" *)
210 )
211
212 let clean_baseuris ?(verbose=true) buris =
213   let dbd = LibraryDb.instance () in
214   let dbtype = 
215     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
216   in
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:";
220   if debug then
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:";
226   if debug then
227     List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l; 
228   List.iter
229    (fun baseuri ->
230      try 
231       let obj_file =
232        LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
233       in
234        HExtlib.safe_remove obj_file ;
235        HExtlib.safe_remove 
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)));
242   List.iter
243    (let last_baseuri = ref "" in
244     fun uri ->
245      let buri = UriManager.buri_of_uri uri in
246      if buri <> !last_baseuri then
247       begin
248         if Helm_registry.get_bool "matita.bench" then
249             (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
250           else 
251             HLog.message ("Removing: " ^ buri ^ "/*");
252        last_baseuri := buri
253       end;
254      LibrarySync.remove_obj uri
255    ) l;
256   if HSql.isMysql dbtype dbd then
257    begin
258    cleaned_no := !cleaned_no + List.length l;
259    if !cleaned_no > 30 && HSql.isMysql dbtype dbd then
260     begin
261      cleaned_no := 0;
262      List.iter
263       (function table ->
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()]
268     end
269    end