You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/02/06 10:11:42 UTC

[shardingsphere] branch master updated: Add ShardingIndexReviser (#24022)

This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 c73afdc688d Add ShardingIndexReviser (#24022)
c73afdc688d is described below

commit c73afdc688d5f9625ca00b604659949514fd0027
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Feb 6 18:11:34 2023 +0800

    Add ShardingIndexReviser (#24022)
---
 .../metadata/ShardingSchemaMetaDataDecorator.java  | 20 +++------
 .../metadata/reviser/ShardingIndexReviser.java     | 51 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 15 deletions(-)

diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingSchemaMetaDataDecorator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingSchemaMetaDataDecorator.java
index 5811eaecf8e..fc98652c19d 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingSchemaMetaDataDecorator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingSchemaMetaDataDecorator.java
@@ -21,15 +21,15 @@ import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
 import org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser.column.ColumnReviseEngine;
+import org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser.index.IndexReviseEngine;
 import org.apache.shardingsphere.infra.metadata.database.schema.decorator.spi.RuleBasedSchemaMetaDataDecorator;
-import org.apache.shardingsphere.infra.metadata.database.schema.loader.model.ColumnMetaData;
 import org.apache.shardingsphere.infra.metadata.database.schema.loader.model.ConstraintMetaData;
-import org.apache.shardingsphere.infra.metadata.database.schema.loader.model.IndexMetaData;
 import org.apache.shardingsphere.infra.metadata.database.schema.loader.model.SchemaMetaData;
 import org.apache.shardingsphere.infra.metadata.database.schema.loader.model.TableMetaData;
 import org.apache.shardingsphere.sharding.constant.ShardingOrder;
 import org.apache.shardingsphere.sharding.exception.metadata.InconsistentShardingTableMetaDataException;
 import org.apache.shardingsphere.sharding.metadata.reviser.ShardingColumnGeneratedReviser;
+import org.apache.shardingsphere.sharding.metadata.reviser.ShardingIndexReviser;
 import org.apache.shardingsphere.sharding.rule.ShardingRule;
 import org.apache.shardingsphere.sharding.rule.TableRule;
 
@@ -70,10 +70,10 @@ public final class ShardingSchemaMetaDataDecorator implements RuleBasedSchemaMet
     }
     
     private TableMetaData createTableMetaData(final ShardingRule rule, final TableRule tableRule, final TableMetaData tableMetaData) {
-        Collection<ColumnMetaData> columnMetaDataList = new ColumnReviseEngine().revise(tableMetaData.getColumns(), Collections.singleton(new ShardingColumnGeneratedReviser(tableRule)));
-        Collection<IndexMetaData> indexMetaDataList = getIndexMetaDataList(tableMetaData, tableRule);
         Collection<ConstraintMetaData> constraintMetaDataList = getConstraintMetaDataList(tableMetaData, rule, tableRule);
-        return new TableMetaData(tableRule.getLogicTable(), columnMetaDataList, indexMetaDataList, constraintMetaDataList);
+        return new TableMetaData(tableRule.getLogicTable(), new ColumnReviseEngine().revise(tableMetaData.getColumns(), Collections.singleton(new ShardingColumnGeneratedReviser(tableRule))),
+                new IndexReviseEngine().revise(tableMetaData.getName(), tableMetaData.getIndexes(), Collections.singleton(new ShardingIndexReviser(tableRule))),
+                constraintMetaDataList);
     }
     
     private Map<String, Collection<TableMetaData>> getLogicTableMetaDataMap(final SchemaMetaData schemaMetaData, final ShardingRule rule) {
@@ -95,16 +95,6 @@ public final class ShardingSchemaMetaDataDecorator implements RuleBasedSchemaMet
         }
     }
     
-    private Collection<IndexMetaData> getIndexMetaDataList(final TableMetaData tableMetaData, final TableRule tableRule) {
-        Collection<IndexMetaData> result = new HashSet<>();
-        for (IndexMetaData each : tableMetaData.getIndexes()) {
-            for (DataNode dataNode : tableRule.getActualDataNodes()) {
-                getLogicIndex(each.getName(), dataNode.getTableName()).ifPresent(optional -> result.add(new IndexMetaData(optional)));
-            }
-        }
-        return result;
-    }
-    
     private Collection<ConstraintMetaData> getConstraintMetaDataList(final TableMetaData tableMetaData, final ShardingRule shardingRule, final TableRule tableRule) {
         Collection<ConstraintMetaData> result = new HashSet<>();
         for (ConstraintMetaData each : tableMetaData.getConstrains()) {
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/reviser/ShardingIndexReviser.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/reviser/ShardingIndexReviser.java
new file mode 100644
index 00000000000..73198371902
--- /dev/null
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/metadata/reviser/ShardingIndexReviser.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sharding.metadata.reviser;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.metadata.database.schema.decorator.reviser.index.IndexReviser;
+import org.apache.shardingsphere.infra.metadata.database.schema.loader.model.IndexMetaData;
+import org.apache.shardingsphere.sharding.rule.TableRule;
+
+import java.util.Optional;
+
+/**
+ * Sharding index reviser.
+ */
+@RequiredArgsConstructor
+public final class ShardingIndexReviser implements IndexReviser {
+    
+    private final TableRule tableRule;
+    
+    @Override
+    public Optional<IndexMetaData> revise(final String tableName, final IndexMetaData originalMetaData) {
+        for (DataNode each : tableRule.getActualDataNodes()) {
+            Optional<String> logicIndexName = getLogicIndex(originalMetaData.getName(), each.getTableName());
+            if (logicIndexName.isPresent()) {
+                return Optional.of(new IndexMetaData(logicIndexName.get()));
+            }
+        }
+        return Optional.empty();
+    }
+    
+    private Optional<String> getLogicIndex(final String actualIndexName, final String actualTableName) {
+        String indexNameSuffix = "_" + actualTableName;
+        return actualIndexName.endsWith(indexNameSuffix) ? Optional.of(actualIndexName.replace(indexNameSuffix, "")) : Optional.empty();
+    }
+}