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 2022/12/05 09:11:50 UTC

[shardingsphere] branch master updated: Optimize init ShardingSphereData logic. (#22654)

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

zhaojinchao 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 6b8980bf704 Optimize init ShardingSphereData logic. (#22654)
6b8980bf704 is described below

commit 6b8980bf7045c601fe11aeebccade27ea1baf818
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Mon Dec 5 17:11:43 2022 +0800

    Optimize init ShardingSphereData logic. (#22654)
---
 .../mode/metadata/MetaDataContexts.java            | 39 ++++++++++++++++++----
 1 file changed, 33 insertions(+), 6 deletions(-)

diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java
index 5bfc528c0db..b9ad35109cc 100644
--- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java
+++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContexts.java
@@ -20,10 +20,14 @@ package org.apache.shardingsphere.mode.metadata;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.data.ShardingSphereData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereDatabaseData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereSchemaData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereTableData;
 import org.apache.shardingsphere.infra.metadata.data.builder.ShardingSphereDataBuilderFactory;
 import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 
+import java.util.Map.Entry;
 import java.util.Optional;
 
 /**
@@ -45,16 +49,39 @@ public final class MetaDataContexts implements AutoCloseable {
     }
     
     private ShardingSphereData initShardingSphereData(final MetaDataPersistService persistService, final ShardingSphereMetaData metaData) {
-        Optional<ShardingSphereData> result = Optional.ofNullable(persistService.getShardingSphereDataPersistService())
-                .flatMap(shardingSphereDataPersistService -> shardingSphereDataPersistService.load(metaData));
-        if (result.isPresent()) {
-            return result.get();
-        }
         if (metaData.getDatabases().isEmpty()) {
             return new ShardingSphereData();
         }
-        return Optional.ofNullable(metaData.getDatabases().values().iterator().next().getProtocolType())
+        ShardingSphereData result = Optional.ofNullable(metaData.getDatabases().values().iterator().next().getProtocolType())
                 .flatMap(protocolType -> ShardingSphereDataBuilderFactory.getInstance(protocolType).map(builder -> builder.build(metaData))).orElseGet(ShardingSphereData::new);
+        Optional<ShardingSphereData> loadedShardingSphereData = Optional.ofNullable(persistService.getShardingSphereDataPersistService())
+                .flatMap(shardingSphereDataPersistService -> shardingSphereDataPersistService.load(metaData));
+        loadedShardingSphereData.ifPresent(sphereData -> useLoadedToReplaceInit(result, sphereData));
+        return result;
+    }
+    
+    private void useLoadedToReplaceInit(final ShardingSphereData initShardingSphereData, final ShardingSphereData loadedShardingSphereData) {
+        for (Entry<String, ShardingSphereDatabaseData> entry : initShardingSphereData.getDatabaseData().entrySet()) {
+            if (loadedShardingSphereData.getDatabaseData().containsKey(entry.getKey())) {
+                useLoadedToReplaceInitByDatabaseData(entry.getValue(), loadedShardingSphereData.getDatabaseData().get(entry.getKey()));
+            }
+        }
+    }
+    
+    private void useLoadedToReplaceInitByDatabaseData(final ShardingSphereDatabaseData initDatabaseData, final ShardingSphereDatabaseData loadedDatabaseData) {
+        for (Entry<String, ShardingSphereSchemaData> entry : initDatabaseData.getSchemaData().entrySet()) {
+            if (loadedDatabaseData.getSchemaData().containsKey(entry.getKey())) {
+                useLoadedToReplaceInitBySchemaData(entry.getValue(), loadedDatabaseData.getSchemaData().get(entry.getKey()));
+            }
+        }
+    }
+    
+    private void useLoadedToReplaceInitBySchemaData(final ShardingSphereSchemaData initSchemaData, final ShardingSphereSchemaData loadedSchemaData) {
+        for (Entry<String, ShardingSphereTableData> entry : initSchemaData.getTableData().entrySet()) {
+            if (loadedSchemaData.getTableData().containsKey(entry.getKey())) {
+                entry.setValue(loadedSchemaData.getTableData().get(entry.getKey()));
+            }
+        }
     }
     
     @Override