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 GenericSchemaManager #32919

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,49 +57,49 @@ private static ShardingSphereSchema getToBeAddedTablesBySchema(final ShardingSph
*
* @param reloadTables reload tables
* @param currentTables current tables
* @return To be added table meta data
* @return to be added tables
*/
public static Map<String, ShardingSphereTable> getToBeAddedTables(final Map<String, ShardingSphereTable> reloadTables, final Map<String, ShardingSphereTable> currentTables) {
return reloadTables.entrySet().stream().filter(entry -> !entry.getValue().equals(currentTables.get(entry.getKey()))).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}

/**
* Get to be deleted tables by schemas.
* Get to be dropped tables by schemas.
*
* @param reloadSchemas reload schemas
* @param currentSchemas current schemas
* @return To be deleted table meta data
* @return to be dropped table
*/
public static Map<String, ShardingSphereSchema> getToBeDeletedTablesBySchemas(final Map<String, ShardingSphereSchema> reloadSchemas, final Map<String, ShardingSphereSchema> currentSchemas) {
public static Map<String, ShardingSphereSchema> getToBeDroppedTablesBySchemas(final Map<String, ShardingSphereSchema> reloadSchemas, final Map<String, ShardingSphereSchema> currentSchemas) {
Map<String, ShardingSphereSchema> result = new LinkedHashMap<>(currentSchemas.size(), 1F);
currentSchemas.entrySet().stream().filter(entry -> reloadSchemas.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue))
.forEach((key, value) -> result.put(key, getToBeDeletedTablesBySchema(reloadSchemas.get(key), value)));
.forEach((key, value) -> result.put(key, getToBeDroppedTablesBySchema(reloadSchemas.get(key), value)));
return result;
}

private static ShardingSphereSchema getToBeDeletedTablesBySchema(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) {
return new ShardingSphereSchema(currentSchema.getName(), getToBeDeletedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>());
private static ShardingSphereSchema getToBeDroppedTablesBySchema(final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema) {
return new ShardingSphereSchema(currentSchema.getName(), getToBeDroppedTables(reloadSchema.getTables(), currentSchema.getTables()), new LinkedHashMap<>());
}

/**
* Get to be deleted tables.
* Get to be drop tables.
*
* @param reloadTables reload tables
* @param currentTables current tables
* @return To be deleted table meta data
* @return to be dropped table
*/
public static Map<String, ShardingSphereTable> getToBeDeletedTables(final Map<String, ShardingSphereTable> reloadTables, final Map<String, ShardingSphereTable> currentTables) {
public static Map<String, ShardingSphereTable> getToBeDroppedTables(final Map<String, ShardingSphereTable> reloadTables, final Map<String, ShardingSphereTable> currentTables) {
return currentTables.entrySet().stream().filter(entry -> !reloadTables.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}

/**
* Get to be deleted schema names.
* Get to be dropped schemas.
*
* @param reloadSchemas reload schemas
* @param currentSchemas current schemas
* @return To be deleted schema names
* @return to be dropped schemas
*/
public static Map<String, ShardingSphereSchema> getToBeDeletedSchemaNames(final Map<String, ShardingSphereSchema> reloadSchemas, final Map<String, ShardingSphereSchema> currentSchemas) {
public static Map<String, ShardingSphereSchema> getToBeDroppedSchemas(final Map<String, ShardingSphereSchema> reloadSchemas, final Map<String, ShardingSphereSchema> currentSchemas) {
return currentSchemas.entrySet().stream().filter(entry -> !reloadSchemas.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ void assertGetToBeAddedTablesBySchemas() {
}

@Test
void assertGetToBeDeletedTablesBySchemas() {
void assertGetToBeDroppedTablesBySchemas() {
Map<String, ShardingSphereSchema> currentSchemas = Collections.singletonMap("foo_schema",
new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.singletonMap("foo_table", new ShardingSphereTable("foo_table",
Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyMap()));
Map<String, ShardingSphereSchema> reloadSchemas = Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyMap(), Collections.emptyMap()));
Map<String, ShardingSphereSchema> actual = GenericSchemaManager.getToBeDeletedTablesBySchemas(reloadSchemas, currentSchemas);
Map<String, ShardingSphereSchema> actual = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadSchemas, currentSchemas);
assertThat(actual.size(), is(1));
assertThat(actual.get("foo_schema").getTables().size(), is(1));
assertTrue(actual.get("foo_schema").getTables().containsKey("foo_table"));
Expand All @@ -64,16 +64,16 @@ void assertGetToBeAddedTables() {
}

@Test
void assertGetToBeDeletedTables() {
Map<String, ShardingSphereTable> actual = GenericSchemaManager.getToBeDeletedTables(Collections.emptyMap(), Collections.singletonMap("foo_table", new ShardingSphereTable()));
void assertGetToBeDroppedTables() {
Map<String, ShardingSphereTable> actual = GenericSchemaManager.getToBeDroppedTables(Collections.emptyMap(), Collections.singletonMap("foo_table", new ShardingSphereTable()));
assertThat(actual.size(), is(1));
assertTrue(actual.containsKey("foo_table"));
}

@Test
void assertGetToBeDeletedSchemaNames() {
void assertGetToBeDroppedSchemas() {
Map<String, ShardingSphereSchema> actual =
GenericSchemaManager.getToBeDeletedSchemaNames(Collections.emptyMap(), Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME)));
GenericSchemaManager.getToBeDroppedSchemas(Collections.emptyMap(), Collections.singletonMap("foo_schema", new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME)));
assertThat(actual.size(), is(1));
assertTrue(actual.containsKey("foo_schema"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public Map<String, DataSourceConfiguration> loadDataSourceConfigurations(final S
* @param currentDatabase current database
*/
public void persistReloadDatabaseByAlter(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) {
Map<String, ShardingSphereSchema> toBeDeletedSchemas = GenericSchemaManager.getToBeDeletedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
Map<String, ShardingSphereSchema> toBeDeletedSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
Map<String, ShardingSphereSchema> toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
toBeAddedSchemas.forEach((key, value) -> databaseMetaDataService.persistByAlterConfiguration(databaseName, key, value));
toBeDeletedSchemas.forEach((key, value) -> databaseMetaDataService.delete(databaseName, key, value));
Expand All @@ -163,7 +163,7 @@ public void persistReloadDatabaseByAlter(final String databaseName, final Shardi
* @param currentDatabase current database
*/
public void persistReloadDatabaseByDrop(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) {
Map<String, ShardingSphereSchema> toBeAlterSchemas = GenericSchemaManager.getToBeDeletedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
Map<String, ShardingSphereSchema> toBeAlterSchemas = GenericSchemaManager.getToBeDroppedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
Map<String, ShardingSphereSchema> toBeAddedSchemas = GenericSchemaManager.getToBeAddedTablesBySchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas());
toBeAddedSchemas.forEach((key, value) -> databaseMetaDataService.persistByDropConfiguration(databaseName, key, value));
toBeAlterSchemas.forEach((key, value) -> databaseMetaDataService.delete(databaseName, key, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void compareAndPersist(final String databaseName, final String schemaName
}
Map<String, ShardingSphereTable> currentTables = tableMetaDataPersistService.load(databaseName, schemaName);
tableMetaDataPersistService.persist(databaseName, schemaName, GenericSchemaManager.getToBeAddedTables(schema.getTables(), currentTables));
GenericSchemaManager.getToBeDeletedTables(schema.getTables(), currentTables).forEach((key, value) -> tableMetaDataPersistService.delete(databaseName, schemaName, key));
GenericSchemaManager.getToBeDroppedTables(schema.getTables(), currentTables).forEach((key, value) -> tableMetaDataPersistService.delete(databaseName, schemaName, key));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ public MetaDataContextManager(final AtomicReference<MetaDataContexts> metaDataCo
}

/**
* Delete schema names.
* Drop schemas.
*
* @param databaseName database name
* @param reloadDatabase reload database
* @param currentDatabase current database
*/
public void deletedSchemaNames(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) {
GenericSchemaManager.getToBeDeletedSchemaNames(reloadDatabase.getSchemas(), currentDatabase.getSchemas()).keySet()
.forEach(each -> metaDataPersistService.getDatabaseMetaDataService().dropSchema(databaseName, each));
public void dropSchemas(final String databaseName, final ShardingSphereDatabase reloadDatabase, final ShardingSphereDatabase currentDatabase) {
GenericSchemaManager.getToBeDroppedSchemas(reloadDatabase.getSchemas(), currentDatabase.getSchemas())
.keySet().forEach(each -> metaDataPersistService.getDatabaseMetaDataService().dropSchema(databaseName, each));
}

/**
Expand Down Expand Up @@ -138,7 +138,7 @@ public void forceRefreshDatabaseMetaData(final ShardingSphereDatabase database)
public void refreshTableMetaData(final ShardingSphereDatabase database) {
try {
MetaDataContexts reloadedMetaDataContexts = createMetaDataContexts(database);
deletedSchemaNames(database.getName(), reloadedMetaDataContexts.getMetaData().getDatabase(database.getName()), database);
dropSchemas(database.getName(), reloadedMetaDataContexts.getMetaData().getDatabase(database.getName()), database);
metaDataContexts.set(reloadedMetaDataContexts);
metaDataContexts.get().getMetaData().getDatabase(database.getName()).getSchemas()
.forEach((schemaName, schema) -> metaDataPersistService.getDatabaseMetaDataService().compareAndPersist(database.getName(), schemaName, schema));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void unregisterStorageUnits(final String databaseName, final Collection<S
metaDataContextManager.getMetaDataContexts().get(), metaDataPersistService, metaDataContextManager.getComputeNodeInstanceContext());
metaDataPersistService.persistReloadDatabaseByDrop(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase(databaseName));
metaDataContextManager.deletedSchemaNames(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
metaDataContextManager.dropSchemas(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName),
metaDataContextManager.getMetaDataContexts().get().getMetaData().getDatabase(databaseName));
metaDataContextManager.renewMetaDataContexts(reloadMetaDataContexts);
switchingResource.closeStaleDataSources();
Expand Down