Skip to content

Commit

Permalink
Rename ShardingSphereStatisticsNodePath (#34431)
Browse files Browse the repository at this point in the history
* Rename ShardingSphereStatisticsNodePath

* Rename ShardingSphereStatisticsNodePath
  • Loading branch information
terrymanu authored Jan 22, 2025
1 parent 1a4088f commit 46f2844
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereDataNodePath;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereStatisticsNodePath;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void initialize() {

private CoordinatorRegistryCenter createRegistryCenter(final ModeConfiguration modeConfig) {
ClusterPersistRepositoryConfiguration repositoryConfig = (ClusterPersistRepositoryConfiguration) modeConfig.getRepository();
String namespace = repositoryConfig.getNamespace() + ShardingSphereDataNodePath.getJobPath();
String namespace = repositoryConfig.getNamespace() + ShardingSphereStatisticsNodePath.getJobPath();
CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(getZookeeperConfiguration(repositoryConfig, namespace));
result.init();
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData;
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlShardingSphereRowDataSwapper;
import org.apache.shardingsphere.mode.metadata.persist.service.metadata.table.TableRowDataPersistService;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereDataNodePath;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereStatisticsNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.ArrayList;
Expand Down Expand Up @@ -54,7 +54,7 @@ public ShardingSphereDataPersistService(final PersistRepository repository) {
* @return statistics
*/
public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {
Collection<String> databaseNames = repository.getChildrenKeys(ShardingSphereDataNodePath.getDatabasesRootPath());
Collection<String> databaseNames = repository.getChildrenKeys(ShardingSphereStatisticsNodePath.getDatabasesRootPath());
if (databaseNames.isEmpty()) {
return new ShardingSphereStatistics();
}
Expand All @@ -67,15 +67,15 @@ public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {

private ShardingSphereDatabaseData load(final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDatabaseData();
for (String each : repository.getChildrenKeys(ShardingSphereDataNodePath.getSchemaRootPath(database.getName())).stream().filter(database::containsSchema).collect(Collectors.toList())) {
for (String each : repository.getChildrenKeys(ShardingSphereStatisticsNodePath.getSchemaRootPath(database.getName())).stream().filter(database::containsSchema).collect(Collectors.toList())) {
result.putSchema(each, load(database.getName(), database.getSchema(each)));
}
return result;
}

private ShardingSphereSchemaData load(final String databaseName, final ShardingSphereSchema schema) {
ShardingSphereSchemaData result = new ShardingSphereSchemaData();
for (String each : repository.getChildrenKeys(ShardingSphereDataNodePath.getTableRootPath(databaseName, schema.getName())).stream().filter(schema::containsTable)
for (String each : repository.getChildrenKeys(ShardingSphereStatisticsNodePath.getTableRootPath(databaseName, schema.getName())).stream().filter(schema::containsTable)
.collect(Collectors.toList())) {
result.getTableData().put(each, tableRowDataPersistService.load(databaseName, schema.getName(), schema.getTable(each)));

Expand All @@ -98,7 +98,7 @@ public void persist(final ShardingSphereDatabase database, final String schemaNa
}

private void persistSchema(final String databaseName, final String schemaName) {
repository.persist(ShardingSphereDataNodePath.getSchemaPath(databaseName, schemaName), "");
repository.persist(ShardingSphereStatisticsNodePath.getSchemaPath(databaseName, schemaName), "");
}

private void persistTableData(final ShardingSphereDatabase database, final String schemaName, final ShardingSphereSchemaData schemaData) {
Expand Down Expand Up @@ -130,6 +130,6 @@ public void update(final AlteredShardingSphereDatabaseData alteredData) {
* @param databaseName database name
*/
public void delete(final String databaseName) {
repository.delete(ShardingSphereDataNodePath.getDatabasePath(databaseName));
repository.delete(ShardingSphereStatisticsNodePath.getDatabasePath(databaseName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData;
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlShardingSphereRowDataSwapper;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereDataNodePath;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereStatisticsNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.ArrayList;
Expand All @@ -48,9 +48,10 @@ public final class TableRowDataPersistService {
*/
public void persist(final String databaseName, final String schemaName, final String tableName, final Collection<YamlShardingSphereRowData> rows) {
if (rows.isEmpty()) {
repository.persist(ShardingSphereDataNodePath.getTablePath(databaseName, schemaName, tableName.toLowerCase()), "");
repository.persist(ShardingSphereStatisticsNodePath.getTablePath(databaseName, schemaName, tableName.toLowerCase()), "");
} else {
rows.forEach(each -> repository.persist(ShardingSphereDataNodePath.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey()), YamlEngine.marshal(each)));
rows.forEach(
each -> repository.persist(ShardingSphereStatisticsNodePath.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey()), YamlEngine.marshal(each)));
}
}

Expand All @@ -63,7 +64,7 @@ public void persist(final String databaseName, final String schemaName, final St
* @param rows rows
*/
public void delete(final String databaseName, final String schemaName, final String tableName, final Collection<YamlShardingSphereRowData> rows) {
rows.forEach(each -> repository.delete(ShardingSphereDataNodePath.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey())));
rows.forEach(each -> repository.delete(ShardingSphereStatisticsNodePath.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey())));
}

/**
Expand All @@ -77,8 +78,8 @@ public void delete(final String databaseName, final String schemaName, final Str
public ShardingSphereTableData load(final String databaseName, final String schemaName, final ShardingSphereTable table) {
ShardingSphereTableData result = new ShardingSphereTableData(table.getName());
YamlShardingSphereRowDataSwapper swapper = new YamlShardingSphereRowDataSwapper(new ArrayList<>(table.getAllColumns()));
for (String each : repository.getChildrenKeys(ShardingSphereDataNodePath.getTablePath(databaseName, schemaName, table.getName()))) {
String yamlRow = repository.query(ShardingSphereDataNodePath.getTableRowPath(databaseName, schemaName, table.getName(), each));
for (String each : repository.getChildrenKeys(ShardingSphereStatisticsNodePath.getTablePath(databaseName, schemaName, table.getName()))) {
String yamlRow = repository.query(ShardingSphereStatisticsNodePath.getTableRowPath(databaseName, schemaName, table.getName(), each));
if (!Strings.isNullOrEmpty(yamlRow)) {
result.getRows().add(swapper.swapToObject(YamlEngine.unmarshal(yamlRow, YamlShardingSphereRowData.class)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.util.regex.Pattern;

/**
* ShardingSphere data node path.
* ShardingSphere statistics node path.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingSphereDataNodePath {
public final class ShardingSphereStatisticsNodePath {

private static final String ROOT_NODE = "/statistics";

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.mode.node.path;

import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereStatisticsNodePath;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ShardingSphereStatisticsNodePathTest {

@Test
void assertGetDatabasesRootPath() {
assertThat(ShardingSphereStatisticsNodePath.getDatabasesRootPath(), is("/statistics/databases"));
}

@Test
void assertGetDatabasePath() {
assertThat(ShardingSphereStatisticsNodePath.getDatabasePath("foo_db"), is("/statistics/databases/foo_db"));
}

@Test
void assertGetSchemaRootPath() {
assertThat(ShardingSphereStatisticsNodePath.getSchemaRootPath("foo_db"), is("/statistics/databases/foo_db/schemas"));
}

@Test
void assertGetSchemaPath() {
assertThat(ShardingSphereStatisticsNodePath.getSchemaPath("foo_db", "db_schema"), is("/statistics/databases/foo_db/schemas/db_schema"));
}

@Test
void assertGetTableRootPath() {
assertThat(ShardingSphereStatisticsNodePath.getTableRootPath("foo_db", "db_schema"), is("/statistics/databases/foo_db/schemas/db_schema/tables"));
}

@Test
void assertGetTablePath() {
assertThat(ShardingSphereStatisticsNodePath.getTablePath("foo_db", "db_schema", "tbl_name"), is("/statistics/databases/foo_db/schemas/db_schema/tables/tbl_name"));
}

@Test
void assertGetTableRowPath() {
assertThat(ShardingSphereStatisticsNodePath.getTableRowPath("foo_db", "db_schema", "tbl_name", "key"), is("/statistics/databases/foo_db/schemas/db_schema/tables/tbl_name/key"));
}

@Test
void assertFindDatabaseNameWithNotContainsChildPath() {
assertThat(ShardingSphereStatisticsNodePath.findDatabaseName("/statistics/databases/foo_db", false), is(Optional.of("foo_db")));
assertThat(ShardingSphereStatisticsNodePath.findDatabaseName("/statistics/databases", false), is(Optional.empty()));
}

@Test
void assertFindDatabaseNameWithContainsChildPath() {
assertThat(ShardingSphereStatisticsNodePath.findDatabaseName("/statistics/databases/foo_db", true), is(Optional.of("foo_db")));
assertThat(ShardingSphereStatisticsNodePath.findDatabaseName("/statistics/databases/foo_db/schemas/db_schema", true), is(Optional.of("foo_db")));
assertThat(ShardingSphereStatisticsNodePath.findDatabaseName("/statistics/databases", true), is(Optional.empty()));
}

@Test
void assertFindSchemaNameWithNotContainsChildPath() {
assertThat(ShardingSphereStatisticsNodePath.findSchemaName("/statistics/databases/foo_db/schemas/foo_schema", false), is(Optional.of("foo_schema")));
assertThat(ShardingSphereStatisticsNodePath.findSchemaName("/statistics/databases/foo_db", false), is(Optional.empty()));
}

@Test
void assertFindSchemaNameWithContainsChildPath() {
assertThat(ShardingSphereStatisticsNodePath.findSchemaName("/statistics/databases/foo_db/schemas/foo_schema", true), is(Optional.of("foo_schema")));
assertThat(ShardingSphereStatisticsNodePath.findSchemaName("/statistics/databases/foo_db/schemas/foo_schema/tables/foo_tbl", true), is(Optional.of("foo_schema")));
assertThat(ShardingSphereStatisticsNodePath.findSchemaName("/statistics/databases/foo_db", true), is(Optional.empty()));
}

@Test
void assertFindTableNameWithNotContainsChildPath() {
assertThat(ShardingSphereStatisticsNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name", false), is(Optional.of("tbl_name")));
assertThat(ShardingSphereStatisticsNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema", false), is(Optional.empty()));
}

@Test
void assertFindTableNameWithContainsChildPath() {
assertThat(ShardingSphereStatisticsNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name", true), is(Optional.of("tbl_name")));
assertThat(ShardingSphereStatisticsNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name/key", true), is(Optional.of("tbl_name")));
assertThat(ShardingSphereStatisticsNodePath.findTableName("/statistics/databases/foo_db/schemas/foo_schema/tables", true), is(Optional.empty()));
}

@Test
void assertFindRowUniqueKey() {
assertThat(ShardingSphereStatisticsNodePath.findRowUniqueKey("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name/key"), is(Optional.of("key")));
assertThat(ShardingSphereStatisticsNodePath.findRowUniqueKey("/statistics/databases/foo_db/schemas/foo_schema/tables/tbl_name"), is(Optional.empty()));
}

@Test
void assertGetJobPath() {
assertThat(ShardingSphereStatisticsNodePath.getJobPath(), is("/statistics/job"));
}
}
Loading

0 comments on commit 46f2844

Please sign in to comment.