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