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/
30 let execute_insert dbd uri (sort_cols, rel_cols, obj_cols) =
32 List.fold_left (fun s l -> match l with
33 | [`String a; `String b; `Int c; `String d] ->
34 sprintf "(\"%s\", \"%s\", %d, \"%s\")" a b c d :: s
39 List.fold_left (fun s l -> match l with
40 | [`String a; `String b; `Int c] ->
41 sprintf "(\"%s\", \"%s\", %d)" a b c :: s
45 let obj_tuples = List.fold_left (fun s l -> match l with
46 | [`String a; `String b; `String c; `Int d] ->
47 sprintf "(\"%s\", \"%s\", \"%s\", %d)" a b c d :: s
48 | [`String a; `String b; `String c; `Null] ->
49 sprintf "(\"%s\", \"%s\", \"%s\", %s)" a b c "NULL" :: s
53 if sort_tuples <> [] then
56 sprintf "INSERT %s VALUES %s;" (sort_tbl ()) (String.concat "," sort_tuples)
58 ignore (Mysql.exec dbd query_sort)
60 if rel_tuples <> [] then
63 sprintf "INSERT %s VALUES %s;" (rel_tbl ()) (String.concat "," rel_tuples)
65 ignore (Mysql.exec dbd query_rel)
67 if obj_tuples <> [] then
70 sprintf "INSERT %s VALUES %s;" (obj_tbl ()) (String.concat "," obj_tuples)
72 ignore (Mysql.exec dbd query_obj)
76 let count_distinct position l =
77 MetadataConstraints.StringSet.cardinal
78 (List.fold_left (fun acc d ->
82 | `Obj (name,`InConclusion)
83 | `Obj (name,`MainConclusion _ ) ->
84 MetadataConstraints.StringSet.add name acc
88 | `Obj (name,`InHypothesis)
89 | `Obj (name,`MainHypothesis _) ->
90 MetadataConstraints.StringSet.add name acc
94 | `Obj (name,`InBody) -> acc
95 | `Obj (name,_) -> MetadataConstraints.StringSet.add name acc
97 ) MetadataConstraints.StringSet.empty l)
99 let insert_const_no dbd uri =
100 let term = CicUtil.term_of_uri uri in
101 let ty = CicTypeChecker.type_of_aux'
103 sprintf "INSERT %s SELECT \"%s\", COUNT(DISTINCT h_occurrence) FROM %s WHERE (h_position=\"%s\" OR h_position=\"%s\") AND source LIKE \"%s%%\""
104 (conclno_tbl ()) uri (obj_tbl ()) inconcl_pos mainconcl_pos uri
108 SELECT \"%s\",COUNT(DISTINCT h_occurrence)
110 WHERE NOT (h_position=\"%s\") AND (source = \"%s\")"
111 (fullno_tbl ()) uri (obj_tbl ()) inbody_pos uri
113 ignore (Mysql.exec dbd inconcl_no);
114 ignore (Mysql.exec dbd concl_hyp)
116 let insert_const_no dbd (uri,metadata) =
117 let no_concl = count_distinct `Conclusion metadata in
118 let no_hyp = count_distinct `Hypothesis metadata in
119 let no_full = count_distinct `Statement metadata in
121 sprintf "INSERT INTO %s VALUES (\"%s\", %d, %d, %d)"
122 (count_tbl ()) uri no_concl no_hyp no_full
124 ignore (Mysql.exec dbd insert)
126 let insert_name ~dbd ~uri ~name =
128 sprintf "INSERT %s VALUES (\"%s\", \"%s\")" (name_tbl ()) uri name
130 ignore (Mysql.exec dbd query)
133 MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list
135 (* TODO ZACK: verify if an object has already been indexed *)
136 let already_indexed _ = false
138 let index_constant ~dbd =
139 let query = prepare_insert () in
140 fun ~uri ~body ~ty ->
141 if not (already_indexed uri) then begin
142 let name = UriManager.name_of_uri uri in
143 let uri = UriManager.string_of_uri uri in
144 let metadata = MetadataExtractor.compute ~body ~ty in
145 let columns = MetadataPp.columns_of_metadata ~about:uri metadata in
146 execute_insert dbd query uri (columns :> columns);
147 insert_const_no dbd uri;
148 insert_name ~dbd ~uri ~name
151 let index_inductive_def ~dbd =
152 let query = prepare_insert () in
154 if not (already_indexed uri) then begin
155 let metadata = MetadataExtractor.compute_obj uri in
156 let uri_of (a,b,c) = a in
157 let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in
158 let uri = UriManager.string_of_uri uri in
159 let columns = MetadataPp.columns_of_ind_metadata metadata in
160 execute_insert dbd query uri (columns :> columns);
161 List.iter (insert_const_no dbd) uris;
162 List.iter (fun (uri, name, _) -> insert_name ~dbd ~uri ~name) metadata
165 let index_obj ~dbd ~uri =
166 if not (already_indexed uri) then begin
167 let metadata = MetadataExtractor.compute_obj uri in
168 let uri_of (a,b,c) = (a,c) 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 List.iter (insert_const_no dbd) (List.map uri_of metadata);
173 List.iter (fun (uri, name, _) -> insert_name ~dbd ~uri ~name) metadata
177 let tables_to_clean =
178 [sort_tbl; rel_tbl; obj_tbl; name_tbl; count_tbl]
180 let clean ~(dbd:Mysql.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 = Mysql.exec dbd query in
184 let uris = Mysql.map result (fun cols ->
187 | None -> assert false) in
188 (* and now some stuff to remove #xpointers and duplicates *)
193 sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) s
196 (fun source_col -> ignore (Mysql.exec dbd (query source_col)))
199 List.iter del_from tables_to_clean;
202 let unindex ~dbd ~uri =
203 let uri = UriManager.string_of_uri uri in
206 sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) uri
208 ignore (Mysql.exec dbd (query tbl))
210 List.iter del_from tables_to_clean