]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/metadata/metadataDb.ml
6358df5b04718f634defe679c0af44cb8cfcc04d
[helm.git] / helm / software / 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 tbl tuples = 
33      if HSql.isMysql 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   if sort_tuples <> [] then
64     begin
65     let query_sort = 
66      format_insert(sort_tbl ())  sort_tuples 
67     in
68     List.iter (fun query -> ignore (HSql.exec dbd query)) query_sort
69     end;
70   if rel_tuples <> [] then
71     begin
72     let query_rel = 
73      format_insert(rel_tbl ())  rel_tuples 
74     in
75     List.iter (fun query -> ignore (HSql.exec dbd query)) query_rel
76     end;
77   if obj_tuples <> [] then
78     begin
79     let query_obj = 
80      format_insert(obj_tbl ())  obj_tuples 
81     in
82     List.iter (fun query -> ignore (HSql.exec dbd query)) query_obj
83     end
84   
85     
86 let count_distinct position l =
87   MetadataConstraints.UriManagerSet.cardinal
88   (List.fold_left (fun acc d -> 
89     match position with
90     | `Conclusion -> 
91          (match d with
92          | `Obj (name,`InConclusion) 
93          | `Obj (name,`MainConclusion _ ) -> 
94              MetadataConstraints.UriManagerSet.add name acc
95          | _ -> acc)
96     | `Hypothesis ->
97         (match d with
98         | `Obj (name,`InHypothesis) 
99         | `Obj (name,`MainHypothesis _) -> 
100             MetadataConstraints.UriManagerSet.add name acc
101         | _ -> acc)
102     | `Statement ->
103         (match d with
104         | `Obj (name,`InBody) -> acc
105         | `Obj (name,_) -> MetadataConstraints.UriManagerSet.add name acc
106         | _ -> acc)
107     ) MetadataConstraints.UriManagerSet.empty l)
108
109 let insert_const_no ~dbd l =
110  let data =
111   List.fold_left
112    (fun acc (uri,_,metadata) -> 
113      let no_concl = count_distinct `Conclusion metadata in
114      let no_hyp = count_distinct `Hypothesis metadata in
115      let no_full = count_distinct `Statement metadata in
116       (sprintf "(\"%s\", %d, %d, %d)" 
117        (UriManager.string_of_uri uri) no_concl no_hyp no_full) :: acc
118    ) [] l in
119  let insert =
120   format_insert(count_tbl ())  data
121  in
122   List.iter (fun query -> ignore (HSql.exec dbd query)) insert
123   
124 let insert_name ~dbd l =
125  let data =
126   List.fold_left
127    (fun acc (uri,name,_) -> 
128       (sprintf "(\"%s\", \"%s\")" (UriManager.string_of_uri uri) name) :: acc
129    ) [] l in
130  let insert =
131    format_insert(name_tbl ())  data
132  in
133   List.iter (fun query -> ignore (HSql.exec dbd query)) insert
134
135 type columns =
136   MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list
137
138   (* TODO ZACK: verify if an object has already been indexed *)
139 let already_indexed _ = false
140
141 (***** TENTATIVE HACK FOR THE DB SLOWDOWN - BEGIN *******)
142 let analyze_index = ref 0
143 let eventually_analyze dbd =
144   incr analyze_index;
145   if !analyze_index > 30 then
146     if  HSql.isMysql then
147     begin
148       let analyze t = "OPTIMIZE TABLE " ^ t ^ ";" in
149       List.iter 
150         (fun table -> ignore (HSql.exec dbd (analyze table)))
151         [name_tbl (); rel_tbl (); sort_tbl (); obj_tbl(); count_tbl()]
152     end
153   
154 (***** TENTATIVE HACK FOR THE DB SLOWDOWN - END *******)
155
156 let index_obj ~dbd ~uri = 
157   if not (already_indexed uri) then begin
158     eventually_analyze dbd;
159     let metadata = MetadataExtractor.compute_obj uri in
160     let uri = UriManager.string_of_uri uri in
161     let columns = MetadataPp.columns_of_metadata metadata in
162     execute_insert dbd uri (columns :> columns);
163     insert_const_no ~dbd metadata;
164     insert_name ~dbd metadata
165   end
166   
167
168 let tables_to_clean =
169   [sort_tbl; rel_tbl; obj_tbl; name_tbl; count_tbl]
170
171 let clean ~(dbd:HSql.dbd) =
172   let owned_uris =  (* list of uris in list-of-columns format *)
173     let query = sprintf "SELECT source FROM %s" (name_tbl ()) in
174     let result = HSql.exec dbd query in
175     let uris = HSql.map result (fun cols ->
176       match cols.(0) with
177       | Some src -> src
178       | None -> assert false) in
179     (* and now some stuff to remove #xpointers and duplicates *)
180     uris
181   in
182   let del_from tbl =
183     let query s = 
184       sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) s 
185     in
186     List.iter
187       (fun source_col -> ignore (HSql.exec dbd (query source_col)))
188       owned_uris
189   in
190   List.iter del_from tables_to_clean;
191   owned_uris
192
193 let unindex ~dbd ~uri =
194   let uri = UriManager.string_of_uri uri in
195   let del_from tbl =
196     let query tbl =
197       sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) uri
198     in
199     ignore (HSql.exec dbd (query tbl))
200   in
201   List.iter del_from tables_to_clean
202