Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#6629] Improvement(jdbc-catalog): Optimize load database method in JdbcDatabaseOperations #6642

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ void create(String databaseName, String comment, Map<String, String> properties)
/** @return The list name of databases. */
List<String> listDatabases();

/**
* Checks if the specified database exists.
*
* @param databaseName The name of the database to check.
* @return true if the database exists; false otherwise.
*/
boolean exist(String databaseName);

/**
* @param databaseName The name of the database to check.
* @return information object of the JDBC database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ public List<String> listDatabases() {
}
}

@Override
public boolean exist(String databaseName) {
try (final Connection connection = this.dataSource.getConnection()) {
String query = generateDatabaseExistSql(databaseName);
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(query)) {
if (resultSet.next()) {
return true;
}
}
}
} catch (SQLException sqlException) {
throw this.exceptionMapper.toGravitinoException(sqlException);
}
return false;
}

protected String generateDatabaseExistSql(String databaseName) {
return String.format(
"SELECT * FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = '%s'", databaseName);
}

protected void dropDatabase(String databaseName, boolean cascade) {
try (final Connection connection = getConnection()) {
JdbcConnectorUtils.executeUpdate(connection, generateDropDatabaseSql(databaseName, cascade));
Expand Down Expand Up @@ -176,16 +198,11 @@ protected String generateDropDatabaseSql(String databaseName, boolean cascade) {
*/
@Override
public JdbcSchema load(String databaseName) throws NoSuchSchemaException {
List<String> allDatabases = listDatabases();
String dbName =
allDatabases.stream()
.filter(db -> db.equals(databaseName))
.findFirst()
.orElseThrow(
() -> new NoSuchSchemaException("Database %s could not be found", databaseName));

if (!exist(databaseName)) {
throw new NoSuchSchemaException("Database %s could not be found", databaseName);
}
return JdbcSchema.builder()
.withName(dbName)
.withName(databaseName)
.withProperties(ImmutableMap.of())
.withAuditInfo(AuditInfo.EMPTY)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public void create(String databaseName, String comment, Map<String, String> prop
} catch (SQLException e) {
throw exceptionMapper.toGravitinoException(e);
}
Preconditions.checkArgument(exist(databaseName), "Database %s does not exist", databaseName);
Preconditions.checkArgument(
this.exist(databaseName), "Database %s does not exist", databaseName);
}

@Override
public boolean exist(String databaseName) {
return new File(dbPath + "/" + databaseName).exists();
}
Expand All @@ -87,7 +89,7 @@ public List<String> listDatabases() {

@Override
public JdbcSchema load(String databaseName) throws NoSuchSchemaException {
if (exist(databaseName)) {
if (this.exist(databaseName)) {
return JdbcSchema.builder().withName(databaseName).withAuditInfo(AuditInfo.EMPTY).build();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ protected Connection getConnection() throws SQLException {
return connection;
}

@Override
protected String generateDatabaseExistSql(String databaseName) {
return String.format(
"SELECT n.datname FROM pg_catalog.pg_database n where n.datname='%s'", databaseName);
}

@Override
protected boolean supportSchemaComment() {
return true;
Expand Down