]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacleanLib.ml
An object should be removed from disk (i.e. from the Getter scope) _before_ removing it
[helm.git] / helm / matita / matitacleanLib.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 let debug = false
27 let debug_prerr = if debug then prerr_endline else ignore
28
29 module HGT = Http_getter_types;;
30 module HG = Http_getter;;
31 module HGM = Http_getter_misc;;
32 module UM = UriManager;;
33 module TA = GrafiteAst;;
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 = Mysql.escape buri in
51         let obj_tbl = MetadataTypes.obj_tbl () in
52         Printf.sprintf 
53           "SELECT source, h_occurrence FROM %s WHERE h_occurrence LIKE '%s%%'" obj_tbl buri
54       in
55       try 
56         let rc = Mysql.exec (MatitaDb.instance ()) query in
57         let l = ref [] in
58         Mysql.iter rc (
59           fun row -> 
60             match row.(0), row.(1) with 
61             | Some uri, Some occ when Filename.dirname occ = buri -> 
62                 l := uri :: !l
63             | _ -> ());
64         let l = List.sort Pervasives.compare !l in
65         MatitaMisc.list_uniq l
66       with
67         exn -> raise exn (* no errors should be accepted *)
68     end
69
70     
71 let safe_buri_of_suri suri =
72   try
73     UM.buri_of_uri (UM.uri_of_string suri)
74   with
75     UM.IllFormedUri _ -> suri
76
77 let close_uri_list uri_to_remove =
78   (* to remove an uri you have to remove the whole script *)
79   let buri_to_remove = 
80     MatitaMisc.list_uniq 
81       (List.fast_sort Pervasives.compare 
82         (List.map safe_buri_of_suri uri_to_remove))
83   in
84   (* cleand the already visided baseuris *)
85   let buri_to_remove = 
86     List.filter 
87       (fun buri -> 
88         if Hashtbl.mem cache_of_processed_baseuri buri then false
89         else true)
90       buri_to_remove
91   in
92   (* now calculate the list of objects that belong to these baseuris *)
93   let uri_to_remove = 
94     try
95       List.fold_left 
96         (fun acc buri ->
97           let inhabitants = HG.ls (buri ^ "/") in
98           let inhabitants = List.filter 
99               (function HGT.Ls_object _ -> true | _ -> false) 
100             inhabitants
101           in
102           let inhabitants = List.map 
103               (function 
104                | HGT.Ls_object e -> buri ^ "/" ^ e.HGT.uri 
105                | _ -> assert false)
106             inhabitants
107           in
108           inhabitants @ acc)
109       [] buri_to_remove 
110     with HGT.Invalid_URI u -> 
111       MatitaLog.error ("We were listing an invalid buri: " ^ u);
112       exit 1
113   in
114   (* now we want the list of all uri that depend on them *) 
115   let depend = 
116     List.fold_left
117     (fun acc u -> one_step_depend u @ acc) [] uri_to_remove
118   in
119   let depend = 
120     MatitaMisc.list_uniq 
121       (List.fast_sort Pervasives.compare depend) 
122   in
123   uri_to_remove, depend
124
125 let rec close uris next =
126   match next with
127   | [] -> uris
128   | l -> let uris, next = close_uri_list l in close uris next @ uris
129   
130 let cleaned_no = ref 0;;
131
132 let clean_baseuris ?(verbose=true) buris =
133   Hashtbl.clear cache_of_processed_baseuri;
134   let buris = List.map HGM.strip_trailing_slash buris in
135   debug_prerr "clean_baseuris called on:";
136   if debug then
137     List.iter debug_prerr buris; 
138   let l = close [] buris in
139   let l = MatitaMisc.list_uniq (List.fast_sort Pervasives.compare l) in
140   let l = List.map UriManager.uri_of_string l in
141   debug_prerr "clean_baseuri will remove:";
142   if debug then
143     List.iter (fun u -> debug_prerr (UriManager.string_of_uri u)) l; 
144   Hashtbl.iter
145    (fun buri _ ->
146      MatitaMisc.safe_remove (MatitaMisc.obj_file_of_baseuri buri)
147    ) cache_of_processed_baseuri;
148   List.iter (MatitaSync.remove ~verbose) l;
149   cleaned_no := !cleaned_no + List.length l;
150   if !cleaned_no > 30 then
151    List.iter
152     (function table ->
153       ignore (Mysql.exec (MatitaDb.instance ()) ("OPTIMIZE TABLE " ^ table)))
154     [MetadataTypes.name_tbl (); MetadataTypes.rel_tbl ();
155      MetadataTypes.sort_tbl (); MetadataTypes.obj_tbl();
156      MetadataTypes.count_tbl()]