clear code

This commit is contained in:
赵禹光 2019-08-01 15:38:05 +08:00
parent a5b67493e8
commit 9745e87079
3 changed files with 18 additions and 14 deletions

View File

@ -269,10 +269,10 @@ public class ServiceController {
Map<String, List<Service>> services = new HashMap<>(16); Map<String, List<Service>> services = new HashMap<>(16);
if (StringUtils.isNotBlank(namespaceId)) { if (StringUtils.isNotBlank(namespaceId)) {
services.put(namespaceId, serviceManager.searchServices(namespaceId, ".*" + expr + ".*", false)); services.put(namespaceId, serviceManager.searchServices(namespaceId, ".*" + expr + ".*"));
} else { } else {
for (String namespace : serviceManager.getAllNamespaces()) { for (String namespace : serviceManager.getAllNamespaces()) {
services.put(namespace, serviceManager.searchServices(namespace, ".*" + expr + ".*", false)); services.put(namespace, serviceManager.searchServices(namespace, ".*" + expr + ".*"));
} }
} }

View File

@ -46,6 +46,7 @@ import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
/** /**
* Core manager storing all services in Nacos * Core manager storing all services in Nacos
@ -639,21 +640,14 @@ public class ServiceManager implements RecordListener<Service> {
} }
public List<Service> searchServices(String namespaceId, String regex, boolean hasIpCount) { public List<Service> searchServices(String namespaceId, String regex) {
if (StringUtils.isBlank(regex) && !hasIpCount) {
return new ArrayList<>(chooseServiceMap(namespaceId).values());
}
List<Service> result = new ArrayList<>(); List<Service> result = new ArrayList<>();
for (Map.Entry<String, Service> entry : chooseServiceMap(namespaceId).entrySet()) { for (Map.Entry<String, Service> entry : chooseServiceMap(namespaceId).entrySet()) {
Service service = entry.getValue(); Service service = entry.getValue();
String key = service.getName() + ":" + ArrayUtils.toString(service.getOwners()); String key = service.getName() + ":" + ArrayUtils.toString(service.getOwners());
if (StringUtils.isBlank(regex) || !key.matches(regex)) { if (key.matches(regex)) {
continue; result.add(service);
} }
if (hasIpCount && CollectionUtils.isEmpty(service.allIPs())) {
continue;
}
result.add(service);
} }
return result; return result;
@ -683,11 +677,21 @@ public class ServiceManager implements RecordListener<Service> {
public int getPagedService(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List<Service> serviceList, boolean hasIpCount) { public int getPagedService(String namespaceId, int startPage, int pageSize, String keyword, String containedInstance, List<Service> serviceList, boolean hasIpCount) {
List<Service> matchList;
if (chooseServiceMap(namespaceId) == null) { if (chooseServiceMap(namespaceId) == null) {
return 0; return 0;
} }
List<Service> matchList = searchServices(namespaceId, StringUtils.isNotBlank(keyword) ? ".*" + keyword + ".*" : null, hasIpCount); if (StringUtils.isNotBlank(keyword)) {
matchList = searchServices(namespaceId, ".*" + keyword + ".*");
} else {
matchList = new ArrayList<>(chooseServiceMap(namespaceId).values());
}
if (!CollectionUtils.isEmpty(matchList) && hasIpCount) {
matchList = matchList.stream().filter(s -> !CollectionUtils.isEmpty(s.allIPs())).collect(Collectors.toList());
}
if (StringUtils.isNotBlank(containedInstance)) { if (StringUtils.isNotBlank(containedInstance)) {

View File

@ -60,7 +60,7 @@ public class DomainsManagerTest extends BaseTest {
service.setNamespaceId(TEST_NAMESPACE); service.setNamespaceId(TEST_NAMESPACE);
manager.putService(service); manager.putService(service);
List<Service> list = manager.searchServices(TEST_NAMESPACE, "test.*", false); List<Service> list = manager.searchServices(TEST_NAMESPACE, "test.*");
Assert.assertNotNull(list); Assert.assertNotNull(list);
Assert.assertEquals(1, list.size()); Assert.assertEquals(1, list.size());
Assert.assertEquals(TEST_SERVICE_NAME, list.get(0).getName()); Assert.assertEquals(TEST_SERVICE_NAME, list.get(0).getName());