Skip to content

Commit 71f0922

Browse files
committed
Optimize load database method in JdbcDatabaseOperations #6629
1 parent 392cdd5 commit 71f0922

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/operation/DatabaseOperation.java

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ void create(String databaseName, String comment, Map<String, String> properties)
5858
/** @return The list name of databases. */
5959
List<String> listDatabases();
6060

61+
/**
62+
* Checks if the specified database exists.
63+
*
64+
* @param databaseName The name of the database to check.
65+
* @return true if the database exists; false otherwise.
66+
*/
67+
boolean exist(String databaseName);
68+
6169
/**
6270
* @param databaseName The name of the database to check.
6371
* @return information object of the JDBC database.

catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/operation/JdbcDatabaseOperations.java

+26-9
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ public List<String> listDatabases() {
107107
}
108108
}
109109

110+
@Override
111+
public boolean exist(String databaseName) {
112+
try (final Connection connection = this.dataSource.getConnection()) {
113+
String query = generateDatabaseExistSql(databaseName);
114+
try (Statement statement = connection.createStatement()) {
115+
try (ResultSet resultSet = statement.executeQuery(query)) {
116+
if (resultSet.next()) {
117+
return true;
118+
}
119+
}
120+
}
121+
} catch (SQLException sqlException) {
122+
throw this.exceptionMapper.toGravitinoException(sqlException);
123+
}
124+
return false;
125+
}
126+
127+
protected String generateDatabaseExistSql(String databaseName) {
128+
return String.format(
129+
"SELECT * FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = '%s'", databaseName);
130+
}
131+
110132
protected void dropDatabase(String databaseName, boolean cascade) {
111133
try (final Connection connection = getConnection()) {
112134
JdbcConnectorUtils.executeUpdate(connection, generateDropDatabaseSql(databaseName, cascade));
@@ -176,16 +198,11 @@ protected String generateDropDatabaseSql(String databaseName, boolean cascade) {
176198
*/
177199
@Override
178200
public JdbcSchema load(String databaseName) throws NoSuchSchemaException {
179-
List<String> allDatabases = listDatabases();
180-
String dbName =
181-
allDatabases.stream()
182-
.filter(db -> db.equals(databaseName))
183-
.findFirst()
184-
.orElseThrow(
185-
() -> new NoSuchSchemaException("Database %s could not be found", databaseName));
186-
201+
if (!exist(databaseName)) {
202+
throw new NoSuchSchemaException("Database %s could not be found", databaseName);
203+
}
187204
return JdbcSchema.builder()
188-
.withName(dbName)
205+
.withName(databaseName)
189206
.withProperties(ImmutableMap.of())
190207
.withAuditInfo(AuditInfo.EMPTY)
191208
.build();

catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/operation/SqliteDatabaseOperations.java

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void create(String databaseName, String comment, Map<String, String> prop
6767
Preconditions.checkArgument(exist(databaseName), "Database %s does not exist", databaseName);
6868
}
6969

70+
@Override
7071
public boolean exist(String databaseName) {
7172
return new File(dbPath + "/" + databaseName).exists();
7273
}

catalogs/catalog-jdbc-postgresql/src/main/java/org/apache/gravitino/catalog/postgresql/operation/PostgreSqlSchemaOperations.java

+6
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ protected Connection getConnection() throws SQLException {
155155
return connection;
156156
}
157157

158+
@Override
159+
protected String generateDatabaseExistSql(String databaseName) {
160+
return String.format(
161+
"SELECT n.datname FROM pg_catalog.pg_database n where n.datname='%s'", databaseName);
162+
}
163+
158164
@Override
159165
protected boolean supportSchemaComment() {
160166
return true;

0 commit comments

Comments
 (0)