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);
if (StringUtils.isNotBlank(namespaceId)) {
services.put(namespaceId, serviceManager.searchServices(namespaceId, ".*" + expr + ".*", false));
services.put(namespaceId, serviceManager.searchServices(namespaceId, ".*" + expr + ".*"));
} else {
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.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
/**
* 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) {
if (StringUtils.isBlank(regex) && !hasIpCount) {
return new ArrayList<>(chooseServiceMap(namespaceId).values());
}
public List<Service> searchServices(String namespaceId, String regex) {
List<Service> result = new ArrayList<>();
for (Map.Entry<String, Service> entry : chooseServiceMap(namespaceId).entrySet()) {
Service service = entry.getValue();
String key = service.getName() + ":" + ArrayUtils.toString(service.getOwners());
if (StringUtils.isBlank(regex) || !key.matches(regex)) {
continue;
if (key.matches(regex)) {
result.add(service);
}
if (hasIpCount && CollectionUtils.isEmpty(service.allIPs())) {
continue;
}
result.add(service);
}
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) {
List<Service> matchList;
if (chooseServiceMap(namespaceId) == null) {
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)) {

View File

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