X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fmetadata%2FmetadataDb.ml;h=d82894ed6f67ba9287a727c12b64df98b9f07389;hb=aca103d3c3d740efcc0bcc2932922cff77facb49;hp=e7e60fafff64e823519d50bfba721fe51c91dc25;hpb=43f7741d0d6e714f0e1fd183d2fc43af267a445c;p=helm.git diff --git a/helm/ocaml/metadata/metadataDb.ml b/helm/ocaml/metadata/metadataDb.ml index e7e60faff..d82894ed6 100644 --- a/helm/ocaml/metadata/metadataDb.ml +++ b/helm/ocaml/metadata/metadataDb.ml @@ -27,105 +27,185 @@ open MetadataTypes open Printf -module DB = Dbi_mysql - -(* let baseuri = "http://www.cs.unibo.it/helm/schemas/schema-helm#" *) -let baseuri = "" -let inconcl_uri = baseuri ^ "InConclusion" -let mainconcl_uri = baseuri ^ "MainConclusion" -let mainhyp_uri = baseuri ^ "MainHypothesis" -let inhyp_uri = baseuri ^ "InHypothesis" -let inbody_uri = baseuri ^ "InBody" - -let prepare_insert (dbh: Dbi.connection) = - let insert_owner = - dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\")" owners_tbl) +let execute_insert dbd uri (sort_cols, rel_cols, obj_cols) = + let sort_tuples = + List.fold_left (fun s l -> match l with + | [`String a; `String b; `Int c; `String d] -> + sprintf "(\"%s\", \"%s\", %d, \"%s\")" a b c d :: s + | _ -> assert false ) + [] sort_cols in - let insert_sort = - dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",?,\"?\")" sort_tbl) + let rel_tuples = + List.fold_left (fun s l -> match l with + | [`String a; `String b; `Int c] -> + sprintf "(\"%s\", \"%s\", %d)" a b c :: s + | _ -> assert false) + [] rel_cols in - let insert_rel = - dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",?)" rel_tbl) + let obj_tuples = List.fold_left (fun s l -> match l with + | [`String a; `String b; `String c; `Int d] -> + sprintf "(\"%s\", \"%s\", \"%s\", %d)" a b c d :: s + | [`String a; `String b; `String c; `Null] -> + sprintf "(\"%s\", \"%s\", \"%s\", %s)" a b c "NULL" :: s + | _ -> assert false) + [] obj_cols in - let insert_obj = - dbh#prepare (sprintf "INSERT INTO %s VALUES (\"?\",\"?\",\"?\",?)" obj_tbl) - in - (insert_owner, insert_sort, insert_rel, insert_obj) - -let execute_insert dbh (insert_owner, insert_sort, insert_rel, insert_obj) - uri owner (sort_cols, rel_cols, obj_cols) -= - insert_owner#execute [`String uri; `String owner]; - List.iter insert_sort#execute sort_cols; - List.iter insert_rel#execute rel_cols; - List.iter insert_obj#execute obj_cols - -let insert_const_no dbh uri = + if sort_tuples <> [] then + begin + let query_sort = + sprintf "INSERT %s VALUES %s;" (sort_tbl ()) (String.concat "," sort_tuples) + in + ignore (Mysql.exec dbd query_sort) + end; + if rel_tuples <> [] then + begin + let query_rel = + sprintf "INSERT %s VALUES %s;" (rel_tbl ()) (String.concat "," rel_tuples) + in + ignore (Mysql.exec dbd query_rel) + end; + if obj_tuples <> [] then + begin + let query_obj = + sprintf "INSERT %s VALUES %s;" (obj_tbl ()) (String.concat "," obj_tuples) + in + ignore (Mysql.exec dbd query_obj) + end + + +let count_distinct position l = + MetadataConstraints.StringSet.cardinal + (List.fold_left (fun acc d -> + match position with + | `Conclusion -> + (match d with + | `Obj (name,`InConclusion) + | `Obj (name,`MainConclusion _ ) -> + MetadataConstraints.StringSet.add name acc + | _ -> acc) + | `Hypothesis -> + (match d with + | `Obj (name,`InHypothesis) + | `Obj (name,`MainHypothesis _) -> + MetadataConstraints.StringSet.add name acc + | _ -> acc) + | `Statement -> + (match d with + | `Obj (name,`InBody) -> acc + | `Obj (name,_) -> MetadataConstraints.StringSet.add name acc + | _ -> acc) + ) MetadataConstraints.StringSet.empty l) +(* +let insert_const_no dbd uri = + let term = CicUtil.term_of_uri uri in + let ty = CicTypeChecker.type_of_aux' let inconcl_no = - sprintf "INSERT INTO %s SELECT \"%s\", COUNT(DISTINCT h_occurrence) FROM %s WHERE (h_position=\"%s\" OR h_position=\"%s\") AND source LIKE \"%s%%\"" - conclno_tbl uri obj_tbl inconcl_uri mainconcl_uri uri + sprintf "INSERT %s SELECT \"%s\", COUNT(DISTINCT h_occurrence) FROM %s WHERE (h_position=\"%s\" OR h_position=\"%s\") AND source LIKE \"%s%%\"" + (conclno_tbl ()) uri (obj_tbl ()) inconcl_pos mainconcl_pos uri in let concl_hyp = - sprintf "INSERT INTO %s + sprintf "INSERT %s SELECT \"%s\",COUNT(DISTINCT h_occurrence) FROM %s WHERE NOT (h_position=\"%s\") AND (source = \"%s\")" - conclno_hyp_tbl uri obj_tbl inbody_uri uri + (fullno_tbl ()) uri (obj_tbl ()) inbody_pos uri in - (dbh#prepare inconcl_no)#execute []; - (dbh#prepare concl_hyp)#execute [] - -let insert_name ~dbh ~uri ~name = + ignore (Mysql.exec dbd inconcl_no); + ignore (Mysql.exec dbd concl_hyp) +*) +let insert_const_no dbd (uri,metadata) = + let no_concl = count_distinct `Conclusion metadata in + let no_hyp = count_distinct `Hypothesis metadata in + let no_full = count_distinct `Statement metadata in + let insert = + sprintf "INSERT INTO %s VALUES (\"%s\", %d, %d, %d)" + (count_tbl ()) uri no_concl no_hyp no_full + in + ignore (Mysql.exec dbd insert) + +let insert_name ~dbd ~uri ~name = let query = - dbh#prepare - (sprintf "INSERT INTO %s VALUES (\"%s\", \"%s\")" name_tbl uri name) + sprintf "INSERT %s VALUES (\"%s\", \"%s\")" (name_tbl ()) uri name in - query#execute [] + ignore (Mysql.exec dbd query) -type dbi_columns = - Dbi.sql_t list list * Dbi.sql_t list list * Dbi.sql_t list list +type columns = + MetadataPp.t list list * MetadataPp.t list list * MetadataPp.t list list -let index_constant ~dbh = - let query = prepare_insert dbh in - fun ~owner ~uri ~body ~ty -> - let name = UriManager.name_of_uri uri in - let uri = UriManager.string_of_uri uri in - let metadata = MetadataExtractor.compute ~body ~ty in - let columns = MetadataPp.columns_of_metadata ~about:uri metadata in - execute_insert dbh query uri owner (columns :> dbi_columns); - insert_const_no dbh uri; - insert_name ~dbh ~uri ~name + (* TODO ZACK: verify if an object has already been indexed *) +let already_indexed _ = false +(* +let index_constant ~dbd = + let query = prepare_insert () in + fun ~uri ~body ~ty -> + if not (already_indexed uri) then begin + let name = UriManager.name_of_uri uri in + let uri = UriManager.string_of_uri uri in + let metadata = MetadataExtractor.compute ~body ~ty in + let columns = MetadataPp.columns_of_metadata ~about:uri metadata in + execute_insert dbd query uri (columns :> columns); + insert_const_no dbd uri; + insert_name ~dbd ~uri ~name + end -let index_inductive_def ~dbh = - let query = prepare_insert dbh in - fun ~owner ~uri ~types -> - let metadata = MetadataExtractor.compute_ind ~uri ~types in - let uri_of (a,b,c) = a in - let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in +let index_inductive_def ~dbd = + let query = prepare_insert () in + fun ~uri ~types -> + if not (already_indexed uri) then begin + let metadata = MetadataExtractor.compute_obj uri in + let uri_of (a,b,c) = a in + let uris = UriManager.string_of_uri uri :: List.map uri_of metadata in + let uri = UriManager.string_of_uri uri in + let columns = MetadataPp.columns_of_ind_metadata metadata in + execute_insert dbd query uri (columns :> columns); + List.iter (insert_const_no dbd) uris; + List.iter (fun (uri, name, _) -> insert_name ~dbd ~uri ~name) metadata + end +*) +let index_obj ~dbd ~uri = + if not (already_indexed uri) then begin + let metadata = MetadataExtractor.compute_obj uri in + let uri_of (a,b,c) = (a,c) in let uri = UriManager.string_of_uri uri in - let columns = MetadataPp.columns_of_ind_metadata metadata in - execute_insert dbh query uri owner (columns :> dbi_columns); - List.iter (insert_const_no dbh) uris; - List.iter (fun (uri, name, _) -> insert_name ~dbh ~uri ~name) metadata + let columns = MetadataPp.columns_of_metadata metadata in + execute_insert dbd uri (columns :> columns); + List.iter (insert_const_no dbd) (List.map uri_of metadata); + List.iter (fun (uri, name, _) -> insert_name ~dbd ~uri ~name) metadata + end + + +let tables_to_clean = + [sort_tbl; rel_tbl; obj_tbl; name_tbl; count_tbl] -let clean ~(dbh: Dbi.connection) ~owner = +let clean ~(dbd:Mysql.dbd) = let owned_uris = (* list of uris in list-of-columns format *) - let query = - dbh#prepare (sprintf - "SELECT source FROM %s WHERE owner = \"%s\"" owners_tbl owner) + let query = sprintf "SELECT source FROM %s" (name_tbl ()) in + let result = Mysql.exec dbd query in + let uris = Mysql.map result (fun cols -> + match cols.(0) with + | Some src -> src + | None -> assert false) in + (* and now some stuff to remove #xpointers and duplicates *) + uris + in + let del_from tbl = + let query s = + sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) s in - query#execute []; - List.map - (function [`String source_col] -> source_col | _ -> assert false) - (query#fetchall ()) + List.iter + (fun source_col -> ignore (Mysql.exec dbd (query source_col))) + owned_uris in + List.iter del_from tables_to_clean; + owned_uris + +let unindex ~dbd ~uri = + let uri = UriManager.string_of_uri uri in let del_from tbl = - let query = - dbh#prepare (sprintf "DELETE FROM %s WHERE source LIKE \"?%%\"" tbl) + let query tbl = + sprintf "DELETE FROM %s WHERE source LIKE \"%s%%\"" (tbl ()) uri in - List.iter (fun source_col -> query#execute [`String source_col]) owned_uris + ignore (Mysql.exec dbd (query tbl)) in - List.iter del_from - [sort_tbl; rel_tbl; obj_tbl; conclno_tbl; conclno_hyp_tbl; name_tbl; - owners_tbl] + List.iter del_from tables_to_clean