Skip to content

Commit

Permalink
Refactor IndexMetaData's constructor (#33657)
Browse files Browse the repository at this point in the history
* Refactor IndexMetaData's constructor

* Refactor IndexMetaData's constructor

* Refactor IndexMetaData's constructor
  • Loading branch information
terrymanu authored Nov 14, 2024
1 parent b8053db commit d1a9f62
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public Optional<IndexMetaData> revise(final String tableName, final IndexMetaDat
columns.add(each);
}
}
IndexMetaData result = new IndexMetaData(originalMetaData.getName());
result.getColumns().addAll(columns);
IndexMetaData result = new IndexMetaData(originalMetaData.getName(), columns);
result.setUnique(originalMetaData.isUnique());
return Optional.of(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public Optional<IndexMetaData> revise(final String tableName, final IndexMetaDat
if (shardingTable.getActualDataNodes().isEmpty()) {
return Optional.empty();
}
IndexMetaData result = new IndexMetaData(IndexMetaDataUtils.getLogicIndexName(originalMetaData.getName(), shardingTable.getActualDataNodes().iterator().next().getTableName()));
result.getColumns().addAll(originalMetaData.getColumns());
IndexMetaData result = new IndexMetaData(
IndexMetaDataUtils.getLogicIndexName(originalMetaData.getName(), shardingTable.getActualDataNodes().iterator().next().getTableName()), originalMetaData.getColumns());
result.setUnique(originalMetaData.isUnique());
return Optional.of(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@
import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.equalToObject;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -43,6 +42,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class IndexReviseEngineTest<T extends ShardingSphereRule> {

@Mock
Expand All @@ -51,19 +51,14 @@ class IndexReviseEngineTest<T extends ShardingSphereRule> {
@InjectMocks
private IndexReviseEngine<T> indexReviseEngine;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void assertReviseIsPresentIsFalse() {
when(metaDataReviseEntry.getIndexReviser(any(), anyString())).thenReturn(Optional.empty());
Collection<IndexMetaData> indexMetaDataCollection = Collections.singletonList(new IndexMetaData("index"));
Collection<IndexMetaData> actual = indexReviseEngine.revise("tableName", indexMetaDataCollection);
Collection<IndexMetaData> indexMetaDataList = Collections.singletonList(new IndexMetaData("index"));
Collection<IndexMetaData> actual = indexReviseEngine.revise("tableName", indexMetaDataList);
assertNotNull(actual);
assertThat(actual.size(), is(1));
assertThat(actual, equalToObject(indexMetaDataCollection));
assertThat(actual, is(indexMetaDataList));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
Expand Down Expand Up @@ -57,8 +59,7 @@ public static Collection<IndexMetaData> load(final Connection connection, final
continue;
}
if (!result.containsKey(indexName)) {
IndexMetaData indexMetaData = new IndexMetaData(indexName);
indexMetaData.getColumns().add(resultSet.getString("COLUMN_NAME"));
IndexMetaData indexMetaData = new IndexMetaData(indexName, new LinkedList<>(Collections.singleton(resultSet.getString("COLUMN_NAME"))));
indexMetaData.setUnique(!resultSet.getBoolean("NON_UNIQUE"));
result.put(indexName, indexMetaData);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@
*/
@RequiredArgsConstructor
@Getter
@Setter
@EqualsAndHashCode
@ToString
public final class IndexMetaData {

private final String name;

private final Collection<String> columns = new LinkedList<>();
private final Collection<String> columns;

@Setter
private boolean unique;

public IndexMetaData(final String name) {
this(name, new LinkedList<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ private Map<String, Collection<IndexMetaData>> loadIndexMetaData(final DataSourc
if (indexMap.containsKey(indexName)) {
indexMap.get(indexName).getColumns().add(resultSet.getString("COLUMN_NAME"));
} else {
IndexMetaData indexMetaData = new IndexMetaData(indexName);
indexMetaData.getColumns().add(resultSet.getString("COLUMN_NAME"));
IndexMetaData indexMetaData = new IndexMetaData(indexName, new LinkedList<>(Collections.singleton(resultSet.getString("COLUMN_NAME"))));
indexMetaData.setUnique("0".equals(resultSet.getString("NON_UNIQUE")));
indexMap.put(indexName, indexMetaData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ private Map<String, Collection<IndexMetaData>> loadIndexMetaData(final Connectio
if (!result.containsKey(tableName)) {
result.put(tableName, new LinkedList<>());
}
IndexMetaData indexMetaData = new IndexMetaData(indexName);
IndexMetaData indexMetaData = new IndexMetaData(indexName, loadIndexColumnNames(connection, tableName, indexName));
indexMetaData.setUnique(isUnique);
indexMetaData.getColumns().addAll(loadIndexColumnNames(connection, tableName, indexName));
result.get(tableName).add(indexMetaData);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ private Map<String, Collection<IndexMetaData>> loadIndexMetaData(final DataSourc
if (indexMap.containsKey(indexName)) {
indexMap.get(indexName).getColumns().add(resultSet.getString("COLUMN_NAME"));
} else {
IndexMetaData indexMetaData = new IndexMetaData(indexName);
indexMetaData.getColumns().add(resultSet.getString("COLUMN_NAME"));
IndexMetaData indexMetaData = new IndexMetaData(indexName, new LinkedList<>(Collections.singleton(resultSet.getString("COLUMN_NAME"))));
indexMetaData.setUnique("1".equals(resultSet.getString("IS_UNIQUE")));
indexMap.put(indexName, indexMetaData);
}
Expand Down

0 comments on commit d1a9f62

Please sign in to comment.