You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2024/03/06 11:25:08 UTC
(shardingsphere) branch master updated: Reuse TableRefreshUtils.isSingleTable (#30408)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 93ceabc8260 Reuse TableRefreshUtils.isSingleTable (#30408)
93ceabc8260 is described below
commit 93ceabc8260e3b59d6c45d05bd4178bc0f532156
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Mar 6 19:25:01 2024 +0800
Reuse TableRefreshUtils.isSingleTable (#30408)
---
.../table/DropTableStatementSchemaRefresher.java | 21 +++------------------
.../context/ResourceMetaDataContextManager.java | 17 +++--------------
.../standalone/NewStandaloneModeContextManager.java | 19 ++++---------------
.../standalone/StandaloneModeContextManager.java | 19 ++++---------------
4 files changed, 14 insertions(+), 62 deletions(-)
diff --git a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java
index 865ac1034da..169c173e292 100644
--- a/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java
+++ b/infra/context/src/main/java/org/apache/shardingsphere/infra/connection/refresher/type/table/DropTableStatementSchemaRefresher.java
@@ -23,15 +23,11 @@ import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUti
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
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.pojo.AlterSchemaMetaDataPOJO;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;
import java.util.Collection;
-import java.util.Optional;
/**
* Schema refresher for drop table statement.
@@ -43,27 +39,16 @@ public final class DropTableStatementSchemaRefresher implements MetaDataRefreshe
final String schemaName, final DatabaseType databaseType, final DropTableStatement sqlStatement, final ConfigurationProperties props) {
AlterSchemaMetaDataPOJO alterSchemaMetaDataPOJO = new AlterSchemaMetaDataPOJO(database.getName(), schemaName);
sqlStatement.getTables().forEach(each -> alterSchemaMetaDataPOJO.getDroppedTables().add(each.getTableName().getIdentifier().getValue()));
- RuleMetaData ruleMetaData = database.getRuleMetaData();
- boolean isRuleRefreshRequired = TableRefreshUtils.isRuleRefreshRequired(ruleMetaData, schemaName, sqlStatement.getTables());
+ boolean isRuleRefreshRequired = TableRefreshUtils.isRuleRefreshRequired(database.getRuleMetaData(), schemaName, sqlStatement.getTables());
modeContextManager.alterSchemaMetaData(alterSchemaMetaDataPOJO);
for (SimpleTableSegment each : sqlStatement.getTables()) {
- if (isRuleRefreshRequired && isSingleTable(each.getTableName().getIdentifier().getValue(), ruleMetaData)) {
- modeContextManager.alterRuleConfiguration(database.getName(), ruleMetaData.getConfigurations());
+ if (isRuleRefreshRequired && TableRefreshUtils.isSingleTable(each.getTableName().getIdentifier().getValue(), database)) {
+ modeContextManager.alterRuleConfiguration(database.getName(), database.getRuleMetaData().getConfigurations());
break;
}
}
}
- private boolean isSingleTable(final String tableName, final RuleMetaData ruleMetaData) {
- for (ShardingSphereRule each : ruleMetaData.getRules()) {
- Optional<TableMapperRule> tableMapperRule = each.getRuleIdentifiers().findIdentifier(TableMapperRule.class);
- if (tableMapperRule.isPresent() && tableMapperRule.get().getDistributedTableMapper().contains(tableName)) {
- return false;
- }
- }
- return true;
- }
-
@Override
public Class<DropTableStatement> getType() {
return DropTableStatement.class;
diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java
index 5a07663a3ca..1655dd7963d 100644
--- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java
+++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ResourceMetaDataContextManager.java
@@ -18,16 +18,15 @@
package org.apache.shardingsphere.mode.manager.context;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUtils;
import org.apache.shardingsphere.infra.database.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
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.database.schema.model.ShardingSphereView;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MetaDataHeldRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
-import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import java.util.Collections;
@@ -156,7 +155,7 @@ public final class ResourceMetaDataContextManager {
private void alterTable(final String databaseName, final String schemaName, final ShardingSphereTable beBoChangedTable) {
ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName);
- if (isSingleTable(database, beBoChangedTable.getName())) {
+ if (TableRefreshUtils.isSingleTable(beBoChangedTable.getName(), database)) {
database.reloadRules(MutableDataNodeRule.class);
}
database.getSchema(schemaName).putTable(beBoChangedTable.getName(), beBoChangedTable);
@@ -164,19 +163,9 @@ public final class ResourceMetaDataContextManager {
private void alterView(final String databaseName, final String schemaName, final ShardingSphereView beBoChangedView) {
ShardingSphereDatabase database = metaDataContexts.get().getMetaData().getDatabase(databaseName);
- if (isSingleTable(database, beBoChangedView.getName())) {
+ if (TableRefreshUtils.isSingleTable(beBoChangedView.getName(), database)) {
database.reloadRules(MutableDataNodeRule.class);
}
database.getSchema(schemaName).putView(beBoChangedView.getName(), beBoChangedView);
}
-
- private boolean isSingleTable(final ShardingSphereDatabase database, final String tableName) {
- for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
- Optional<TableMapperRule> tableMapperRule = each.getRuleIdentifiers().findIdentifier(TableMapperRule.class);
- if (tableMapperRule.isPresent() && tableMapperRule.get().getDistributedTableMapper().contains(tableName)) {
- return true;
- }
- }
- return false;
- }
}
diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java
index a5b627b36ef..d66552e7bed 100644
--- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java
+++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/NewStandaloneModeContextManager.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mode.manager.standalone;
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUtils;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -28,11 +29,9 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO;
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO;
import org.apache.shardingsphere.infra.metadata.version.MetaDataVersion;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MetaDataHeldRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule;
-import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache;
import org.apache.shardingsphere.metadata.persist.service.config.global.GlobalPersistService;
import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataBasedPersistService;
@@ -109,7 +108,7 @@ public final class NewStandaloneModeContextManager implements ModeContextManager
private void addDataNode(final ShardingSphereDatabase database, final String logicDataSourceName, final String schemaName, final Collection<String> tobeAddedTableNames) {
tobeAddedTableNames.forEach(each -> {
- if (!Strings.isNullOrEmpty(logicDataSourceName) && isSingleTable(each, database)) {
+ if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(each, database)) {
database.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, each));
}
});
@@ -123,7 +122,7 @@ public final class NewStandaloneModeContextManager implements ModeContextManager
private void addTablesToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, final Map<String, ShardingSphereTable> toBeAddedTables) {
for (Entry<String, ShardingSphereTable> entry : toBeAddedTables.entrySet()) {
- if (!Strings.isNullOrEmpty(logicDataSourceName) && isSingleTable(entry.getKey(), database)) {
+ if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) {
database.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey()));
}
database.getSchema(schemaName).putTable(entry.getKey(), entry.getValue());
@@ -133,7 +132,7 @@ public final class NewStandaloneModeContextManager implements ModeContextManager
private void addViewsToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName,
final Map<String, ShardingSphereTable> toBeAddedTables, final Map<String, ShardingSphereView> toBeAddedViews) {
for (Entry<String, ShardingSphereView> entry : toBeAddedViews.entrySet()) {
- if (!Strings.isNullOrEmpty(logicDataSourceName) && isSingleTable(entry.getKey(), database)) {
+ if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) {
database.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey()));
}
database.getSchema(schemaName).putTable(entry.getKey(), toBeAddedTables.get(entry.getKey().toLowerCase()));
@@ -141,16 +140,6 @@ public final class NewStandaloneModeContextManager implements ModeContextManager
}
}
- private boolean isSingleTable(final String tableName, final ShardingSphereDatabase database) {
- for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
- Optional<TableMapperRule> tableMapperRule = each.getRuleIdentifiers().findIdentifier(TableMapperRule.class);
- if (tableMapperRule.isPresent() && tableMapperRule.get().getDistributedTableMapper().contains(tableName)) {
- return false;
- }
- }
- return true;
- }
-
private void removeSchemaMetaData(final ShardingSphereDatabase database, final String schemaName) {
ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(schemaName).getTables(), database.getSchema(schemaName).getViews());
database.dropSchema(schemaName);
diff --git a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java
index 61db6020ae6..01ada812ca8 100644
--- a/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java
+++ b/mode/type/standalone/core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneModeContextManager.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mode.manager.standalone;
import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.connection.refresher.util.TableRefreshUtils;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.instance.mode.ModeContextManager;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -27,11 +28,9 @@ import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView;
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO;
import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaPOJO;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MetaDataHeldRule;
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule;
-import org.apache.shardingsphere.infra.rule.identifier.type.table.TableMapperRule;
import org.apache.shardingsphere.infra.spi.type.ordered.cache.OrderedServicesCache;
import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataBasedPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -101,7 +100,7 @@ public final class StandaloneModeContextManager implements ModeContextManager, C
private void addDataNode(final ShardingSphereDatabase database, final String logicDataSourceName, final String schemaName, final Collection<String> tobeAddedTableNames) {
tobeAddedTableNames.forEach(each -> {
- if (!Strings.isNullOrEmpty(logicDataSourceName) && isSingleTable(each, database)) {
+ if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(each, database)) {
database.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, each));
}
});
@@ -115,7 +114,7 @@ public final class StandaloneModeContextManager implements ModeContextManager, C
private void addTablesToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName, final Map<String, ShardingSphereTable> toBeAddedTables) {
for (Entry<String, ShardingSphereTable> entry : toBeAddedTables.entrySet()) {
- if (!Strings.isNullOrEmpty(logicDataSourceName) && isSingleTable(entry.getKey(), database)) {
+ if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) {
database.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey()));
}
database.getSchema(schemaName).putTable(entry.getKey(), entry.getValue());
@@ -125,7 +124,7 @@ public final class StandaloneModeContextManager implements ModeContextManager, C
private void addViewsToDataNode(final ShardingSphereDatabase database, final String schemaName, final String logicDataSourceName,
final Map<String, ShardingSphereTable> toBeAddedTables, final Map<String, ShardingSphereView> toBeAddedViews) {
for (Entry<String, ShardingSphereView> entry : toBeAddedViews.entrySet()) {
- if (!Strings.isNullOrEmpty(logicDataSourceName) && isSingleTable(entry.getKey(), database)) {
+ if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) {
database.getRuleMetaData().findRules(MutableDataNodeRule.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey()));
}
database.getSchema(schemaName).putTable(entry.getKey(), toBeAddedTables.get(entry.getKey().toLowerCase()));
@@ -133,16 +132,6 @@ public final class StandaloneModeContextManager implements ModeContextManager, C
}
}
- private boolean isSingleTable(final String tableName, final ShardingSphereDatabase database) {
- for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
- Optional<TableMapperRule> tableMapperRule = each.getRuleIdentifiers().findIdentifier(TableMapperRule.class);
- if (tableMapperRule.isPresent() && tableMapperRule.get().getDistributedTableMapper().contains(tableName)) {
- return false;
- }
- }
- return true;
- }
-
private void removeSchemaMetaData(final ShardingSphereDatabase database, final String schemaName) {
ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(schemaName).getTables(), database.getSchema(schemaName).getViews());
database.dropSchema(schemaName);