diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereDatabaseData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereDatabaseData.java index 4aaa32a97a691..19cdb2cf2f455 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereDatabaseData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereDatabaseData.java @@ -30,42 +30,42 @@ public final class ShardingSphereDatabaseData { private final Map schemaData = new CaseInsensitiveMap<>(); + /** + * Judge whether to contains schema. + * + * @param schemaName schema name + * @return contains schema or not + */ + public boolean containsSchema(final String schemaName) { + return schemaData.containsKey(schemaName); + } + /** * Get ShardingSphere schema data. * * @param schemaName schema name - * @return ShardingSphere schema data + * @return schema data */ public ShardingSphereSchemaData getSchema(final String schemaName) { return schemaData.get(schemaName); } /** - * Put ShardingSphere schema data. + * Put schema data. * * @param schemaName schema name - * @param schema ShardingSphere schema data + * @param schema schema data */ public void putSchema(final String schemaName, final ShardingSphereSchemaData schema) { schemaData.put(schemaName, schema); } /** - * Remove ShardingSphere schema data. + * Remove schema data. * * @param schemaName schema name */ public void removeSchema(final String schemaName) { schemaData.remove(schemaName); } - - /** - * Judge contains ShardingSphere schema from ShardingSphere database or not. - * - * @param schemaName schema name - * @return Contains schema from database or not - */ - public boolean containsSchema(final String schemaName) { - return schemaData.containsKey(schemaName); - } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereDatabaseDataTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereDatabaseDataTest.java new file mode 100644 index 0000000000000..a8696badf14c1 --- /dev/null +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereDatabaseDataTest.java @@ -0,0 +1,59 @@ +/* + * 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.infra.metadata.statistics; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ShardingSphereDatabaseDataTest { + + private final ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData(); + + @BeforeEach + void setUp() { + databaseData.putSchema("foo_schema", new ShardingSphereSchemaData()); + } + + @Test + void assertContainsSchema() { + assertTrue(databaseData.containsSchema("foo_schema")); + assertFalse(databaseData.containsSchema("bar_schema")); + } + + @Test + void assertGetSchema() { + assertTrue(databaseData.getSchema("foo_schema").getTableData().isEmpty()); + assertNull(databaseData.getSchema("bar_schema")); + } + + @Test + void assertPutSchema() { + databaseData.putSchema("bar_schema", new ShardingSphereSchemaData()); + assertTrue(databaseData.containsSchema("bar_schema")); + } + + @Test + void assertRemoveSchema() { + databaseData.removeSchema("foo_schema"); + assertFalse(databaseData.containsSchema("foo_schema")); + } +} diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java index a09d909e55302..f61da68b54ec1 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/data/ShardingSphereDataPersistService.java @@ -69,7 +69,7 @@ public Optional load(final ShardingSphereMetaData meta private ShardingSphereDatabaseData load(final String databaseName, final ShardingSphereDatabase database) { ShardingSphereDatabaseData result = new ShardingSphereDatabaseData(); for (String each : repository.getChildrenKeys(ShardingSphereDataNode.getSchemasPath(databaseName)).stream().filter(database::containsSchema).collect(Collectors.toList())) { - result.getSchemaData().put(each, load(databaseName, each, database.getSchema(each))); + result.putSchema(each, load(databaseName, each, database.getSchema(each))); } return result; }