This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 2b31a99d758 Refactor data collection for pg_namespace and pg_class
tables (#32066)
2b31a99d758 is described below
commit 2b31a99d75824d9b937e11bc78abd3b83fe1a204
Author: jiangML <[email protected]>
AuthorDate: Thu Jul 11 19:23:12 2024 +0800
Refactor data collection for pg_namespace and pg_class tables (#32066)
---
.../ShardingSphereTableDataCollectorUtils.java | 68 +++-------------------
.../collector/tables/PgClassTableCollector.java | 52 ++++++-----------
.../tables/PgNamespaceTableCollector.java | 29 +++++----
3 files changed, 45 insertions(+), 104 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java
index 0d99ad37607..25033b786ac 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/ShardingSphereTableDataCollectorUtils.java
@@ -19,21 +19,13 @@ package
org.apache.shardingsphere.infra.metadata.statistics.collector;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
-import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
-import java.sql.Connection;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
import java.sql.Types;
-import java.util.Collection;
-import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
+import java.util.Map;
/**
* Table data collector utility class.
@@ -42,66 +34,20 @@ import java.util.List;
public final class ShardingSphereTableDataCollectorUtils {
/**
- * Collect row data.
+ * Create row value.
*
- * @param database ShardingSphere database
- * @param table table
- * @param selectedColumnNames selected column names
- * @param sql SQL
- * @return ShardingSphere row data
- * @throws SQLException sql exception
+ * @param columnValues column values
+ * @param table sharding sphere table
+ * @return objects
*/
- public static Collection<ShardingSphereRowData> collectRowData(final
ShardingSphereDatabase database, final ShardingSphereTable table,
- final
Collection<String> selectedColumnNames, final String sql) throws SQLException {
- if (isDifferentProtocolAndStorageType(database)) {
- return Collections.emptyList();
- }
- Collection<ShardingSphereRowData> result = new LinkedList<>();
- for (StorageUnit each :
database.getResourceMetaData().getStorageUnits().values()) {
- try (
- Connection connection =
each.getDataSource().getConnection();
- Statement statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery(sql)) {
- result.addAll(getRows(table, selectedColumnNames, resultSet));
- }
- }
- return result;
- }
-
- private static boolean isDifferentProtocolAndStorageType(final
ShardingSphereDatabase database) {
- return
!database.getResourceMetaData().getStorageUnits().values().stream().allMatch(each
-> each.getStorageType().equals(database.getProtocolType()));
- }
-
- private static Collection<ShardingSphereRowData> getRows(final
ShardingSphereTable table, final Collection<String> selectedColumnNames, final
ResultSet resultSet) throws SQLException {
- Collection<ShardingSphereRowData> result = new LinkedList<>();
- while (resultSet.next()) {
- result.add(new ShardingSphereRowData(getRow(table,
selectedColumnNames, resultSet)));
- }
- return result;
- }
-
- private static List<Object> getRow(final ShardingSphereTable table, final
Collection<String> selectedColumnNames, final ResultSet resultSet) throws
SQLException {
+ public static List<Object> createRowValue(final Map<String, Object>
columnValues, final ShardingSphereTable table) {
List<Object> result = new LinkedList<>();
for (ShardingSphereColumn each : table.getColumnValues()) {
- result.add(selectedColumnNames.contains(each.getName()) ?
convertValueIfNecessary(resultSet.getObject(each.getName()),
each.getDataType()) : mockValue(each.getDataType()));
+ result.add(columnValues.getOrDefault(each.getName(),
mockValue(each.getDataType())));
}
return result;
}
- private static Object convertValueIfNecessary(final Object data, final int
dataType) {
- if (null == data) {
- return null;
- }
- switch (dataType) {
- case Types.ARRAY:
- return data.toString();
- case Types.BIGINT:
- return Long.valueOf(data.toString());
- default:
- return data;
- }
- }
-
private static Object mockValue(final int dataType) {
switch (dataType) {
case Types.BIGINT:
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java
index 3f1ff70e775..d58aee597ed 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgClassTableCollector.java
@@ -17,24 +17,22 @@
package org.apache.shardingsphere.infra.metadata.statistics.collector.tables;
+import com.cedarsoftware.util.CaseInsensitiveMap;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereStatisticsCollector;
import
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereTableDataCollectorUtils;
-import
org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
-import java.util.stream.Collectors;
/**
* Table pg_class data collector.
@@ -43,48 +41,36 @@ public final class PgClassTableCollector implements
ShardingSphereStatisticsColl
private static final String PG_CLASS = "pg_class";
- private static final String COLUMN_NAMES = "relname, relnamespace,
relkind, reloptions";
+ private static final String PUBLIC_SCHEMA = "public";
- private static final String SELECT_SQL = "SELECT " + COLUMN_NAMES + " FROM
pg_catalog.pg_class WHERE relkind IN ('r','v','m','S','L','f','e','o','') "
- + "AND relname NOT LIKE 'matviewmap\\_%' AND relname NOT LIKE
'mlog\\_%' AND pg_catalog.pg_table_is_visible(oid);";
+ private static final Long PUBLIC_SCHEMA_OID = 0L;
@Override
public Optional<ShardingSphereTableData> collect(final String
databaseName, final ShardingSphereTable table, final Map<String,
ShardingSphereDatabase> databases,
final RuleMetaData
globalRuleMetaData) throws SQLException {
- Collection<ShardingSphereRowData> rows =
ShardingSphereTableDataCollectorUtils.collectRowData(databases.get(databaseName),
- table,
Arrays.stream(COLUMN_NAMES.split(",")).map(String::trim).collect(Collectors.toList()),
SELECT_SQL);
- Collection<ShardingSphereRowData> rowData = decorateTableName(rows,
table, databases.get(databaseName).getRuleMetaData());
ShardingSphereTableData result = new ShardingSphereTableData(PG_CLASS);
- result.getRows().addAll(rowData);
+ long oid = 0L;
+ for (Entry<String, ShardingSphereSchema> entry :
databases.get(databaseName).getSchemas().entrySet()) {
+ if (PUBLIC_SCHEMA.equalsIgnoreCase(entry.getKey())) {
+ result.getRows().addAll(collectForSchema(oid++,
PUBLIC_SCHEMA_OID, entry.getValue(), table));
+ }
+ }
return Optional.of(result);
}
- private Collection<ShardingSphereRowData> decorateTableName(final
Collection<ShardingSphereRowData> rows, final ShardingSphereTable table, final
RuleMetaData ruleMetaData) {
- Collection<DataNodeRuleAttribute> ruleAttributes =
ruleMetaData.getAttributes(DataNodeRuleAttribute.class);
- if (ruleAttributes.isEmpty()) {
- return rows;
- }
- int tableNameIndex = table.getColumnNames().indexOf("relname");
+ private Collection<ShardingSphereRowData> collectForSchema(final Long oid,
final Long relNamespace, final ShardingSphereSchema schema, final
ShardingSphereTable table) {
Collection<ShardingSphereRowData> result = new LinkedList<>();
- for (ShardingSphereRowData each : rows) {
- String tableName = (String) each.getRows().get(tableNameIndex);
- String logicTableName = decorateTableName(ruleAttributes,
tableName);
- List<Object> decoratedRow = new ArrayList<>(each.getRows());
- decoratedRow.set(tableNameIndex, logicTableName);
- result.add(new ShardingSphereRowData(decoratedRow));
+ for (Entry<String, ShardingSphereTable> entry :
schema.getTables().entrySet()) {
+ Map<String, Object> columnValues = new CaseInsensitiveMap<>(4, 1F);
+ columnValues.put("oid", oid);
+ columnValues.put("relnamespace", relNamespace);
+ columnValues.put("relname", entry.getKey());
+ columnValues.put("relkind", "r");
+ result.add(new
ShardingSphereRowData(ShardingSphereTableDataCollectorUtils.createRowValue(columnValues,
table)));
}
return result;
}
- private String decorateTableName(final Collection<DataNodeRuleAttribute>
ruleAttributes, final String actualTableName) {
- for (DataNodeRuleAttribute each : ruleAttributes) {
- if (each.findLogicTableByActualTable(actualTableName).isPresent())
{
- return each.findLogicTableByActualTable(actualTableName).get();
- }
- }
- return actualTableName;
- }
-
@Override
public String getType() {
return PG_CLASS;
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgNamespaceTableCollector.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgNamespaceTableCollector.java
index 9c59bc8f8d4..d3213537f91 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgNamespaceTableCollector.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/collector/tables/PgNamespaceTableCollector.java
@@ -17,20 +17,21 @@
package org.apache.shardingsphere.infra.metadata.statistics.collector.tables;
+import com.cedarsoftware.util.CaseInsensitiveMap;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereStatisticsCollector;
import
org.apache.shardingsphere.infra.metadata.statistics.collector.ShardingSphereTableDataCollectorUtils;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.Collection;
+import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
-import java.util.stream.Collectors;
/**
* Table pg_namespace data collector.
@@ -39,20 +40,28 @@ public final class PgNamespaceTableCollector implements
ShardingSphereStatistics
private static final String PG_NAMESPACE = "pg_namespace";
- private static final String COLUMN_NAMES = "oid, nspname, nspowner,
nspacl";
+ private static final String PUBLIC_SCHEMA = "public";
- private static final String SELECT_SQL = "SELECT " + COLUMN_NAMES + " FROM
pg_catalog.pg_namespace";
+ private static final Long PUBLIC_SCHEMA_OID = 0L;
@Override
public Optional<ShardingSphereTableData> collect(final String
databaseName, final ShardingSphereTable table, final Map<String,
ShardingSphereDatabase> databases,
final RuleMetaData
globalRuleMetaData) throws SQLException {
- Collection<ShardingSphereRowData> rows =
ShardingSphereTableDataCollectorUtils.collectRowData(databases.get(databaseName),
- table,
Arrays.stream(COLUMN_NAMES.split(",")).map(String::trim).collect(Collectors.toList()),
SELECT_SQL);
ShardingSphereTableData result = new
ShardingSphereTableData(PG_NAMESPACE);
- result.getRows().addAll(rows);
+ long oid = 1L;
+ for (Entry<String, ShardingSphereSchema> entry :
databases.get(databaseName).getSchemas().entrySet()) {
+ result.getRows().add(new
ShardingSphereRowData(getRow(PUBLIC_SCHEMA.equalsIgnoreCase(entry.getKey()) ?
PUBLIC_SCHEMA_OID : oid++, entry.getKey(), table)));
+ }
return Optional.of(result);
}
+ private List<Object> getRow(final Long oid, final String schemaName, final
ShardingSphereTable table) {
+ Map<String, Object> columnValues = new CaseInsensitiveMap<>(2, 1F);
+ columnValues.put("oid", oid);
+ columnValues.put("nspname", schemaName);
+ return
ShardingSphereTableDataCollectorUtils.createRowValue(columnValues, table);
+ }
+
@Override
public String getType() {
return PG_NAMESPACE;