You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2022/09/03 16:07:53 UTC

[shardingsphere] branch master updated: Implement ViewMetaDataPersistService interface (#20753)

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

yx9o 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 1c0b47bad70 Implement ViewMetaDataPersistService interface (#20753)
1c0b47bad70 is described below

commit 1c0b47bad70364d112a81976caa8818706d1b30a
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Sun Sep 4 00:07:38 2022 +0800

    Implement ViewMetaDataPersistService interface (#20753)
---
 .../schema/decorator/model/ShardingSphereView.java |  4 --
 .../persist/node/DatabaseMetaDataNode.java         | 25 ++++++++++
 .../service/schema/ViewMetaDataPersistService.java | 53 +++++++++++++++++++---
 .../persist/node/DatabaseMetaDataNodeTest.java     | 10 ++++
 .../DatabaseMetaDataPersistServiceTest.java        |  2 +-
 .../schema/TableMetaDataPersistServiceTest.java    |  2 +-
 ...st.java => ViewMetaDataPersistServiceTest.java} | 45 +++++++++---------
 .../yaml/schema/{schema.yaml => table.yaml}        |  0
 .../yaml/schema/{schema.yaml => view.yaml}         | 13 +-----
 9 files changed, 109 insertions(+), 45 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/model/ShardingSphereView.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/model/ShardingSphereView.java
index a506b01b3c8..263adc932c4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/model/ShardingSphereView.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/decorator/model/ShardingSphereView.java
@@ -33,10 +33,6 @@ public final class ShardingSphereView {
     
     private final String viewDefinition;
     
-    public ShardingSphereView() {
-        this("", "");
-    }
-    
     public ShardingSphereView(final String name, final String viewDefinition) {
         this.name = name;
         this.viewDefinition = viewDefinition;
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNode.java b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNode.java
index c881a29bd1e..ebd622163b4 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNode.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNode.java
@@ -40,6 +40,8 @@ public final class DatabaseMetaDataNode {
     
     private static final String TABLES_NODE = "tables";
     
+    private static final String VIEWS_NODE = "views";
+    
     private static final String ACTIVE_VERSION = "active_version";
     
     private static final String VERSIONS = "versions";
@@ -96,6 +98,17 @@ public final class DatabaseMetaDataNode {
         return String.join("/", getMetaDataSchemaPath(databaseName, schemaName), TABLES_NODE);
     }
     
+    /**
+     * Get meta data views path.
+     *
+     * @param databaseName database name
+     * @param schemaName schema name
+     * @return views path
+     */
+    public static String getMetaDataViewsPath(final String databaseName, final String schemaName) {
+        return String.join("/", getMetaDataSchemaPath(databaseName, schemaName), VIEWS_NODE);
+    }
+    
     /**
      * Get schema path.
      *
@@ -129,6 +142,18 @@ public final class DatabaseMetaDataNode {
         return String.join("/", getMetaDataTablesPath(databaseName, schemaName), table);
     }
     
+    /**
+     * Get view meta data path.
+     *
+     * @param databaseName database name
+     * @param schemaName schema name
+     * @param view view name
+     * @return view meta data path
+     */
+    public static String getViewMetaDataPath(final String databaseName, final String schemaName, final String view) {
+        return String.join("/", getMetaDataViewsPath(databaseName, schemaName), view);
+    }
+    
     private static String getFullMetaDataPath(final String databaseName, final String node) {
         return String.join("/", "", ROOT_NODE, databaseName, node);
     }
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/schema/ViewMetaDataPersistService.java b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/schema/ViewMetaDataPersistService.java
index 688e3d51c95..907a36fd8d4 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/schema/ViewMetaDataPersistService.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/metadata/persist/service/schema/ViewMetaDataPersistService.java
@@ -18,11 +18,19 @@
 package org.apache.shardingsphere.mode.metadata.persist.service.schema;
 
 import lombok.RequiredArgsConstructor;
+import org.apache.commons.lang.StringUtils;
 import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereView;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereView;
+import org.apache.shardingsphere.infra.yaml.schema.swapper.YamlViewSwapper;
+import org.apache.shardingsphere.mode.metadata.persist.node.DatabaseMetaDataNode;
 import org.apache.shardingsphere.mode.persist.PersistRepository;
 
+import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * View meta data persist service.
@@ -33,22 +41,55 @@ public final class ViewMetaDataPersistService implements SchemaMetaDataPersistSe
     private final PersistRepository repository;
     
     @Override
-    public void compareAndPersist(final String databaseName, final String schemaName, final Map<String, ShardingSphereView> views) {
-        
+    public void compareAndPersist(final String databaseName, final String schemaName, final Map<String, ShardingSphereView> loadedViews) {
+        // TODO Add ShardingSphereSchemaFactory to support getToBeAddedViews and getToBeDeletedViews.
+        Map<String, ShardingSphereView> currentViews = load(databaseName, schemaName);
+        persist(databaseName, schemaName, getToBeAddedViews(loadedViews, currentViews));
+        getToBeDeletedViews(loadedViews, currentViews).forEach((key, value) -> delete(databaseName, schemaName, key));
     }
     
     @Override
     public void persist(final String databaseName, final String schemaName, final Map<String, ShardingSphereView> views) {
-        
+        views.forEach((key, value) -> repository.persist(DatabaseMetaDataNode.getViewMetaDataPath(databaseName, schemaName, key.toLowerCase()),
+                YamlEngine.marshal(new YamlViewSwapper().swapToYamlConfiguration(value))));
     }
     
     @Override
     public Map<String, ShardingSphereView> load(final String databaseName, final String schemaName) {
-        return Collections.emptyMap();
+        Collection<String> viewNames = repository.getChildrenKeys(DatabaseMetaDataNode.getMetaDataViewsPath(databaseName, schemaName));
+        return viewNames.isEmpty() ? Collections.emptyMap() : getViewMetaDataByViewNames(databaseName, schemaName, viewNames);
     }
     
     @Override
-    public void delete(final String databaseName, final String schemaName, final String name) {
-        
+    public void delete(final String databaseName, final String schemaName, final String viewName) {
+        repository.delete(DatabaseMetaDataNode.getViewMetaDataPath(databaseName, schemaName, viewName.toLowerCase()));
+    }
+    
+    private Map<String, ShardingSphereView> getToBeAddedViews(final Map<String, ShardingSphereView> loadedViews, final Map<String, ShardingSphereView> currentViews) {
+        Map<String, ShardingSphereView> result = new LinkedHashMap<>(loadedViews.size(), 1);
+        for (Map.Entry<String, ShardingSphereView> entry : loadedViews.entrySet()) {
+            ShardingSphereView currentView = currentViews.get(entry.getKey());
+            if (null != currentView && !entry.getValue().equals(currentView)) {
+                result.put(entry.getKey(), entry.getValue());
+            } else if (null == currentView) {
+                result.put(entry.getKey(), entry.getValue());
+            }
+        }
+        return result;
+    }
+    
+    private Map<String, ShardingSphereView> getToBeDeletedViews(final Map<String, ShardingSphereView> loadedViews, final Map<String, ShardingSphereView> currentViews) {
+        return currentViews.entrySet().stream().filter(entry -> !loadedViews.containsKey(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+    }
+    
+    private Map<String, ShardingSphereView> getViewMetaDataByViewNames(final String databaseName, final String schemaName, final Collection<String> viewNames) {
+        Map<String, ShardingSphereView> result = new LinkedHashMap<>(viewNames.size(), 1);
+        viewNames.forEach(each -> {
+            String view = repository.get(DatabaseMetaDataNode.getViewMetaDataPath(databaseName, schemaName, each));
+            if (!StringUtils.isEmpty(view)) {
+                result.put(each.toLowerCase(), new YamlViewSwapper().swapToObject(YamlEngine.unmarshal(view, YamlShardingSphereView.class)));
+            }
+        });
+        return result;
     }
 }
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNodeTest.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNodeTest.java
index 6bf23573083..9c12bb332cd 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNodeTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/node/DatabaseMetaDataNodeTest.java
@@ -57,6 +57,11 @@ public class DatabaseMetaDataNodeTest {
         assertThat(DatabaseMetaDataNode.getMetaDataTablesPath("sharding_db", "sharding_schema"), is("/metadata/sharding_db/schemas/sharding_schema/tables"));
     }
     
+    @Test
+    public void assertGetMetaDataViewsPath() {
+        assertThat(DatabaseMetaDataNode.getMetaDataViewsPath("sharding_db", "sharding_schema"), is("/metadata/sharding_db/schemas/sharding_schema/views"));
+    }
+    
     @Test
     public void assertGetDatabaseNameByDatabasePath() {
         Optional<String> actualSchemaName = DatabaseMetaDataNode.getDatabaseNameByDatabasePath("/metadata/logic_db/schemas/logic_schema");
@@ -107,6 +112,11 @@ public class DatabaseMetaDataNodeTest {
         assertThat(DatabaseMetaDataNode.getTableMetaDataPath("logic_db", "logic_schema", "order"), is("/metadata/logic_db/schemas/logic_schema/tables/order"));
     }
     
+    @Test
+    public void assertGetViewMetaDataPath() {
+        assertThat(DatabaseMetaDataNode.getViewMetaDataPath("logic_db", "logic_schema", "order_view"), is("/metadata/logic_db/schemas/logic_schema/views/order_view"));
+    }
+    
     @Test
     public void assertGetMetaDataNodePath() {
         assertThat(DatabaseMetaDataNode.getMetaDataNodePath(), is("/metadata"));
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java
index 364e2394ec2..fe7d92fb76d 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/DatabaseMetaDataPersistServiceTest.java
@@ -123,6 +123,6 @@ public final class DatabaseMetaDataPersistServiceTest {
     
     @SneakyThrows({IOException.class, URISyntaxException.class})
     private String readYAML() {
-        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/schema.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
+        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/table.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
     }
 }
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/TableMetaDataPersistServiceTest.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/TableMetaDataPersistServiceTest.java
index bd4087a4332..6555c32ccdb 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/TableMetaDataPersistServiceTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/TableMetaDataPersistServiceTest.java
@@ -81,6 +81,6 @@ public final class TableMetaDataPersistServiceTest {
     
     @SneakyThrows({IOException.class, URISyntaxException.class})
     private String readYAML() {
-        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/schema.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
+        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/table.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
     }
 }
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/TableMetaDataPersistServiceTest.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/ViewMetaDataPersistServiceTest.java
similarity index 58%
copy from shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/TableMetaDataPersistServiceTest.java
copy to shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/ViewMetaDataPersistServiceTest.java
index bd4087a4332..de687b69492 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/TableMetaDataPersistServiceTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/metadata/persist/service/config/schema/ViewMetaDataPersistServiceTest.java
@@ -18,8 +18,8 @@
 package org.apache.shardingsphere.mode.metadata.persist.service.config.schema;
 
 import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereTable;
-import org.apache.shardingsphere.mode.metadata.persist.service.schema.TableMetaDataPersistService;
+import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereView;
+import org.apache.shardingsphere.mode.metadata.persist.service.schema.ViewMetaDataPersistService;
 import org.apache.shardingsphere.mode.persist.PersistRepository;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -40,47 +40,48 @@ import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
-public final class TableMetaDataPersistServiceTest {
+public final class ViewMetaDataPersistServiceTest {
     
     @Mock
     private PersistRepository repository;
     
     @Test
     public void assertCompareAndPersist() {
-        TableMetaDataPersistService tableMetaDataPersistService = new TableMetaDataPersistService(repository);
-        when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/tables")).thenReturn(Collections.singletonList("t_order"));
-        when(repository.get("/metadata/foo_db/schemas/foo_schema/tables/t_order")).thenReturn(readYAML());
-        tableMetaDataPersistService.compareAndPersist("foo_db", "foo_schema", Collections.emptyMap());
-        verify(repository).delete("/metadata/foo_db/schemas/foo_schema/tables/t_order");
+        ViewMetaDataPersistService viewMetaDataPersistService = new ViewMetaDataPersistService(repository);
+        when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/views")).thenReturn(Collections.singletonList("foo_view"));
+        when(repository.get("/metadata/foo_db/schemas/foo_schema/views/foo_view")).thenReturn(readYAML());
+        viewMetaDataPersistService.compareAndPersist("foo_db", "foo_schema", Collections.emptyMap());
+        verify(repository).delete("/metadata/foo_db/schemas/foo_schema/views/foo_view");
     }
     
     @Test
     public void assertPersist() {
-        ShardingSphereTable table = new ShardingSphereTable("foo_table", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
-        new TableMetaDataPersistService(repository).persist("foo_db", "foo_schema", Collections.singletonMap("foo_table", table));
-        verify(repository).persist("/metadata/foo_db/schemas/foo_schema/tables/foo_table", "name: foo_table\n");
+        ShardingSphereView view = new ShardingSphereView("foo_view", "select `db`.`db`.`id` AS `id`,`db`.`db`.`order_id` AS `order_id` from `db`.`db`");
+        new ViewMetaDataPersistService(repository).persist("foo_db", "foo_schema", Collections.singletonMap("foo_view", view));
+        verify(repository).persist("/metadata/foo_db/schemas/foo_schema/views/foo_view", "name: foo_view\n"
+                + "viewDefinition: select `db`.`db`.`id` AS `id`,`db`.`db`.`order_id` AS `order_id` from\n"
+                + "  `db`.`db`\n");
     }
     
     @Test
     public void assertLoad() {
-        TableMetaDataPersistService tableMetaDataPersistService = new TableMetaDataPersistService(repository);
-        when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/tables")).thenReturn(Collections.singletonList("t_order"));
-        when(repository.get("/metadata/foo_db/schemas/foo_schema/tables/t_order")).thenReturn(readYAML());
-        Map<String, ShardingSphereTable> tables = tableMetaDataPersistService.load("foo_db", "foo_schema");
-        assertThat(tables.size(), is(1));
-        assertThat(tables.get("t_order").getIndexes().keySet(), is(Collections.singleton("primary")));
-        assertThat(tables.get("t_order").getColumns().size(), is(1));
-        assertThat(tables.get("t_order").getColumns().keySet(), is(Collections.singleton("id")));
+        ViewMetaDataPersistService viewMetaDataPersistService = new ViewMetaDataPersistService(repository);
+        when(repository.getChildrenKeys("/metadata/foo_db/schemas/foo_schema/views")).thenReturn(Collections.singletonList("foo_view"));
+        when(repository.get("/metadata/foo_db/schemas/foo_schema/views/foo_view")).thenReturn(readYAML());
+        Map<String, ShardingSphereView> views = viewMetaDataPersistService.load("foo_db", "foo_schema");
+        assertThat(views.size(), is(1));
+        assertThat(views.get("foo_view").getName(), is("foo_view"));
+        assertThat(views.get("foo_view").getViewDefinition(), is("select `db`.`db`.`id` AS `id`,`db`.`db`.`order_id` AS `order_id` from `db`.`db`"));
     }
     
     @Test
     public void assertDelete() {
-        new TableMetaDataPersistService(repository).delete("foo_db", "foo_schema", "foo_table");
-        verify(repository).delete("/metadata/foo_db/schemas/foo_schema/tables/foo_table");
+        new ViewMetaDataPersistService(repository).delete("foo_db", "foo_schema", "foo_view");
+        verify(repository).delete("/metadata/foo_db/schemas/foo_schema/views/foo_view");
     }
     
     @SneakyThrows({IOException.class, URISyntaxException.class})
     private String readYAML() {
-        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/schema.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
+        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/schema/view.yaml").toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
     }
 }
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/schema.yaml b/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/table.yaml
similarity index 100%
copy from shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/schema.yaml
copy to shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/table.yaml
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/schema.yaml b/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/view.yaml
similarity index 82%
rename from shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/schema.yaml
rename to shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/view.yaml
index 5533aa6daa9..ae82c56e5da 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/schema.yaml
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/schema/view.yaml
@@ -14,14 +14,5 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-name: t_order
-columns:
-  id:
-    caseSensitive: false
-    dataType: 0
-    generated: false
-    name: id
-    primaryKey: true
-indexes:
-   primary:
-      name: PRIMARY
+name: foo_view
+viewDefinition: select `db`.`db`.`id` AS `id`,`db`.`db`.`order_id` AS `order_id` from `db`.`db`