Skip to content

Commit

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

* Refactor ShardingSphereStatisticsBuilder

* Refactor ShardingSphereStatisticsBuilder
  • Loading branch information
terrymanu authored Jan 16, 2025
1 parent c2fcf41 commit f753113
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;

/**
* ShardingSphere statistics builder.
* Dialect statistics appender.
*/
@SingletonSPI
public interface ShardingSphereStatisticsBuilder extends DatabaseTypedSPI {
public interface DialectStatisticsAppender extends DatabaseTypedSPI {

/**
* Build ShardingSphere data.
* Append database data.
*
* @param databaseData to be appended database data
* @param database database
* @return built ShardingSphere data
*/
ShardingSphereDatabaseData build(ShardingSphereDatabase database);
void append(ShardingSphereDatabaseData databaseData, ShardingSphereDatabase database);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@

import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;
import org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender;

/**
* ShardingSphere statistics builder for MySQL.
* Statistics appender for MySQL.
*/
public final class MySQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder {
public final class MySQLStatisticsAppender implements DialectStatisticsAppender {

@Override
public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
return new ShardingSphereDefaultStatisticsBuilder().build(database);
public void append(final ShardingSphereDatabaseData databaseData, final ShardingSphereDatabase database) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder;
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsBuilder;
import org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -33,9 +32,9 @@
import java.util.Map.Entry;

/**
* ShardingSphere statistics builder for PostgreSQL.
* Statistics appender for PostgreSQL.
*/
public final class PostgreSQLShardingSphereStatisticsBuilder implements ShardingSphereStatisticsBuilder {
public final class PostgreSQLStatisticsAppender implements DialectStatisticsAppender {

private static final Map<String, Collection<String>> INIT_DATA_SCHEMA_TABLES = new LinkedHashMap<>();

Expand All @@ -44,16 +43,14 @@ public final class PostgreSQLShardingSphereStatisticsBuilder implements Sharding
}

@Override
public ShardingSphereDatabaseData build(final ShardingSphereDatabase database) {
ShardingSphereDatabaseData result = new ShardingSphereDefaultStatisticsBuilder().build(database);
public void append(final ShardingSphereDatabaseData databaseData, final ShardingSphereDatabase database) {
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);
result.putSchema(entry.getKey(), schemaData);
databaseData.putSchema(entry.getKey(), schemaData);
}
}
return result;
}

private void initTables(final ShardingSphereSchema schema, final Collection<String> tables, final ShardingSphereSchemaData schemaData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
# limitations under the License.
#

org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.MySQLShardingSphereStatisticsBuilder
org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.PostgreSQLShardingSphereStatisticsBuilder
org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.MySQLStatisticsAppender
org.apache.shardingsphere.infra.metadata.statistics.builder.dialect.PostgreSQLStatisticsAppender

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.builder.ShardingSphereDefaultStatisticsBuilder;
import org.junit.jupiter.api.Test;

import java.util.Collections;
Expand All @@ -30,12 +31,13 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class PostgreSQLShardingSphereStatisticsBuilderTest {
class PostgreSQLStatisticsAppenderTest {

@Test
void assertBuild() {
void assertAppend() {
ShardingSphereDatabase database = mockDatabase();
ShardingSphereDatabaseData databaseData = new PostgreSQLShardingSphereStatisticsBuilder().build(database);
ShardingSphereDatabaseData databaseData = new ShardingSphereDefaultStatisticsBuilder().build(database);
new PostgreSQLStatisticsAppender().append(databaseData, database);
assertTrue(databaseData.getSchemaData().containsKey("pg_catalog"));
assertTrue(databaseData.getSchemaData().get("pg_catalog").getTableData().containsKey("pg_class"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import java.util.Map;

/**
* Meta data context manager..
* Meta data context manager.
*/
@Getter
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
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.metadata.statistics.builder.ShardingSphereDefaultStatisticsBuilder;
import org.apache.shardingsphere.infra.metadata.statistics.builder.DialectStatisticsAppender;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;

Expand All @@ -52,20 +53,21 @@ public static ShardingSphereStatistics create(final MetaDataPersistService persi
if (metaData.getAllDatabases().isEmpty()) {
return new ShardingSphereStatistics();
}
Optional<ShardingSphereStatisticsBuilder> statisticsBuilder = DatabaseTypedSPILoader.findService(ShardingSphereStatisticsBuilder.class, getDatabaseType(metaData));
if (!statisticsBuilder.isPresent()) {
Optional<DialectStatisticsAppender> dialectStatisticsAppender = DatabaseTypedSPILoader.findService(DialectStatisticsAppender.class, getDatabaseType(metaData));
if (!dialectStatisticsAppender.isPresent()) {
return new ShardingSphereStatistics();
}
ShardingSphereStatistics loadedStatistics = persistService.getShardingSphereDataPersistService().load(metaData).orElse(new ShardingSphereStatistics());
Collection<ShardingSphereDatabase> unloadedDatabases = metaData.getAllDatabases().stream().filter(each -> !loadedStatistics.containsDatabase(each.getName())).collect(Collectors.toList());
return create(statisticsBuilder.get(), unloadedDatabases, loadedStatistics);
return create(dialectStatisticsAppender.get(), unloadedDatabases, loadedStatistics);
}

private static ShardingSphereStatistics create(final ShardingSphereStatisticsBuilder statisticsBuilder,
private static ShardingSphereStatistics create(final DialectStatisticsAppender statisticsBuilder,
final Collection<ShardingSphereDatabase> unloadedDatabases, final ShardingSphereStatistics loadedStatistics) {
ShardingSphereStatistics result = new ShardingSphereStatistics();
for (ShardingSphereDatabase each : unloadedDatabases) {
ShardingSphereDatabaseData databaseData = statisticsBuilder.build(each);
ShardingSphereDatabaseData databaseData = new ShardingSphereDefaultStatisticsBuilder().build(each);
statisticsBuilder.append(databaseData, each);
if (!databaseData.getSchemaData().isEmpty()) {
result.putDatabase(each.getName(), databaseData);
}
Expand Down

0 comments on commit f753113

Please sign in to comment.