Skip to content

Commit

Permalink
Refactor ShardingSphereSchema.putTable (#33818)
Browse files Browse the repository at this point in the history
* Refactor ShardingSphereSchema.putView

* Refactor ShardingSphereSchema.putTable

* Refactor ShardingSphereSchema.putTable
  • Loading branch information
terrymanu authored Nov 26, 2024
1 parent 7b87b62 commit d6459a5
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ public ShardingSphereView getView(final String viewName) {
/**
* Add table.
*
* @param tableName table name
* @param table table
*/
public void putTable(final String tableName, final ShardingSphereTable table) {
tables.put(tableName.toLowerCase(), table);
public void putTable(final ShardingSphereTable table) {
tables.put(table.getName().toLowerCase(), table);
}

/**
Expand All @@ -99,7 +98,7 @@ public void putTable(final String tableName, final ShardingSphereTable table) {
*/
public void putTables(final Map<String, ShardingSphereTable> tables) {
for (Entry<String, ShardingSphereTable> entry : tables.entrySet()) {
putTable(entry.getKey(), entry.getValue());
putTable(entry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ShardingSphereSchemaTest {

Expand Down Expand Up @@ -55,7 +56,8 @@ void assertGetView() {
void assertPutTable() {
ShardingSphereSchema actual = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, Collections.emptyMap(), Collections.emptyMap());
ShardingSphereTable table = mock(ShardingSphereTable.class);
actual.putTable("tbl", table);
when(table.getName()).thenReturn("tbl");
actual.putTable(table);
assertThat(actual.getTable("tbl"), is(table));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void assertValidate() {
private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME);
schema.putTable("foo_tbl", new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE));
schema.putTable(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE));
when(result.getSchemas()).thenReturn(Collections.singletonMap("foo_schema", schema));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void alterTable(final String databaseName, final String schemaName, fina
if (TableRefreshUtils.isSingleTable(beBoChangedTable.getName(), database)) {
database.reloadRules();
}
database.getSchema(schemaName).putTable(beBoChangedTable.getName(), beBoChangedTable);
database.getSchema(schemaName).putTable(beBoChangedTable);
}

private void alterView(final String databaseName, final String schemaName, final ShardingSphereView beBoChangedView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private ShardingSphereSchema getSchema(final ShardingSphereDatabase database, fi
Map<String, ShardingSphereSchema> schemaMap = GenericSchemaBuilder.build(Collections.singletonList(viewName), material);
Optional<ShardingSphereTable> actualViewMetaData = Optional.ofNullable(schemaMap.get(schemaName)).map(optional -> optional.getTable(viewName));
ShardingSphereSchema result = new ShardingSphereSchema(schemaName);
actualViewMetaData.ifPresent(optional -> result.putTable(viewName, optional));
actualViewMetaData.ifPresent(result::putTable);
result.putView(new ShardingSphereView(viewName, viewDefinition));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void addTablesToDataNode(final ShardingSphereDatabase database, final St
if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) {
database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, entry.getKey()));
}
database.getSchema(schemaName).putTable(entry.getKey(), entry.getValue());
database.getSchema(schemaName).putTable(entry.getValue());
}
}

Expand All @@ -154,7 +154,7 @@ private void addViewsToDataNode(final ShardingSphereDatabase database, final Str
if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(entry.getKey(), database)) {
database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(each -> each.put(logicDataSourceName, schemaName, entry.getKey()));
}
Optional.ofNullable(toBeAddedTables.get(entry.getKey().toLowerCase())).ifPresent(optional -> database.getSchema(schemaName).putTable(entry.getKey(), optional));
Optional.ofNullable(toBeAddedTables.get(entry.getKey().toLowerCase())).ifPresent(optional -> database.getSchema(schemaName).putTable(optional));
database.getSchema(schemaName).putView(entry.getValue());
}
}
Expand Down Expand Up @@ -366,7 +366,7 @@ private void addTableToDataNode(final ShardingSphereDatabase database, final Str
if (!Strings.isNullOrEmpty(logicDataSourceName) && TableRefreshUtils.isSingleTable(table.getName(), database)) {
database.getRuleMetaData().getAttributes(MutableDataNodeRuleAttribute.class).forEach(rule -> rule.put(logicDataSourceName, schemaName, table.getName()));
}
database.getSchema(schemaName).putTable(table.getName(), table);
database.getSchema(schemaName).putTable(table);
}

private void clearServiceCache() {
Expand Down

0 comments on commit d6459a5

Please sign in to comment.