Skip to content

Commit

Permalink
Refactor ShowTablesExecutor, fix sort without like filter (#31305)
Browse files Browse the repository at this point in the history
* Refactor ShowTablesExecutor, fix sort without like filter

* Fix StateChangedSubscriberTest
  • Loading branch information
RaigorJiang authored May 19, 2024
1 parent 0eabc08 commit 6cf5e7e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterLockDeletedEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.cluster.event.ClusterStateEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.ComputeNodeInstanceStateChangedEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.InstanceOfflineEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.InstanceOnlineEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.LabelsEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.ComputeNodeInstanceStateChangedEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.WorkerIdEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.storage.event.StorageNodeChangedEvent;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
Expand All @@ -64,6 +64,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -166,14 +167,14 @@ void assertRenewInstanceOnlineEvent() {
InstanceOnlineEvent instanceOnlineEvent1 = new InstanceOnlineEvent(instanceMetaData1);
subscriber.renew(instanceOnlineEvent1);
assertThat(contextManager.getInstanceContext().getAllClusterComputeNodeInstances().size(), is(1));
assertThat(((LinkedList<ComputeNodeInstance>) contextManager.getInstanceContext().getAllClusterComputeNodeInstances()).get(0).getMetaData(), is(instanceMetaData1));
assertThat(((CopyOnWriteArrayList<ComputeNodeInstance>) contextManager.getInstanceContext().getAllClusterComputeNodeInstances()).get(0).getMetaData(), is(instanceMetaData1));
InstanceMetaData instanceMetaData2 = new ProxyInstanceMetaData("foo_instance_3308", 3308);
InstanceOnlineEvent instanceOnlineEvent2 = new InstanceOnlineEvent(instanceMetaData2);
subscriber.renew(instanceOnlineEvent2);
assertThat(contextManager.getInstanceContext().getAllClusterComputeNodeInstances().size(), is(2));
assertThat(((LinkedList<ComputeNodeInstance>) contextManager.getInstanceContext().getAllClusterComputeNodeInstances()).get(1).getMetaData(), is(instanceMetaData2));
assertThat(((CopyOnWriteArrayList<ComputeNodeInstance>) contextManager.getInstanceContext().getAllClusterComputeNodeInstances()).get(1).getMetaData(), is(instanceMetaData2));
subscriber.renew(instanceOnlineEvent1);
assertThat(contextManager.getInstanceContext().getAllClusterComputeNodeInstances().size(), is(2));
assertThat(((LinkedList<ComputeNodeInstance>) contextManager.getInstanceContext().getAllClusterComputeNodeInstances()).get(1).getMetaData(), is(instanceMetaData1));
assertThat(((CopyOnWriteArrayList<ComputeNodeInstance>) contextManager.getInstanceContext().getAllClusterComputeNodeInstances()).get(1).getMetaData(), is(instanceMetaData1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,13 @@ private QueryResult getQueryResult(final String databaseName) {

private Collection<ShardingSphereTable> getTables(final String databaseName) {
Collection<ShardingSphereTable> tables = ProxyContext.getInstance().getContextManager().getDatabase(databaseName).getSchema(databaseName).getTables().values();
Collection<ShardingSphereTable> filteredTables = filterByLike(tables);
return filteredTables.stream().sorted(Comparator.comparing(ShardingSphereTable::getName)).collect(Collectors.toList());
}

private Collection<ShardingSphereTable> filterByLike(final Collection<ShardingSphereTable> tables) {
Optional<Pattern> likePattern = getLikePattern();
return likePattern.isPresent()
? tables.stream().filter(each -> likePattern.get().matcher(each.getName()).matches()).sorted(Comparator.comparing(ShardingSphereTable::getName)).collect(Collectors.toList())
: tables;
return likePattern.isPresent() ? tables.stream().filter(each -> likePattern.get().matcher(each.getName()).matches()).collect(Collectors.toList()) : tables;
}

private Optional<Pattern> getLikePattern() {
Expand Down

0 comments on commit 6cf5e7e

Please sign in to comment.