Skip to content

Commit

Permalink
Refactor SchemaMetaDataPersistService (apache#33853)
Browse files Browse the repository at this point in the history
* Refactor SchemaMetaDataPersistService

* Refactor SchemaMetaDataPersistService
  • Loading branch information
terrymanu authored Nov 30, 2024
1 parent 2cb8451 commit a5389f5
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void persistReloadDatabaseByAlter(final String databaseName, final Shardi
public void persistReloadDatabaseByDrop(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) {
Map<String, ShardingSphereSchema> toBeAlterSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
Map<String, ShardingSphereSchema> toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
toBeAddedSchemas.forEach((key, value) -> databaseMetaDataFacade.getSchema().alterByRuleDropped(databaseName, key, value));
toBeAddedSchemas.forEach((key, value) -> databaseMetaDataFacade.getSchema().alterByRuleDropped(databaseName, value));
toBeAlterSchemas.forEach((key, value) -> databaseMetaDataFacade.getTable().drop(databaseName, key, value.getAllTables()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ public void alterByRuleAltered(final String databaseName, final ShardingSphereSc
* Alter schema by rule dropped.
*
* @param databaseName database name
* @param schemaName schema name
* @param schema to be altered schema
*/
public void alterByRuleDropped(final String databaseName, final String schemaName, final ShardingSphereSchema schema) {
tableMetaDataPersistService.persist(databaseName, schemaName, schema.getAllTables());
public void alterByRuleDropped(final String databaseName, final ShardingSphereSchema schema) {
tableMetaDataPersistService.persist(databaseName, schema.getName(), schema.getAllTables());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void assertPersistReloadDatabaseByDrop() {
when(GenericSchemaManager.getToBeDroppedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_deleted", toBeDeletedSchema));
when(GenericSchemaManager.getToBeAddedTablesBySchemas(any(), any())).thenReturn(Collections.singletonMap("to_be_altered", toBeAlterSchema));
metaDataPersistService.persistReloadDatabaseByDrop("foo_db", mock(ShardingSphereDatabase.class), mock(ShardingSphereDatabase.class));
verify(databaseMetaDataFacade.getSchema()).alterByRuleDropped("foo_db", "to_be_altered", toBeAlterSchema);
verify(databaseMetaDataFacade.getSchema()).alterByRuleDropped("foo_db", toBeAlterSchema);
verify(databaseMetaDataFacade.getTable()).drop("foo_db", "to_be_deleted", Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ void assertAlterSchemaByAlterConfigurationWithNotEmptyViewsByRefresh() {

@Test
void assertAlterByRefreshByDropConfiguration() {
persistService.alterByRuleDropped("foo_db", "foo_schema", mock(ShardingSphereSchema.class));
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.getName()).thenReturn("foo_schema");
persistService.alterByRuleDropped("foo_db", schema);
verify(tableMetaDataPersistService).persist(eq("foo_db"), eq("foo_schema"), anyCollection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ private Collection<String> getToBeDroppedResourceNames(final String databaseName
}

private void afterStorageUnitsAltered(final String databaseName, final MetaDataContexts originalMetaDataContexts, final MetaDataContexts reloadMetaDataContexts) {
reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataFacade()
.getSchema().alterByRuleAltered(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schema));
reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().values().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade()
.getSchema().alterByRuleAltered(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), each));
Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName))
.ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> metaDataPersistService.getShardingSphereDataPersistService()
.persist(originalMetaDataContexts.getMetaData().getDatabases().get(databaseName.toLowerCase()), schemaName, schemaData)));
Expand All @@ -190,8 +190,8 @@ private void afterStorageUnitsAltered(final String databaseName, final MetaDataC
}

private void afterStorageUnitsDropped(final String databaseName, final MetaDataContexts originalMetaDataContexts, final MetaDataContexts reloadMetaDataContexts) {
reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataFacade()
.getSchema().alterByRuleDropped(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), schemaName, schema));
reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getSchemas().values().forEach(each -> metaDataPersistService.getDatabaseMetaDataFacade()
.getSchema().alterByRuleDropped(reloadMetaDataContexts.getMetaData().getDatabase(databaseName).getName(), each));
Optional.ofNullable(reloadMetaDataContexts.getStatistics().getDatabaseData().get(databaseName))
.ifPresent(optional -> optional.getSchemaData().forEach((schemaName, schemaData) -> metaDataPersistService.getShardingSphereDataPersistService()
.persist(originalMetaDataContexts.getMetaData().getDatabases().get(databaseName.toLowerCase()), schemaName, schemaData)));
Expand Down

0 comments on commit a5389f5

Please sign in to comment.