]> matita.cs.unibo.it Git - helm.git/blob - components/library/libraryClean.ml
make directory erased, no more -bench since it is the default,
[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[^/]*(#xpointer.*)?$'") obj_tbl buri
102   else
103    begin
104     sprintf ("SELECT source FROM %s WHERE " 
105       ^^ "REGEXP(source, '^%s[^/]*\\(#xpointer.*\\)?$')") obj_tbl buri
106    end
107  in
108  try 
109   let rc = HSql.exec dbtype dbd query in
110   let strip_xpointer s = Pcre.replace ~pat:"#.*$" s in
111   let l = ref [] in
112   HSql.iter rc (
113     fun row -> 
114       match row.(0) with 
115       | Some uri when Filename.dirname (strip_xpointer uri) = buri -> 
116           l := uri :: !l
117       | _ ->
118           ());
119   let l = List.sort Pervasives.compare !l in
120   HExtlib.list_uniq l
121  with
122   exn -> raise exn (* no errors should be accepted *)
123 ;;
124
125 let close_uri_list uri_to_remove =
126   let dbd = LibraryDb.instance () in
127   let dbtype = 
128     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
129   in
130   (* to remove an uri you have to remove the whole script *)
131   let buri_to_remove = 
132     HExtlib.list_uniq 
133       (List.fast_sort Pervasives.compare 
134         (List.map safe_buri_of_suri uri_to_remove))
135   in
136   (* cleand the already visided baseuris *)
137   let buri_to_remove = 
138     List.filter 
139       (fun buri -> 
140         if Hashtbl.mem cache_of_processed_baseuri buri then false
141         else true)
142       buri_to_remove
143   in
144   (* now calculate the list of objects that belong to these baseuris *)
145   let uri_to_remove = 
146     try
147       List.fold_left 
148         (fun acc buri ->
149           let inhabitants = HG.ls ~local:true (buri ^ "/") in
150           let inhabitants = List.filter 
151               (function HGT.Ls_object _ -> true | _ -> false) 
152             inhabitants
153           in
154           let inhabitants = List.map 
155               (function 
156                | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri 
157                | _ -> assert false)
158             inhabitants
159           in
160           inhabitants @ acc)
161       [] buri_to_remove 
162     with HGT.Invalid_URI u -> 
163       HLog.error ("We were listing an invalid buri: " ^ u);
164       exit 1
165   in
166   let uri_to_remove_from_db =
167    List.fold_left 
168      (fun acc buri -> 
169        let dbu = db_uris_of_baseuri buri in
170        dbu @ acc
171      ) [] buri_to_remove 
172   in
173   let uri_to_remove = uri_to_remove @ uri_to_remove_from_db in
174   let uri_to_remove =
175    HExtlib.list_uniq (List.sort Pervasives.compare uri_to_remove) in
176   (* now we want the list of all uri that depend on them *) 
177   let depend = 
178     List.fold_left
179     (fun acc u -> one_step_depend u dbtype dbd @ acc) [] uri_to_remove
180   in
181   let depend = 
182     HExtlib.list_uniq (List.fast_sort Pervasives.compare depend) 
183   in
184   uri_to_remove, depend
185
186 let rec close_db uris next =
187   match next with
188   | [] -> uris
189   | l -> let uris, next = close_uri_list l in close_db uris next @ uris
190   
191 let cleaned_no = ref 0;;
192
193   (** TODO repellent code ... *)
194 let moo_root_dir = lazy (
195   let url =
196     List.assoc "cic:/matita/"
197       (List.map
198         (fun pair ->
199           match
200             Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
201           with
202           | a::b::_ -> a, b
203           | _ -> assert false)
204         (Helm_registry.get_list Helm_registry.string "getter.prefix"))
205   in
206   String.sub url 7 (String.length url - 7)  (* remove heading "file:///" *)
207 )
208
209 let clean_baseuris ?(verbose=true) buris =
210   let dbd = LibraryDb.instance () in
211   let dbtype = 
212     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
213   in
214   Hashtbl.clear cache_of_processed_baseuri;
215   let buris = List.map Http_getter_misc.strip_trailing_slash buris in
216   debug_prerr "clean_baseuris called on:";
217   if debug then
218     List.iter debug_prerr buris; 
219   let l = close_db [] buris in
220   let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
221   let l = List.map UriManager.uri_of_string l in
222   debug_prerr "clean_baseuri will remove:";
223   if debug then
224     List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l; 
225   List.iter
226    (fun baseuri ->
227      try 
228       let obj_file =
229        LibraryMisc.obj_file_of_baseuri ~must_exist:false ~writable:true ~baseuri
230       in
231        HExtlib.safe_remove obj_file ;
232        HExtlib.safe_remove 
233          (LibraryMisc.lexicon_file_of_baseuri 
234            ~must_exist:false ~writable:true ~baseuri) ;
235        HExtlib.rmdir_descend (Filename.chop_extension obj_file)
236      with Http_getter_types.Key_not_found _ -> ())
237    (HExtlib.list_uniq (List.fast_sort Pervasives.compare
238      (List.map (UriManager.buri_of_uri) l @ buris)));
239   List.iter
240    (let last_baseuri = ref "" in
241     fun uri ->
242      let buri = UriManager.buri_of_uri uri in
243      if buri <> !last_baseuri then
244       begin
245         if not (Helm_registry.get_bool "matita.verbose") then
246             (print_endline ("matitaclean " ^ buri ^ "/");flush stdout)
247           else 
248             HLog.message ("Removing: " ^ buri ^ "/*");
249        last_baseuri := buri
250       end;
251      LibrarySync.remove_obj uri
252    ) l;
253   if HSql.isMysql dbtype dbd then
254    begin
255    cleaned_no := !cleaned_no + List.length l;
256    if !cleaned_no > 30 && HSql.isMysql dbtype dbd then
257     begin
258      cleaned_no := 0;
259      List.iter
260       (function table ->
261         ignore (HSql.exec dbtype dbd ("OPTIMIZE TABLE " ^ table)))
262       [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
263        MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
264        MetadataTypes.count_tbl()]
265     end
266    end