You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/06/02 16:55:33 UTC
[shardingsphere] branch master updated: Refactor
DropResourceBackendHandler (#10628)
This is an automated email from the ASF dual-hosted git repository.
wuweijie 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 1a31bbc Refactor DropResourceBackendHandler (#10628)
1a31bbc is described below
commit 1a31bbc21848e00f5aab5e048d84b3c6359e8df6
Author: Liang Zhang <te...@163.com>
AuthorDate: Thu Jun 3 00:54:36 2021 +0800
Refactor DropResourceBackendHandler (#10628)
---
.../metadata/resource/ShardingSphereResource.java | 2 +-
.../rdl/impl/DropResourceBackendHandler.java | 65 +++++++++++-----------
2 files changed, 34 insertions(+), 33 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java
index 7cff8af..c451985 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/ShardingSphereResource.java
@@ -58,7 +58,7 @@ public final class ShardingSphereResource {
* @throws SQLException exception
*/
public void close(final Collection<String> dataSources) throws SQLException {
- for (String each :dataSources) {
+ for (String each : dataSources) {
close(this.dataSources.get(each));
}
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropResourceBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropResourceBackendHandler.java
index 30f8d5b..1fbdb1e 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropResourceBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropResourceBackendHandler.java
@@ -35,8 +35,8 @@ import org.apache.shardingsphere.proxy.backend.text.SchemaRequiredBackendHandler
import javax.sql.DataSource;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@@ -52,43 +52,46 @@ public final class DropResourceBackendHandler extends SchemaRequiredBackendHandl
@Override
public ResponseHeader execute(final String schemaName, final DropResourceStatement sqlStatement) {
- Collection<String> resourceNames = sqlStatement.getResourceNames();
- check(schemaName, resourceNames);
- Map<String, DataSource> dataSources = drop(schemaName, resourceNames);
- post(schemaName, dataSources);
+ check(schemaName, sqlStatement.getResourceNames());
+ drop(schemaName, sqlStatement.getResourceNames());
+ post(schemaName, ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources());
return new UpdateResponseHeader(sqlStatement);
}
- private void check(final String schemaName, final Collection<String> resourceNames) {
- Map<String, DataSource> resourceMap = ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources();
- if (null == resourceMap || resourceMap.isEmpty()) {
- throw new ResourceNotExistedException(schemaName, resourceNames);
- }
- Collection<String> notExistedResourceNames = resourceNames.stream().filter(each -> !resourceMap.containsKey(each)).collect(Collectors.toList());
+ private void check(final String schemaName, final Collection<String> toBeDroppedResourceNames) {
+ checkResourceNameExisted(schemaName, toBeDroppedResourceNames);
+ checkResourceNameNotInUse(schemaName, toBeDroppedResourceNames);
+ }
+
+ private void checkResourceNameExisted(final String schemaName, final Collection<String> resourceNames) {
+ Map<String, DataSource> resources = ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources();
+ Collection<String> notExistedResourceNames = resourceNames.stream().filter(each -> !resources.containsKey(each)).collect(Collectors.toList());
if (!notExistedResourceNames.isEmpty()) {
throw new ResourceNotExistedException(schemaName, notExistedResourceNames);
}
- Collection<ShardingSphereRule> ruleConfig = ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getRules();
- Set<String> useResources = new HashSet<>();
- for (ShardingSphereRule each : ruleConfig) {
+ }
+
+ private void checkResourceNameNotInUse(final String schemaName, final Collection<String> toBeDroppedResourceNames) {
+ Collection<String> inUsedResourceNames = getInUsedResourceNames(schemaName);
+ inUsedResourceNames.retainAll(toBeDroppedResourceNames);
+ if (!inUsedResourceNames.isEmpty()) {
+ throw new ResourceInUsedException(inUsedResourceNames);
+ }
+ }
+
+ private Collection<String> getInUsedResourceNames(final String schemaName) {
+ for (ShardingSphereRule each : ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getRules()) {
if (each instanceof DataSourceContainedRule) {
- useResources = getResources((DataSourceContainedRule) each);
- } else if (each instanceof DataNodeContainedRule) {
- useResources = getResources((DataNodeContainedRule) each);
+ return getInUsedResourceNames((DataSourceContainedRule) each);
}
- }
- Collection<String> conflictResources = new LinkedList<>();
- for (String each : resourceNames) {
- if (useResources.contains(each)) {
- conflictResources.add(each);
+ if (each instanceof DataNodeContainedRule) {
+ return getInUsedResourceNames((DataNodeContainedRule) each);
}
}
- if (!conflictResources.isEmpty()) {
- throw new ResourceInUsedException(conflictResources);
- }
+ return Collections.emptyList();
}
- private Set<String> getResources(final DataSourceContainedRule rule) {
+ private Set<String> getInUsedResourceNames(final DataSourceContainedRule rule) {
Set<String> result = new HashSet<>();
for (Collection<String> each : rule.getDataSourceMapper().values()) {
result.addAll(each);
@@ -96,7 +99,7 @@ public final class DropResourceBackendHandler extends SchemaRequiredBackendHandl
return result;
}
- private Set<String> getResources(final DataNodeContainedRule rule) {
+ private Set<String> getInUsedResourceNames(final DataNodeContainedRule rule) {
Set<String> result = new HashSet<>();
for (Collection<DataNode> each : rule.getAllDataNodes().values()) {
result.addAll(each.stream().map(DataNode::getDataSourceName).collect(Collectors.toList()));
@@ -104,12 +107,10 @@ public final class DropResourceBackendHandler extends SchemaRequiredBackendHandl
return result;
}
- private Map<String, DataSource> drop(final String schemaName, final Collection<String> resourceNames) {
- Map<String, DataSource> result = ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources();
- for (String each : resourceNames) {
- result.remove(each);
+ private void drop(final String schemaName, final Collection<String> toBeDroppedResourceNames) {
+ for (String each : toBeDroppedResourceNames) {
+ ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources().remove(each);
}
- return result;
}
private void post(final String schemaName, final Map<String, DataSource> dataSources) {