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 ShardingSphereDatabase, keep method name consistency with ShardingSphereMetaData #28567

Merged
merged 1 commit into from
Sep 24, 2023
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 @@ -127,6 +127,16 @@ private static ResourceMetaData createResourceMetaData(final String databaseName
return new ResourceMetaData(databaseName, storageResource, propsMap);
}

/**
* Judge contains schema from database or not.
*
* @param schemaName schema name
* @return contains schema from database or not
*/
public boolean containsSchema(final String schemaName) {
return schemas.containsKey(schemaName.toLowerCase());
}

/**
* Get schema.
*
Expand All @@ -138,34 +148,24 @@ public ShardingSphereSchema getSchema(final String schemaName) {
}

/**
* Put schema.
* Add schema.
*
* @param schemaName schema name
* @param schema schema
*/
public void putSchema(final String schemaName, final ShardingSphereSchema schema) {
public void addSchema(final String schemaName, final ShardingSphereSchema schema) {
schemas.put(schemaName.toLowerCase(), schema);
}

/**
* Remove schema.
* Drop schema.
*
* @param schemaName schema name
*/
public void removeSchema(final String schemaName) {
public void dropSchema(final String schemaName) {
schemas.remove(schemaName.toLowerCase());
}

/**
* Judge contains schema from database or not.
*
* @param schemaName schema name
* @return contains schema from database or not
*/
public boolean containsSchema(final String schemaName) {
return schemas.containsKey(schemaName.toLowerCase());
}

/**
* Judge whether is completed.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ public void reloadSchema(final String databaseName, final String schemaName, fin
try {
ShardingSphereSchema reloadedSchema = loadSchema(databaseName, schemaName, dataSourceName);
if (reloadedSchema.getTables().isEmpty()) {
metaDataContexts.get().getMetaData().getDatabase(databaseName).removeSchema(schemaName);
metaDataContexts.get().getMetaData().getDatabase(databaseName).dropSchema(schemaName);
metaDataContexts.get().getPersistService().getDatabaseMetaDataService().dropSchema(metaDataContexts.get().getMetaData().getDatabase(databaseName).getName(),
schemaName);
} else {
metaDataContexts.get().getMetaData().getDatabase(databaseName).putSchema(schemaName, reloadedSchema);
metaDataContexts.get().getMetaData().getDatabase(databaseName).addSchema(schemaName, reloadedSchema);
metaDataContexts.get().getPersistService().getDatabaseMetaDataService()
.compareAndPersist(metaDataContexts.get().getMetaData().getDatabase(databaseName).getName(), schemaName, reloadedSchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public synchronized void addSchema(final String databaseName, final String schem
if (database.containsSchema(schemaName)) {
return;
}
database.putSchema(schemaName, new ShardingSphereSchema());
database.addSchema(schemaName, new ShardingSphereSchema());
alterMetaDataHeldRule(database);
}

Expand All @@ -102,7 +102,7 @@ public synchronized void dropSchema(final String databaseName, final String sche
if (!database.containsSchema(schemaName)) {
return;
}
database.removeSchema(schemaName);
database.dropSchema(schemaName);
alterMetaDataHeldRule(database);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ void assertDropNotExistedDatabase() {
@Test
void assertAddSchema() {
contextManager.getResourceMetaDataContextManager().addSchema("foo_db", "bar_schema");
verify(metaDataContexts.getMetaData().getDatabase("foo_db")).putSchema(anyString(), any(ShardingSphereSchema.class));
verify(metaDataContexts.getMetaData().getDatabase("foo_db")).addSchema(anyString(), any(ShardingSphereSchema.class));
}

@Test
void assertAddExistedSchema() {
when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").containsSchema("foo_schema")).thenReturn(true);
contextManager.getResourceMetaDataContextManager().addSchema("foo_db", "foo_schema");
verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).putSchema(anyString(), any(ShardingSphereSchema.class));
verify(metaDataContexts.getMetaData().getDatabase("foo_db"), times(0)).addSchema(anyString(), any(ShardingSphereSchema.class));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ void assertRenewForDatabaseDeleted() {
@Test
void assertRenewForSchemaAdded() {
subscriber.renew(new SchemaAddedEvent("db", "foo_schema"));
verify(contextManager.getMetaDataContexts().getMetaData().getDatabase("db")).putSchema(argThat(argument -> argument.equals("foo_schema")), any(ShardingSphereSchema.class));
verify(contextManager.getMetaDataContexts().getMetaData().getDatabase("db")).addSchema(argThat(argument -> argument.equals("foo_schema")), any(ShardingSphereSchema.class));
}

@Test
void assertRenewForSchemaDeleted() {
when(contextManager.getMetaDataContexts().getMetaData().getDatabase("db").containsSchema("foo_schema")).thenReturn(true);
subscriber.renew(new SchemaDeletedEvent("db", "foo_schema"));
verify(contextManager.getMetaDataContexts().getMetaData().getDatabase("db")).removeSchema("foo_schema");
verify(contextManager.getMetaDataContexts().getMetaData().getDatabase("db")).dropSchema("foo_schema");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void dropDatabase(final String databaseName) {
public void createSchema(final String databaseName, final String schemaName) {
ShardingSphereSchema schema = new ShardingSphereSchema();
ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName);
database.putSchema(schemaName, schema);
database.addSchema(schemaName, schema);
refreshMetaDataHeldRule(database);
contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().persist(databaseName, schemaName, schema);
}
Expand All @@ -97,7 +97,7 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) {

private void putSchemaMetaData(final ShardingSphereDatabase database, final String schemaName, final String renameSchemaName, final String logicDataSourceName) {
ShardingSphereSchema schema = database.getSchema(schemaName);
database.putSchema(renameSchemaName, schema);
database.addSchema(renameSchemaName, schema);
addDataNode(database, logicDataSourceName, schemaName, schema.getAllTableNames());
}

Expand Down Expand Up @@ -141,7 +141,7 @@ private boolean isSingleTable(final String tableName, final ShardingSphereDataba

private void removeSchemaMetaData(final ShardingSphereDatabase database, final String schemaName) {
ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(schemaName).getTables(), database.getSchema(schemaName).getViews());
database.removeSchema(schemaName);
database.dropSchema(schemaName);
removeDataNode(database.getRuleMetaData().findRules(MutableDataNodeRule.class), Collections.singletonList(schemaName), schema.getAllTableNames());
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public void dropSchema(final String databaseName, final Collection<String> schem
ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName);
for (String each : schemaNames) {
ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(each).getTables(), database.getSchema(each).getViews());
database.removeSchema(each);
database.dropSchema(each);
Optional.of(schema).ifPresent(optional -> tobeRemovedTables.addAll(optional.getAllTableNames()));
tobeRemovedSchemas.add(each.toLowerCase());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void dropDatabase(final String databaseName) {
public void createSchema(final String databaseName, final String schemaName) {
ShardingSphereSchema schema = new ShardingSphereSchema();
ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName);
database.putSchema(schemaName, schema);
database.addSchema(schemaName, schema);
refreshMetaDataHeldRule(database);
contextManager.getMetaDataContexts().getPersistService().getDatabaseMetaDataService().persist(databaseName, schemaName, schema);
}
Expand All @@ -94,7 +94,7 @@ public void alterSchema(final AlterSchemaPOJO alterSchemaPOJO) {

private void putSchemaMetaData(final ShardingSphereDatabase database, final String schemaName, final String renameSchemaName, final String logicDataSourceName) {
ShardingSphereSchema schema = database.getSchema(schemaName);
database.putSchema(renameSchemaName, schema);
database.addSchema(renameSchemaName, schema);
addDataNode(database, logicDataSourceName, schemaName, schema.getAllTableNames());
}

Expand Down Expand Up @@ -138,7 +138,7 @@ private boolean isSingleTable(final String tableName, final ShardingSphereDataba

private void removeSchemaMetaData(final ShardingSphereDatabase database, final String schemaName) {
ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(schemaName).getTables(), database.getSchema(schemaName).getViews());
database.removeSchema(schemaName);
database.dropSchema(schemaName);
removeDataNode(database.getRuleMetaData().findRules(MutableDataNodeRule.class), Collections.singleton(schemaName), schema.getAllTableNames());
}

Expand Down Expand Up @@ -174,7 +174,7 @@ public void dropSchema(final String databaseName, final Collection<String> schem
ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName);
for (String each : schemaNames) {
ShardingSphereSchema schema = new ShardingSphereSchema(database.getSchema(each).getTables(), database.getSchema(each).getViews());
database.removeSchema(each);
database.dropSchema(each);
Optional.of(schema).ifPresent(optional -> tobeRemovedTables.addAll(optional.getAllTableNames()));
tobeRemovedSchemas.add(each.toLowerCase());
}
Expand Down