Skip to content

Commit

Permalink
Refactor ShardingSphereStatisticsBuilder (#34365)
Browse files Browse the repository at this point in the history
* Refactor ShardingSphereStatisticsBuilder

* Refactor ShardingSphereStatisticsBuilder
  • Loading branch information
terrymanu authored Jan 15, 2025
1 parent d39f688 commit 1bc023e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

package org.apache.shardingsphere.infra.metadata.statistics.builder;

import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;

/**
Expand All @@ -31,8 +31,8 @@ public interface ShardingSphereStatisticsBuilder extends DatabaseTypedSPI {
/**
* Build ShardingSphere data.
*
* @param metaData meta data
* @return ShardingSphere statistics
* @param database database
* @return built ShardingSphere data
*/
ShardingSphereStatistics build(ShardingSphereMetaData metaData);
ShardingSphereDatabaseData build(ShardingSphereDatabase database);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
package org.apache.shardingsphere.infra.metadata.statistics.builder.dialect;

import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;

Expand All @@ -43,29 +40,17 @@ public final class MySQLShardingSphereStatisticsBuilder implements ShardingSpher
private static final String SHARDING_TABLE_STATISTICS = "sharding_table_statistics";

@Override
public ShardingSphereStatistics build(final ShardingSphereMetaData metaData) {
ShardingSphereStatistics result = new ShardingSphereStatistics();
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData();
initSchemas(each, databaseData);
if (!databaseData.getSchemaData().isEmpty()) {
result.putDatabase(each.getName(), databaseData);
}
public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDatabaseData();
if (database.containsSchema(SHARDINGSPHERE)) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
initClusterInformationTable(schemaData);
initShardingTableStatisticsTable(schemaData);
result.putSchema(SHARDINGSPHERE, schemaData);
}
return result;
}

private void initSchemas(final ShardingSphereDatabase database, final ShardingSphereDatabaseData databaseData) {
for (ShardingSphereSchema each : database.getAllSchemas()) {
if (SHARDINGSPHERE.equals(each.getName())) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
initClusterInformationTable(schemaData);
initShardingTableStatisticsTable(schemaData);
databaseData.putSchema(SHARDINGSPHERE, schemaData);
}
}
}

private void initClusterInformationTable(final ShardingSphereSchemaData schemaData) {
ShardingSphereTableData tableData = new ShardingSphereTableData(CLUSTER_INFORMATION);
tableData.getRows().add(new ShardingSphereRowData(Collections.singletonList(ShardingSphereVersion.VERSION)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
package org.apache.shardingsphere.infra.metadata.statistics.builder.dialect;

import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;

Expand Down Expand Up @@ -55,32 +53,22 @@ public final class PostgreSQLShardingSphereStatisticsBuilder implements Sharding
}

@Override
public ShardingSphereStatistics build(final ShardingSphereMetaData metaData) {
ShardingSphereStatistics result = new ShardingSphereStatistics();
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
ShardingSphereDatabaseData databaseData = new ShardingSphereDatabaseData();
initSchemas(each, databaseData);
if (!databaseData.getSchemaData().isEmpty()) {
result.putDatabase(each.getName(), databaseData);
}
}
return result;
}

private void initSchemas(final ShardingSphereDatabase database, final ShardingSphereDatabaseData databaseData) {
public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDatabaseData();
if (null != database.getSchema(SHARDINGSPHERE)) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
initClusterInformationTable(schemaData);
initShardingTableStatisticsTable(schemaData);
databaseData.putSchema(SHARDINGSPHERE, schemaData);
result.putSchema(SHARDINGSPHERE, schemaData);
}
for (Entry<String, Collection<String>> entry : INIT_DATA_SCHEMA_TABLES.entrySet()) {
ShardingSphereSchemaData schemaData = new ShardingSphereSchemaData();
if (null != database.getSchema(entry.getKey())) {
initTables(database.getSchema(entry.getKey()), entry.getValue(), schemaData);
databaseData.putSchema(entry.getKey(), schemaData);
result.putSchema(entry.getKey(), schemaData);
}
}
return result;
}

