]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/library/libraryClean.ml
1. matitaEngine splitted into disambiguation (now in grafite_parser) and
[helm.git] / helm / ocaml / 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 open Printf
27
28 let debug = false
29 let debug_prerr = if debug then prerr_endline else ignore
30
31 module HGT = Http_getter_types;;
32 module HG = Http_getter;;
33 module UM = UriManager;;
34
35 let cache_of_processed_baseuri = Hashtbl.create 1024
36
37 let one_step_depend suri =
38   let buri =
39     try
40       UM.buri_of_uri (UM.uri_of_string suri)
41     with UM.IllFormedUri _ -> suri
42   in
43   if Hashtbl.mem cache_of_processed_baseuri buri then 
44     []
45   else
46     begin
47       Hashtbl.add cache_of_processed_baseuri buri true;
48       let query = 
49         let buri = buri ^ "/" in 
50         let buri = HMysql.escape buri in
51         let obj_tbl = MetadataTypes.obj_tbl () in
52         sprintf 
53         ("SELECT source, h_occurrence FROM %s WHERE " ^^ 
54          "h_occurrence REGEXP '^%s[^/]*$'")
55             obj_tbl buri
56       in
57       try 
58         let rc = HMysql.exec (LibraryDb.instance ()) query in
59         let l = ref [] in
60         HMysql.iter rc (
61           fun row -> 
62             match row.(0), row.(1) with 
63             | Some uri, Some occ when Filename.dirname occ = buri -> 
64                 l := uri :: !l
65             | _ -> ());
66         let l = List.sort Pervasives.compare !l in
67         HExtlib.list_uniq l
68       with
69         exn -> raise exn (* no errors should be accepted *)
70     end
71     
72 let safe_buri_of_suri suri =
73   try
74     UM.buri_of_uri (UM.uri_of_string suri)
75   with
76     UM.IllFormedUri _ -> suri
77
78 let close_uri_list uri_to_remove =
79   (* to remove an uri you have to remove the whole script *)
80   let buri_to_remove = 
81     HExtlib.list_uniq 
82       (List.fast_sort Pervasives.compare 
83         (List.map safe_buri_of_suri uri_to_remove))
84   in
85   (* cleand the already visided baseuris *)
86   let buri_to_remove = 
87     List.filter 
88       (fun buri -> 
89         if Hashtbl.mem cache_of_processed_baseuri buri then false
90         else true)
91       buri_to_remove
92   in
93   (* now calculate the list of objects that belong to these baseuris *)
94   let uri_to_remove = 
95     try
96       List.fold_left 
97         (fun acc buri ->
98           let inhabitants = HG.ls (buri ^ "/") in
99           let inhabitants = List.filter 
100               (function HGT.Ls_object _ -> true | _ -> false) 
101             inhabitants
102           in
103           let inhabitants = List.map 
104               (function 
105                | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri 
106                | _ -> assert false)
107             inhabitants
108           in
109           inhabitants @ acc)
110       [] buri_to_remove 
111     with HGT.Invalid_URI u -> 
112       HLog.error ("We were listing an invalid buri: " ^ u);
113       exit 1
114   in
115   (* now we want the list of all uri that depend on them *) 
116   let depend = 
117     List.fold_left
118     (fun acc u -> one_step_depend u @ acc) [] uri_to_remove
119   in
120   let depend = 
121     HExtlib.list_uniq (List.fast_sort Pervasives.compare depend) 
122   in
123   uri_to_remove, depend
124
125 let rec close_using_db uris next =
126   match next with
127   | [] -> uris
128   | l -> let uris, next = close_uri_list l in close_using_db uris next @ uris
129   
130 let cleaned_no = ref 0;;
131
132   (** TODO repellent code ... *)
133 let moo_root_dir = lazy (
134   let url =
135     List.assoc "cic:/matita/"
136       (List.map
137         (fun pair ->
138           match
139             Str.split (Str.regexp "[ \t\r\n]+") (HExtlib.trim_blanks pair)
140           with
141           | [a;b] -> a, b
142           | _ -> assert false)
143         (Helm_registry.get_list Helm_registry.string "getter.prefix"))
144   in
145   String.sub url 7 (String.length url - 7)  (* remove heading "file:///" *)
146 )
147
148 let close_using_moos buris =
149   let rev_deps = Hashtbl.create 97 in
150   let all_moos =
151     HExtlib.find ~test:(fun name -> Filename.check_suffix name ".moo")
152       (Lazy.force moo_root_dir)
153   in
154   List.iter
155     (fun path -> 
156       let _, metadata = GrafiteMarshal.load_moo ~fname:path in
157       let baseuri_of_current_moo = 
158         let rec aux = function 
159           | [] -> assert false
160           | GrafiteAst.Baseuri buri::_ -> buri
161           | _ :: tl -> aux tl
162         in
163         aux metadata
164       in
165       let deps = 
166         HExtlib.filter_map 
167           (function 
168           | GrafiteAst.Dependency buri -> Some buri
169           | _ -> None ) 
170         metadata
171       in
172       List.iter 
173         (fun buri -> Hashtbl.add rev_deps buri baseuri_of_current_moo) deps)
174   all_moos;
175   let buris_to_remove = 
176     HExtlib.list_uniq  
177       (List.fast_sort Pervasives.compare 
178         (List.flatten (List.map (Hashtbl.find_all rev_deps) buris)))
179   in
180   let objects_to_remove = 
181     let objs_of_buri buri =
182       HExtlib.filter_map 
183         (function 
184         | Http_getter_types.Ls_object o ->
185             Some (buri ^ "/" ^ o.Http_getter_types.uri)
186         | _ -> None) 
187       (Http_getter.ls buri)
188     in
189     List.flatten (List.map objs_of_buri (buris @ buris_to_remove))
190   in
191   objects_to_remove
192
193 let clean_baseuris ?(verbose=true) ~basedir buris =
194   Hashtbl.clear cache_of_processed_baseuri;
195   let buris = List.map Http_getter_misc.strip_trailing_slash buris in
196   debug_prerr "clean_baseuris called on:";
197   if debug then
198     List.iter debug_prerr buris; 
199   let l = 
200     if Helm_registry.get_bool "db.nodb" then
201       close_using_moos buris
202     else
203       close_using_db [] buris 
204   in
205   let l = HExtlib.list_uniq (List.fast_sort Pervasives.compare l) in
206   let l = List.map UriManager.uri_of_string l in
207   debug_prerr "clean_baseuri will remove:";
208   if debug then
209     List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l; 
210   List.iter
211    (fun buri ->
212      HExtlib.safe_remove (LibraryMisc.obj_file_of_baseuri basedir buri)) 
213    (HExtlib.list_uniq (List.fast_sort Pervasives.compare
214      (List.map (UriManager.buri_of_uri) l)));
215   List.iter
216    (let last_baseuri = ref "" in
217     fun uri ->
218      let buri = UriManager.buri_of_uri uri in
219      if buri <> !last_baseuri then
220       begin
221        HLog.message ("Removing: " ^ buri ^ "/*");
222        last_baseuri := buri
223       end;
224      LibrarySync.remove_obj uri
225    ) l;
226   cleaned_no := !cleaned_no + List.length l;
227   if !cleaned_no > 30 then
228    begin
229     cleaned_no := 0;
230     List.iter
231      (function table ->
232        ignore (HMysql.exec (LibraryDb.instance ()) ("OPTIMIZE TABLE " ^ table)))
233      [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
234       MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
235       MetadataTypes.count_tbl()]
236    end