Skip to content

Commit ef3707a

Browse files
committed
Improved speed for DatabaseMetaData
1 parent 8223ade commit ef3707a

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

jdbc/src/main/java/tech/ydb/jdbc/impl/YdbDatabaseMetaDataImpl.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.Collections;
1010
import java.util.Comparator;
1111
import java.util.HashSet;
12-
import java.util.LinkedHashMap;
1312
import java.util.List;
1413
import java.util.Map;
1514
import java.util.Objects;
@@ -24,13 +23,15 @@
2423

2524
import com.google.common.base.Strings;
2625

26+
import tech.ydb.core.StatusCode;
2727
import tech.ydb.jdbc.YdbConnection;
2828
import tech.ydb.jdbc.YdbConst;
2929
import tech.ydb.jdbc.YdbDatabaseMetaData;
3030
import tech.ydb.jdbc.YdbDriverInfo;
3131
import tech.ydb.jdbc.YdbTypes;
3232
import tech.ydb.jdbc.common.YdbFunctions;
3333
import tech.ydb.jdbc.context.YdbExecutor;
34+
import tech.ydb.jdbc.exception.YdbExecutionStatusException;
3435
import tech.ydb.proto.scheme.SchemeOperationProtos;
3536
import tech.ydb.scheme.SchemeClient;
3637
import tech.ydb.scheme.description.ListDirectoryResult;
@@ -764,7 +765,9 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
764765
}
765766

766767
Predicate<String> columnFilter = equalsFilter(columnNamePattern);
767-
return fromTables(tableNamePattern,
768+
Collection<String> tables = listTables(tableNamePattern);
769+
770+
return fromTables(tables,
768771
(tableName, tableDesc, rows) -> {
769772
short index = 0;
770773
for (TableColumn column : tableDesc.getColumns()) {
@@ -849,7 +852,7 @@ public ResultSet getBestRowIdentifier(String catalog, String schema, String tabl
849852
}
850853

851854
// Only primary keys could be used as row identifiers
852-
return fromTables(table,
855+
return fromTables(Collections.singletonList(table),
853856
(tableName, tableDesc, rows) -> {
854857
Map<String, TableColumn> columnMap = tableDesc.getColumns().stream()
855858
.collect(Collectors.toMap(TableColumn::getName, Function.identity()));
@@ -899,7 +902,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr
899902
return fromEmptyResultSet();
900903
}
901904

902-
return fromTables(table,
905+
return fromTables(Collections.singletonList(table),
903906
(tableName, tableDesc, rows) -> {
904907
short index = 0;
905908
for (String key : tableDesc.getPrimaryKeys()) {
@@ -1039,7 +1042,7 @@ public ResultSet getIndexInfo(String catalog, String schema, String table, boole
10391042
return fromEmptyResultSet();
10401043
}
10411044

1042-
return fromTables(table,
1045+
return fromTables(Collections.singletonList(table),
10431046
(tableName, tableDesc, rows) -> {
10441047
for (TableIndex tableIndex : tableDesc.getIndexes()) {
10451048
short index = 0;
@@ -1281,21 +1284,6 @@ public boolean isWrapperFor(Class<?> iface) {
12811284
return iface.isAssignableFrom(getClass());
12821285
}
12831286

1284-
private Map<String, TableDescription> collectTableDescriptions(Session session, Collection<String> tables) throws SQLException {
1285-
DescribeTableSettings settings = connection.withDefaultTimeout(new DescribeTableSettings());
1286-
String databaseWithSuffix = withSuffix(connection.getCtx().getDatabase());
1287-
1288-
Map<String, TableDescription> target = new LinkedHashMap<>(tables.size());
1289-
1290-
for (String table: tables) {
1291-
target.put(table, executor.call("Get table description for " + table,
1292-
() -> session.describeTable(databaseWithSuffix + table, settings)
1293-
));
1294-
}
1295-
1296-
return target;
1297-
}
1298-
12991287
private Collection<String> listTables(String tableNamePattern) throws SQLException {
13001288
Predicate<String> filter = equalsFilter(tableNamePattern);
13011289

@@ -1338,21 +1326,35 @@ private List<String> tables(String databasePrefix, String path, Predicate<String
13381326
return tables;
13391327
}
13401328

1341-
private ResultSet fromTables(String tableNamePattern,
1329+
private ResultSet fromTables(Collection<String> tables,
13421330
TableCollector tableCollector,
13431331
Comparator<Map<String, Object>> comparator) throws SQLException {
1344-
Collection<String> tables = listTables(tableNamePattern);
13451332
if (tables.isEmpty()) {
13461333
return fromEmptyResultSet();
13471334
}
13481335

1336+
DescribeTableSettings settings = connection.withDefaultTimeout(new DescribeTableSettings());
1337+
String databaseWithSuffix = withSuffix(connection.getCtx().getDatabase());
1338+
13491339
try (Session session = executor.createSession(connection.getCtx())) {
1350-
Map<String, TableDescription> tableMap = collectTableDescriptions(session, tables);
1340+
List<Map<String, Object>> rows = new ArrayList<>(tables.size() * 16);
13511341

1352-
List<Map<String, Object>> rows = new ArrayList<>(tableMap.size() * 16);
1353-
for (Map.Entry<String, TableDescription> entry : tableMap.entrySet()) {
1354-
tableCollector.collect(entry.getKey(), entry.getValue(), rows);
1342+
for (String table: tables) {
1343+
try {
1344+
TableDescription description = executor.call("Describe table " + table,
1345+
() -> session.describeTable(databaseWithSuffix + table, settings)
1346+
);
1347+
tableCollector.collect(table, description, rows);
1348+
} catch (YdbExecutionStatusException ex) {
1349+
if (ex.getStatusCode() != StatusCode.SCHEME_ERROR) { // ignore scheme errors like path not found
1350+
throw ex;
1351+
}
1352+
LOGGER.log(Level.WARNING, "Cannot describe table {0} -> {1}",
1353+
new Object[]{ table, ex.getMessage() }
1354+
);
1355+
}
13551356
}
1357+
13561358
rows.sort(comparator);
13571359
return fromRows(rows);
13581360
}

0 commit comments

Comments
 (0)