private void initClusterInformationTable(final ShardingSphereSchemaData schemaData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@

package org.apache.shardingsphere.infra.metadata.statistics.builder.dialect;

import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.junit.jupiter.api.Test;

import java.util.Collection;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -36,27 +34,19 @@ class PostgreSQLShardingSphereStatisticsBuilderTest {

@Test
void assertBuild() {
ShardingSphereMetaData metaData = mockMetaData();
ShardingSphereStatistics statistics = new PostgreSQLShardingSphereStatisticsBuilder().build(metaData);
assertTrue(statistics.getDatabaseData().containsKey("logic_db"));
assertTrue(statistics.getDatabaseData().get("logic_db").getSchemaData().containsKey("pg_catalog"));
assertTrue(statistics.getDatabaseData().get("logic_db").getSchemaData().get("pg_catalog").getTableData().containsKey("pg_class"));
ShardingSphereDatabase database = mockDatabase();
ShardingSphereDatabaseData databaseData = new PostgreSQLShardingSphereStatisticsBuilder().build(database);
assertTrue(databaseData.getSchemaData().containsKey("pg_catalog"));
assertTrue(databaseData.getSchemaData().get("pg_catalog").getTableData().containsKey("pg_class"));
}

private ShardingSphereMetaData mockMetaData() {
ShardingSphereMetaData result = mock(ShardingSphereMetaData.class);
Collection<ShardingSphereDatabase> databases = mockDatabases();
when(result.getAllDatabases()).thenReturn(databases);
return result;
}

private Collection<ShardingSphereDatabase> mockDatabases() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("logic_db");
private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(result.getName()).thenReturn("logic_db");
ShardingSphereSchema schema = mockSchema();
when(database.getAllSchemas()).thenReturn(Collections.singleton(schema));
when(database.getSchema("pg_catalog")).thenReturn(schema);
return Collections.singleton(database);
when(result.getAllSchemas()).thenReturn(Collections.singleton(schema));
when(result.getSchema("pg_catalog")).thenReturn(schema);
return result;
}

private ShardingSphereSchema mockSchema() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
Expand Down Expand Up @@ -52,7 +54,7 @@ public static ShardingSphereStatistics create(final MetaDataPersistService persi
if (!statisticsBuilder.isPresent()) {
return new ShardingSphereStatistics();
}
ShardingSphereStatistics builtStatistics = statisticsBuilder.get().build(metaData);
ShardingSphereStatistics builtStatistics = build(metaData, statisticsBuilder.get());
Optional<ShardingSphereStatistics> loadedStatistics = persistService.getShardingSphereDataPersistService().load(metaData);
if (!loadedStatistics.isPresent()) {
return builtStatistics;
Expand All @@ -68,6 +70,17 @@ private static DatabaseType getDatabaseType(final ShardingSphereMetaData metaDat
return dialectDatabaseMetaData.getDefaultSchema().isPresent() ? TypedSPILoader.getService(DatabaseType.class, "PostgreSQL") : protocolType;
}

private static ShardingSphereStatistics build(final ShardingSphereMetaData metaData, final ShardingSphereStatisticsBuilder statisticsBuilder) {
ShardingSphereStatistics result = new ShardingSphereStatistics();
for (ShardingSphereDatabase each : metaData.getAllDatabases()) {
ShardingSphereDatabaseData databaseData = statisticsBuilder.build(each);
if (!databaseData.getSchemaData().isEmpty()) {
result.putDatabase(each.getName(), databaseData);
}
}
return result;
}

private static void putStatisticsIfAbsent(final ShardingSphereStatistics loadedStatistics, final ShardingSphereStatistics builtStatistics) {
loadedStatistics.getDatabaseData().keySet().stream().filter(builtStatistics::containsDatabase).forEach(builtStatistics::dropDatabase);
builtStatistics.getDatabaseData().forEach(loadedStatistics::putDatabase);
Expand Down

0 comments on commit 1bc023e

Please sign in to comment.