Skip to content

Commit

Permalink
Refactor StatisticsNodePath (#34558)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Feb 5, 2025
1 parent 4697964 commit 9948a73
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 139 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.ShardingSphereStatisticsNodePath;
import org.apache.shardingsphere.mode.node.path.metadata.StatisticsNodePath;
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() + ShardingSphereStatisticsNodePath.getJobPath();
String namespace = repositoryConfig.getNamespace() + StatisticsNodePath.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 @@ -24,7 +24,7 @@
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.data.pojo.YamlRowStatistics;
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereStatisticsNodePath;
import org.apache.shardingsphere.mode.node.path.metadata.StatisticsNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

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

Expand All @@ -64,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<YamlRowStatistics> rows) {
rows.forEach(each -> repository.delete(ShardingSphereStatisticsNodePath.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey())));
rows.forEach(each -> repository.delete(StatisticsNodePath.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey())));
}

/**
Expand All @@ -78,8 +78,8 @@ public void delete(final String databaseName, final String schemaName, final Str
public TableStatistics load(final String databaseName, final String schemaName, final ShardingSphereTable table) {
TableStatistics result = new TableStatistics(table.getName());
YamlRowStatisticsSwapper swapper = new YamlRowStatisticsSwapper(new ArrayList<>(table.getAllColumns()));
for (String each : repository.getChildrenKeys(ShardingSphereStatisticsNodePath.getTablePath(databaseName, schemaName, table.getName()))) {
String yamlRow = repository.query(ShardingSphereStatisticsNodePath.getTableRowPath(databaseName, schemaName, table.getName(), each));
for (String each : repository.getChildrenKeys(StatisticsNodePath.getTablePath(databaseName, schemaName, table.getName()))) {
String yamlRow = repository.query(StatisticsNodePath.getTableRowPath(databaseName, schemaName, table.getName(), each));
if (!Strings.isNullOrEmpty(yamlRow)) {
result.getRows().add(swapper.swapToObject(YamlEngine.unmarshal(yamlRow, YamlRowStatistics.class)));
}
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.YamlRowStatistics;
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
import org.apache.shardingsphere.mode.metadata.persist.metadata.service.TableRowDataPersistService;
import org.apache.shardingsphere.mode.node.path.metadata.ShardingSphereStatisticsNodePath;
import org.apache.shardingsphere.mode.node.path.metadata.StatisticsNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

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

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

private SchemaStatistics load(final String databaseName, final ShardingSphereSchema schema) {
SchemaStatistics result = new SchemaStatistics();
for (String each : repository.getChildrenKeys(ShardingSphereStatisticsNodePath.getTableRootPath(databaseName, schema.getName())).stream().filter(schema::containsTable)
for (String each : repository.getChildrenKeys(StatisticsNodePath.getTableRootPath(databaseName, schema.getName())).stream().filter(schema::containsTable)
.collect(Collectors.toList())) {
result.putTableStatistics(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(ShardingSphereStatisticsNodePath.getSchemaPath(databaseName, schemaName), "");
repository.persist(StatisticsNodePath.getSchemaPath(databaseName, schemaName), "");
}

private void persistTableData(final ShardingSphereDatabase database, final String schemaName, final SchemaStatistics schemaStatistics) {
Expand Down Expand Up @@ -133,6 +133,6 @@ public void update(final AlteredDatabaseStatistics alteredDatabaseStatistics) {
* @param databaseName database name
*/
public void delete(final String databaseName) {
repository.delete(ShardingSphereStatisticsNodePath.getDatabasePath(databaseName));
repository.delete(StatisticsNodePath.getDatabasePath(databaseName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import java.util.regex.Pattern;

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

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.StatisticsNodePath;
import org.junit.jupiter.api.Test;

import java.util.Optional;

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

class StatisticsNodePathTest {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 9948a73

Please sign in to comment.