1 (* Copyright (C) 2004, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
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)]
37 sprintf "INSERT INTO %s VALUES %s;" tbl tup) tuples
40 let execute_insert dbd uri (sort_cols, rel_cols, obj_cols) =
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
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
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
64 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
66 if sort_tuples <> [] then
69 format_insert dbtype dbd (sort_tbl ()) sort_tuples
71 List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_sort
73 if rel_tuples <> [] then
76 format_insert dbtype dbd (rel_tbl ()) rel_tuples
78 List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_rel
80 if obj_tuples <> [] then
83 format_insert dbtype dbd (obj_tbl ()) obj_tuples
85 List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) query_obj
89 let count_distinct position l =
90 MetadataConstraints.UriManagerSet.cardinal
91 (List.fold_left (fun acc d ->
95 | `Obj (name,`InConclusion)
96 | `Obj (name,`MainConclusion _ ) ->
97 MetadataConstraints.UriManagerSet.add name acc
101 | `Obj (name,`InHypothesis)
102 | `Obj (name,`MainHypothesis _) ->
103 MetadataConstraints.UriManagerSet.add name acc
107 | `Obj (name,`InBody) -> acc
108 | `Obj (name,_) -> MetadataConstraints.UriManagerSet.add name acc
110 ) MetadataConstraints.UriManagerSet.empty l)
112 let insert_const_no ~dbd l =
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
123 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
126 format_insert dbtype dbd (count_tbl ()) data
128 List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) insert
130 let insert_name ~dbd l =
132 if Helm_registry.get_bool "matita.system" then HSql.Library else HSql.User
136 (fun acc (uri,name,_) ->
137 (sprintf "(\"%s\", \"%s\")" (UriManager.string_of_uri uri) name) :: acc
140 format_insert dbtype dbd (name_tbl ()) data
142 List.iter (fun query -> ignore (HSql.exec dbtype dbd query)) insert
145 MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list
147 (* TODO ZACK: verify if an object has already been indexed *)
148 let already_indexed _ = false
150 (***** TENTATIVE HACK FOR THE DB SLOWDOWN - BEGIN *******)
151 let analyze_index = ref 0
152 let eventually_analyze dbd =
154 if !analyze_index > 30 then
155 if HSql.isMysql HSql.User dbd then
157 let analyze t = "OPTIMIZE TABLE " ^ t ^ ";" in
159 (fun table -> ignore (HSql.exec HSql.User dbd (analyze table)))
160 [name_tbl (); rel_tbl (); sort_tbl (); obj_tbl(); count_tbl()]
163 (***** TENTATIVE HACK FOR THE DB SLOWDOWN - END *******)
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
177 let tables_to_clean =
178 [sort_tbl; rel_tbl; obj_tbl; name_tbl; count_tbl]
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 ->
187 | None -> assert false) in
188 (* and now some stuff to remove #xpointers and duplicates *)
193 Pcre.replace ~pat:"([^\\\\])_" ~templ:"$1\\_" (HSql.escape HSql.User dbd s)
197 ("DELETE FROM %s WHERE source LIKE \"%s%%\" " ^^
198 HSql.escape_string_for_like HSql.User dbd)
202 (fun source_col -> ignore (HSql.exec HSql.User dbd (query source_col)))
205 List.iter del_from tables_to_clean;
208 let unindex ~dbd ~uri =
209 let uri = UriManager.string_of_uri uri in
213 ~pat:"([^\\\\])_" ~templ:"$1\\_" (HSql.escape HSql.User dbd s)
217 ("DELETE FROM %s WHERE source LIKE \"%s%%\" " ^^
218 HSql.escape_string_for_like HSql.User dbd)
219 (tbl ()) (escape uri)
221 ignore (HSql.exec HSql.User dbd (query tbl))
223 List.iter del_from tables_to_clean