From b9060a932fbf69505eeeda5e96063b3cdac53e11 Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Tue, 8 Dec 2015 17:58:41 +0800 Subject: [PATCH 1/2] remove redundant vs filling --- .../java/com/ctrip/zeus/service/model/AutoFiller.java | 8 +------- .../zeus/service/model/impl/GroupRepositoryImpl.java | 10 ---------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java index b8042534..04ac1815 100644 --- a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java +++ b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java @@ -16,13 +16,7 @@ public class AutoFiller { public void autofill(Group group) throws Exception { for (GroupVirtualServer gvs : group.getGroupVirtualServers()) { - VirtualServer tvs = gvs.getVirtualServer(); - VirtualServer vs = virtualServerRepository.getById(gvs.getVirtualServer().getId()); - tvs.setName(vs.getName()).setSlbId(vs.getSlbId()).setPort(vs.getPort()).setSsl(vs.getSsl()); - tvs.getDomains().clear(); - for (Domain domain : vs.getDomains()) { - tvs.getDomains().add(domain); - } + gvs.setVirtualServer(virtualServerRepository.getById(gvs.getVirtualServer().getId())); if (gvs.getPriority() == null) { if (gvs.getPath().endsWith(RegexRootPath)) gvs.setPriority(Integer.MIN_VALUE); diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java index 0fe68911..28479f40 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java @@ -28,8 +28,6 @@ public class GroupRepositoryImpl implements GroupRepository { @Resource private ArchiveService archiveService; @Resource - private VirtualServerRepository virtualServerRepository; - @Resource private GroupValidator groupModelValidator; @Resource private VGroupValidator vGroupValidator; @@ -40,10 +38,6 @@ public class GroupRepositoryImpl implements GroupRepository { public List list(Long[] ids) throws Exception { List result = archiveService.getLatestGroups(ids); for (Group group : result) { - for (GroupVirtualServer groupVirtualServer : group.getGroupVirtualServers()) { - groupVirtualServer.setVirtualServer( - virtualServerRepository.getById(groupVirtualServer.getVirtualServer().getId())); - } autoFiller.autofill(group); hideVirtualValue(group); } @@ -54,10 +48,6 @@ public class GroupRepositoryImpl implements GroupRepository { public Group getById(Long id) throws Exception { if (groupModelValidator.exists(id) || vGroupValidator.exists(id)) { Group result = archiveService.getLatestGroup(id); - for (GroupVirtualServer groupVirtualServer : result.getGroupVirtualServers()) { - groupVirtualServer.setVirtualServer( - virtualServerRepository.getById(groupVirtualServer.getVirtualServer().getId())); - } autoFiller.autofill(result); hideVirtualValue(result); return result; From 4d256af1c86c876b9b73bd40604309b41e24959c Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Tue, 8 Dec 2015 18:28:25 +0800 Subject: [PATCH 2/2] change vs get strategy when batch get groups --- .../ctrip/zeus/service/model/AutoFiller.java | 6 ++++++ .../model/impl/GroupRepositoryImpl.java | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java index 04ac1815..0f18de4c 100644 --- a/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java +++ b/src/main/java/com/ctrip/zeus/service/model/AutoFiller.java @@ -17,6 +17,12 @@ public class AutoFiller { public void autofill(Group group) throws Exception { for (GroupVirtualServer gvs : group.getGroupVirtualServers()) { gvs.setVirtualServer(virtualServerRepository.getById(gvs.getVirtualServer().getId())); + } + autofillEmptyFields(group); + } + + public void autofillEmptyFields(Group group) { + for (GroupVirtualServer gvs : group.getGroupVirtualServers()) { if (gvs.getPriority() == null) { if (gvs.getPath().endsWith(RegexRootPath)) gvs.setPriority(Integer.MIN_VALUE); diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java index 28479f40..11d634f6 100644 --- a/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java +++ b/src/main/java/com/ctrip/zeus/service/model/impl/GroupRepositoryImpl.java @@ -7,9 +7,13 @@ import com.ctrip.zeus.service.model.handler.GroupSync; import com.ctrip.zeus.service.model.handler.GroupValidator; import com.ctrip.zeus.service.model.handler.VGroupValidator; import com.ctrip.zeus.service.query.GroupCriteriaQuery; +import com.ctrip.zeus.service.query.VirtualServerCriteriaQuery; import com.ctrip.zeus.service.status.StatusService; +import com.google.common.base.Function; +import com.google.common.collect.Maps; import org.springframework.stereotype.Repository; +import javax.annotation.Nullable; import javax.annotation.Resource; import java.util.*; @@ -33,12 +37,27 @@ public class GroupRepositoryImpl implements GroupRepository { private VGroupValidator vGroupValidator; @Resource private StatusService statusService; + @Resource + private VirtualServerRepository virtualServerRepository; + @Resource + private VirtualServerCriteriaQuery virtualServerCriteriaQuery; @Override public List list(Long[] ids) throws Exception { + Set vsIds = virtualServerCriteriaQuery.queryByGroupIds(ids); + Map dic = Maps.uniqueIndex(virtualServerRepository.listAll(vsIds.toArray(new Long[vsIds.size()])), new Function() { + @Nullable + @Override + public Long apply(VirtualServer virtualServer) { + return virtualServer.getId(); + } + }); List result = archiveService.getLatestGroups(ids); for (Group group : result) { - autoFiller.autofill(group); + for (GroupVirtualServer groupVirtualServer : group.getGroupVirtualServers()) { + groupVirtualServer.setVirtualServer(dic.get(groupVirtualServer.getVirtualServer().getId())); + } + autoFiller.autofillEmptyFields(group); hideVirtualValue(group); } return result;