]> matita.cs.unibo.it Git - helm.git/blob - components/metadata/metadataDb.ml
tagged 0.5.0-rc1
[helm.git] / components / metadata / metadataDb.ml
1 (* Copyright (C) 2004, 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 MetadataTypes
29
30 open Printf
31
32 let format_insert dbtype dbd tbl tuples = 
33      if HSql.isMysql dbtype dbd then 
34         [sprintf "INSERT %s VALUES %s;" tbl (String.concat "," tuples)]
35      else
36         List.map (fun tup -> 
37                 sprintf "INSERT INTO %s VALUES %s;" tbl tup) tuples 
38 ;;
39
40 let execute_insert dbd uri (sort_cols, rel_cols, obj_cols) =
41   let sort_tuples = 
42     List.fold_left (fun s l -> match l with
43       | [`String a; `String b; `Int c; `String d] -> 
44           sprintf "(\"%s\", \"%s\", %d, \"%s\")" a b c d :: s
45       | _ -> assert false )
46     [] sort_cols
47   in
48   let rel_tuples =
49     List.fold_left (fun s l -> match l with
50       | [`String a; `String b; `Int c] ->
51           sprintf "(\"%s\", \"%s\", %d)" a b c :: s
52       | _ -> assert false)
53     [] rel_cols  
54   in
55   let obj_tuples = List.fold_left (fun s l -> match l with
56       | [`String a; `String b; `String c; `Int d] ->
57           sprintf "(\"%s\", \"%s\", \"%s\", %d)" a b c d :: s
58       | [`String a; `String b; `String c; `Null] ->
59           sprintf "(\"%s\", \"%s\", \"%s\", %s)" a b c "NULL" :: s
60       | _ -> assert false)
61     [] obj_cols
62   in
63   let dbtype = 
64     if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
65   in
66   if sort_tuples <> [] then
67     begin
68     let query_sort = 
69      format_insert dbtype dbd (sort_tbl ())  sort_tuples 
70     in
71     List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_sort
72     end;
73   if rel_tuples <> [] then
74     begin
75     let query_rel = 
76      format_insert dbtype dbd (rel_tbl ())  rel_tuples 
77     in
78     List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_rel
79     end;
80   if obj_tuples <> [] then
81     begin
82     let query_obj = 
83      format_insert dbtype dbd (obj_tbl ())  obj_tuples 
84     in
85     List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_obj
86     end
87   
88     
89 let count_distinct position l =
90   MetadataConstraints.UriManagerSet.cardinal
91   (List.fold_left (fun acc d -> 
92     match position with
93     | `Conclusion -> 
94          (match d with
95          | `Obj (name,`InConclusion) 
96          | `Obj (name,`MainConclusion _ ) -> 
97              MetadataConstraints.UriManagerSet.add name acc
98          | _ -> acc)
99     | `Hypothesis ->
100         (match d with
101         | `Obj (name,`InHypothesis) 
102         | `Obj (name,`MainHypothesis _) -> 
103             MetadataConstraints.UriManagerSet.add name acc
104         | _ -> acc)
105     | `Statement ->
106         (match d with
107         | `Obj (name,`InBody) -> acc
108         | `Obj (name,_) -> MetadataConstraints.UriManagerSet.add name acc
109         | _ -> acc)
110     ) MetadataConstraints.UriManagerSet.empty l)
111
112 let insert_const_no ~dbd l =
113  let data =
114   List.fold_left
115    (fun acc (uri,_,metadata) -> 
116      let no_concl = count_distinct `Conclusion metadata in
117      let no_hyp = count_distinct `Hypothesis metadata in
118      let no_full = count_distinct `Statement metadata in
119       (sprintf "(\"%s\", %d, %d, %d)" 
120        (UriManager.string_of_uri uri) no_concl no_hyp no_full) :: acc
121    ) [] l in
122  let dbtype = 
123    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
124  in
125  let insert =
126   format_insert dbtype dbd (count_tbl ())  data
127  in
128   List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) insert
129   
130 let insert_name ~dbd l =
131  let dbtype =
132    if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
133  in
134  let data =
135   List.fold_left
136    (fun acc (uri,name,_) -> 
137       (sprintf "(\"%s\", \"%s\")" (UriManager.string_of_uri uri) name) :: acc
138    ) [] l in
139  let insert =
140    format_insert dbtype dbd (name_tbl ())  data
141  in
142   List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) insert
143
144 type columns =
145   MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list
146
147   (* TODO ZACK: verify if an object has already been indexed *)
148 let already_indexed _ = false
149
150 (***** TENTATIVE HACK FOR THE DB SLOWDOWN - BEGIN *******)
151 let analyze_index = ref 0
152 let eventually_analyze dbd =
153   incr analyze_index;
154   if !analyze_index > 30 then
155     if  HSql.isMysql HSql.User dbd then
156     begin
157       let analyze t = "OPTIMIZE TABLE " ^ t ^ ";" in
158       List.iter 
159         (fun table -> ignore (HSql.exec HSql.User dbd (analyze table)))
160         [name_tbl (); rel_tbl (); sort_tbl (); obj_tbl(); count_tbl()]
161     end
162   
163 (***** TENTATIVE HACK FOR THE DB SLOWDOWN - END *******)
164
165 let index_obj ~dbd ~uri = 
166   if not (already_indexed uri) then begin
167     eventually_analyze dbd;
168     let metadata = MetadataExtractor.compute_obj uri in
169     let uri = UriManager.string_of_uri uri in
170     let columns = MetadataPp.columns_of_metadata metadata in
171     execute_insert dbd uri (columns :> columns);
172     insert_const_no ~dbd metadata;
173     insert_name ~dbd metadata
174   end
175   
176
177 let tables_to_clean =
178   [sort_tbl; rel_tbl; obj_tbl; name_tbl; count_tbl]
179
180 let clean ~(dbd:HSql.dbd) =
181   let owned_uris =  (* list of uris in list-of-columns format *)
182     let query = sprintf "SELECT source FROM %s" (name_tbl ()) in
183     let result = HSql.exec HSql.User dbd query in
184     let uris = HSql.map result (fun cols ->
185       match cols.(0) with
186       | Some src -> src
187       | None -> assert false) in
188     (* and now some stuff to remove #xpointers and duplicates *)
189     uris
190   in
191   let del_from tbl =
192     let escape s =
193       Pcre.replace ~pat:"([^\\\\])_" ~templ:"$1\\_" (HSql.escape HSql.User dbd s)
194     in
195     let query s = 
196       sprintf
197        ("DELETE FROM %s WHERE source LIKE \"%s%%\" " ^^
198         HSql.escape_string_for_like HSql.User dbd)
199         (tbl ()) (escape s)
200     in
201     List.iter
202       (fun source_col -> ignore (HSql.exec HSql.User dbd (query source_col)))
203       owned_uris
204   in
205   List.iter del_from tables_to_clean;
206   owned_uris
207
208 let unindex ~dbd ~uri =
209   let uri = UriManager.string_of_uri uri in
210   let del_from tbl =
211     let escape s =
212       Pcre.replace 
213         ~pat:"([^\\\\])_" ~templ:"$1\\_" (HSql.escape HSql.User dbd s)
214     in
215     let query tbl =
216       sprintf
217        ("DELETE FROM %s WHERE source LIKE \"%s%%\" " ^^
218         HSql.escape_string_for_like HSql.User dbd)
219        (tbl ()) (escape uri)
220     in
221     ignore (HSql.exec HSql.User dbd (query tbl))
222   in
223   List.iter del_from tables_to_clean
224