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

Use ShardingSphereMetaDataIdentifier on ShardingSphereTable's indexes and constraints #33804

Merged
merged 4 commits into from
Nov 25, 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 @@ -21,6 +21,7 @@
import org.apache.shardingsphere.infra.binder.context.statement.ddl.CreateIndexStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
Expand Down Expand Up @@ -52,9 +53,7 @@ public void preValidate(final ShardingRule shardingRule, final SQLStatementConte
validateTableExist(schema, Collections.singleton(createIndexStatement.getTable()));
String tableName = createIndexStatement.getTable().getTableName().getIdentifier().getValue();
String indexName = createIndexStatementContext.getIndexes().stream().map(each -> each.getIndexName().getIdentifier().getValue()).findFirst().orElse(null);
if (schema.containsIndex(tableName, indexName)) {
throw new DuplicateIndexException(indexName);
}
ShardingSpherePreconditions.checkState(!schema.containsIndex(tableName, indexName), () -> new DuplicateIndexException(indexName));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public boolean containsTable(final String tableName) {
*
* @param tableName table name
* @param indexName index name
* @return whether contains index name or not
* @return contains index name or not
*/
public boolean containsIndex(final String tableName, final String indexName) {
return containsTable(tableName) && getTable(tableName).containsIndex(indexName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* ShardingSphere table.
Expand All @@ -52,9 +54,9 @@ public final class ShardingSphereTable {

private final Map<String, Integer> visibleColumnAndIndexMap = new CaseInsensitiveMap<>();

private final Map<String, ShardingSphereIndex> indexes;
private final Map<ShardingSphereMetaDataIdentifier, ShardingSphereIndex> indexes;

private final Map<String, ShardingSphereConstraint> constraints;
private final Map<ShardingSphereMetaDataIdentifier, ShardingSphereConstraint> constraints;

private final TableType type;

Expand All @@ -77,7 +79,7 @@ public ShardingSphereTable(final String name, final Collection<ShardingSphereCol
}

private Map<ShardingSphereMetaDataIdentifier, ShardingSphereColumn> createColumns(final Collection<ShardingSphereColumn> columns) {
Map<ShardingSphereMetaDataIdentifier, ShardingSphereColumn> result = new CaseInsensitiveMap<>(columns.size(), 1F);
Map<ShardingSphereMetaDataIdentifier, ShardingSphereColumn> result = new LinkedHashMap<>(columns.size(), 1F);
int index = 0;
for (ShardingSphereColumn each : columns) {
result.put(new ShardingSphereMetaDataIdentifier(each.getName()), each);
Expand All @@ -93,20 +95,13 @@ private Map<ShardingSphereMetaDataIdentifier, ShardingSphereColumn> createColumn
return result;
}

private Map<String, ShardingSphereIndex> createIndexes(final Collection<ShardingSphereIndex> indexes) {
Map<String, ShardingSphereIndex> result = new CaseInsensitiveMap<>(indexes.size(), 1F);
for (ShardingSphereIndex each : indexes) {
result.put(each.getName(), each);
}
return result;
private Map<ShardingSphereMetaDataIdentifier, ShardingSphereIndex> createIndexes(final Collection<ShardingSphereIndex> indexes) {
return indexes.stream().collect(Collectors.toMap(each -> new ShardingSphereMetaDataIdentifier(each.getName()), each -> each, (a, b) -> b, () -> new LinkedHashMap<>(indexes.size(), 1F)));
}

private Map<String, ShardingSphereConstraint> createConstraints(final Collection<ShardingSphereConstraint> constraints) {
Map<String, ShardingSphereConstraint> result = new CaseInsensitiveMap<>(constraints.size(), 1F);
for (ShardingSphereConstraint each : constraints) {
result.put(each.getName(), each);
}
return result;
private Map<ShardingSphereMetaDataIdentifier, ShardingSphereConstraint> createConstraints(final Collection<ShardingSphereConstraint> constraints) {
return constraints.stream()
.collect(Collectors.toMap(each -> new ShardingSphereMetaDataIdentifier(each.getName()), each -> each, (a, b) -> b, () -> new LinkedHashMap<>(constraints.size(), 1F)));
}

/**
Expand Down Expand Up @@ -139,50 +134,40 @@ public Collection<ShardingSphereColumn> getAllColumns() {
}

/**
* Put index.
*
* @param index index
*/
public void putIndex(final ShardingSphereIndex index) {
indexes.put(index.getName(), index);
}

/**
* Remove index.
* Judge whether contains index.
*
* @param indexName index name
* @return contains index or not
*/
public void removeIndex(final String indexName) {
indexes.remove(indexName);
public boolean containsIndex(final String indexName) {
return null != indexName && indexes.containsKey(new ShardingSphereMetaDataIdentifier(indexName));
}

/**
* Judge whether contains index.
* Get all indexes.
*
* @param indexName index name
* @return contains index or not
* @return indexes
*/
public boolean containsIndex(final String indexName) {
return null != indexName && indexes.containsKey(indexName);
public Collection<ShardingSphereIndex> getAllIndexes() {
return indexes.values();
}

/**
* Get index.
* Put index.
*
* @param indexName index name
* @return index
* @param index index
*/
public ShardingSphereIndex getIndex(final String indexName) {
return indexes.get(indexName);
public void putIndex(final ShardingSphereIndex index) {
indexes.put(new ShardingSphereMetaDataIdentifier(index.getName()), index);
}

/**
* Get all indexes.
* Remove index.
*
* @return indexes
* @param indexName index name
*/
public Collection<ShardingSphereIndex> getAllIndexes() {
return indexes.values();
public void removeIndex(final String indexName) {
indexes.remove(new ShardingSphereMetaDataIdentifier(indexName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ void assertPutIndex() {
ShardingSphereIndex index1 = new ShardingSphereIndex("foo_idx_1");
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_idx_2");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Arrays.asList(index1, index2), Collections.emptyList());
assertThat(shardingSphereTable.getIndex("foo_idx_1"), is(index1));
assertThat(shardingSphereTable.getIndex("foo_idx_2"), is(index2));
assertNull(shardingSphereTable.getIndex("invalid"));
assertTrue(shardingSphereTable.containsIndex("foo_idx_1"));
assertTrue(shardingSphereTable.containsIndex("foo_idx_2"));
assertFalse(shardingSphereTable.containsIndex("invalid"));
assertThat(shardingSphereTable.getAllIndexes(), hasSize(2));
}

@Test
void assertGetIndex() {
ShardingSphereIndex index = new ShardingSphereIndex("foo_idx");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.singleton(index), Collections.emptyList());
assertThat(shardingSphereTable.getIndex("foo_idx"), is(index));
assertThat(shardingSphereTable.getIndex("FOO_IDX"), is(index));
assertNull(shardingSphereTable.getIndex("invalid"));
assertTrue(shardingSphereTable.containsIndex("foo_idx"));
assertTrue(shardingSphereTable.containsIndex("FOO_IDX"));
assertFalse(shardingSphereTable.containsIndex("invalid"));
}

@Test
Expand All @@ -85,9 +85,9 @@ void assertRemoveIndex() {
ShardingSphereIndex index2 = new ShardingSphereIndex("foo_idx_2");
ShardingSphereTable shardingSphereTable = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Arrays.asList(index1, index2), Collections.emptyList());
shardingSphereTable.removeIndex("foo_idx_1");
assertNull(shardingSphereTable.getIndex("foo_idx_1"));
assertFalse(shardingSphereTable.containsIndex("foo_idx_1"));
shardingSphereTable.removeIndex("invalid");
assertThat(shardingSphereTable.getIndex("foo_idx_2"), is(index2));
assertTrue(shardingSphereTable.containsIndex("foo_idx_2"));
assertThat(shardingSphereTable.getAllIndexes(), hasSize(1));
}

Expand Down Expand Up @@ -115,7 +115,7 @@ void assertGetConstraints() {
ShardingSphereConstraint constraint = new ShardingSphereConstraint("foo_tbl_foreign_key", "foo_tbl");
ShardingSphereTable table = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.singletonList(constraint));
assertThat(table.getAllConstraints(), hasItems(constraint));
assertThat(table.getAllConstraints(), hasSize(1));
assertThat(table.getAllConstraints().size(), is(1));
}

@Test
Expand Down
Loading