Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ShardingSphereStatisticsFactory #34381

Merged
merged 9 commits into from
Jan 17, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,14 @@ public static ShardingSphereStatistics create(final MetaDataPersistService persi
if (metaData.getAllDatabases().isEmpty()) {
return new ShardingSphereStatistics();
}
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(dialectStatisticsAppender.get(), unloadedDatabases, loadedStatistics);
}

private static ShardingSphereStatistics create(final DialectStatisticsAppender dialectStatisticsAppender,
final Collection<ShardingSphereDatabase> unloadedDatabases, final ShardingSphereStatistics loadedStatistics) {
ShardingSphereStatistics result = new ShardingSphereStatistics();
Optional<DialectStatisticsAppender> dialectStatisticsAppender = DatabaseTypedSPILoader.findService(DialectStatisticsAppender.class, getDatabaseType(metaData));
for (ShardingSphereDatabase each : unloadedDatabases) {
ShardingSphereDatabaseData databaseData = new ShardingSphereDefaultStatisticsBuilder().build(each);
dialectStatisticsAppender.append(databaseData, each);
dialectStatisticsAppender.ifPresent(optional -> optional.append(databaseData, each));
if (!databaseData.getSchemaData().isEmpty()) {
result.putDatabase(each.getName(), databaseData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,24 @@
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Optional;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MetaDataContextsTest {

@Test
void assertGetDefaultMetaData() {
ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), mock(), mock(), Collections.emptyList());
ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties()));
assertThat(new MetaDataContexts(metaData, ShardingSphereStatisticsFactory.create(mock(MetaDataPersistService.class), metaData)).getMetaData().getDatabase("foo_db"), is(database));
MetaDataPersistService persistService = mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS);
when(persistService.getShardingSphereDataPersistService().load(any())).thenReturn(Optional.empty());
assertThat(new MetaDataContexts(metaData, ShardingSphereStatisticsFactory.create(persistService, metaData)).getMetaData().getDatabase("foo_db"), is(database));
}
}
Loading