From 7997f1d8fefd9ae060277a051be35140fa4ffc2e Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 18 Jul 2016 11:26:29 +0800 Subject: [PATCH] add query command components --- .../ctrip/zeus/service/model/IdVersion.java | 9 +- .../service/query/GroupCriteriaQuery.java | 2 + .../zeus/service/query/GroupQueryCommand.java | 72 ++++++++++++ .../zeus/service/query/QueryCommand.java | 19 +++ .../zeus/service/query/SlbCriteriaQuery.java | 2 + .../zeus/service/query/SlbQueryCommand.java | 70 ++++++++++++ .../query/VirtualServerCriteriaQuery.java | 4 + .../zeus/service/query/VsQueryCommand.java | 76 ++++++++++++ .../query/impl/DefaultGroupCriteriaQuery.java | 105 +++++++++++++++++ .../query/impl/DefaultSlbCriteriaQuery.java | 98 +++++++++++++++- .../DefaultVirtualServerCriteriaQuery.java | 108 +++++++++++++++++- 11 files changed, 561 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/ctrip/zeus/service/query/GroupQueryCommand.java create mode 100644 src/main/java/com/ctrip/zeus/service/query/QueryCommand.java create mode 100644 src/main/java/com/ctrip/zeus/service/query/SlbQueryCommand.java create mode 100644 src/main/java/com/ctrip/zeus/service/query/VsQueryCommand.java diff --git a/src/main/java/com/ctrip/zeus/service/model/IdVersion.java b/src/main/java/com/ctrip/zeus/service/model/IdVersion.java index 6ae9586a..be9621d4 100644 --- a/src/main/java/com/ctrip/zeus/service/model/IdVersion.java +++ b/src/main/java/com/ctrip/zeus/service/model/IdVersion.java @@ -13,6 +13,12 @@ public class IdVersion implements Comparable { this.version = version; } + public IdVersion(String stringValue) { + int split = stringValue.indexOf('_'); + this.id = Long.parseLong(stringValue.substring(0, split)); + this.version = Integer.parseInt(stringValue.substring(split + 1, stringValue.length())); + } + public Long getId() { return id; } @@ -45,7 +51,8 @@ public class IdVersion implements Comparable { @Override public int compareTo(IdVersion o) { if (this.id < o.id) return -1; - if (this.id.longValue() == o.id.longValue()) return (this.version - o.version) < 0 ? -1 : ((this.version.intValue() == o.version.intValue()) ? 0 : 1); + if (this.id.longValue() == o.id.longValue()) + return (this.version - o.version) < 0 ? -1 : ((this.version.intValue() == o.version.intValue()) ? 0 : 1); return 1; } diff --git a/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java index 06087eef..009469b0 100644 --- a/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/GroupCriteriaQuery.java @@ -10,6 +10,8 @@ import java.util.Set; */ public interface GroupCriteriaQuery { + IdVersion[] queryByCommand(QueryCommand query, SelectionMode mode) throws Exception; + Long queryByName(String name) throws Exception; Set queryByAppId(String appId) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/query/GroupQueryCommand.java b/src/main/java/com/ctrip/zeus/service/query/GroupQueryCommand.java new file mode 100644 index 00000000..d1f79bc0 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/query/GroupQueryCommand.java @@ -0,0 +1,72 @@ +package com.ctrip.zeus.service.query; + +import com.ctrip.zeus.exceptions.ValidationException; + +/** + * Created by zhoumy on 2016/7/15. + */ +public class GroupQueryCommand implements QueryCommand { + public final int id = 0; + public final int name = 1; + public final int app_id = 2; + public final int member_ip = 3; + public final int vs_id = 4; + + private final String type; + private final String[] values = new String[5]; + + public GroupQueryCommand() { + this.type = "group"; + } + + @Override + public boolean add(String queryName, String queryValue) throws ValidationException { + int idx; + switch (queryName) { + case "id": + case "groupId": + idx = id; + break; + case "name": + case "groupName": + idx = name; + break; + case "appId": + idx = app_id; + break; + case "ip": + case "member": + idx = member_ip; + break; + default: + return false; + } + return addAtIndex(idx, queryValue); + } + + @Override + public boolean addAtIndex(int idx, String queryValue) throws ValidationException { + if (idx < values.length) { + values[idx] = queryValue; + return true; + } else { + return false; + } + } + + @Override + public boolean hasValue(int idx) { + return values[idx] != null; + } + + @Override + public String[] getValue(int idx) { + String value = values[idx]; + return value == null ? null : value.split(","); + } + + @Override + public String getType() { + return type; + } +} diff --git a/src/main/java/com/ctrip/zeus/service/query/QueryCommand.java b/src/main/java/com/ctrip/zeus/service/query/QueryCommand.java new file mode 100644 index 00000000..36d63db7 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/query/QueryCommand.java @@ -0,0 +1,19 @@ +package com.ctrip.zeus.service.query; + +import com.ctrip.zeus.exceptions.ValidationException; + +/** + * Created by zhoumy on 2016/7/15. + */ +public interface QueryCommand { + + boolean add(String queryName, String queryValue) throws ValidationException; + + boolean addAtIndex(int idx, String queryValue) throws ValidationException; + + boolean hasValue(int idx); + + String[] getValue(int idx); + + String getType(); +} diff --git a/src/main/java/com/ctrip/zeus/service/query/SlbCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/SlbCriteriaQuery.java index 6433378a..bb57911e 100644 --- a/src/main/java/com/ctrip/zeus/service/query/SlbCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/SlbCriteriaQuery.java @@ -10,6 +10,8 @@ import java.util.Set; */ public interface SlbCriteriaQuery { + IdVersion[] queryByCommand(QueryCommand query, SelectionMode mode) throws Exception; + Set queryAll() throws Exception; Set queryAll(SelectionMode mode) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/query/SlbQueryCommand.java b/src/main/java/com/ctrip/zeus/service/query/SlbQueryCommand.java new file mode 100644 index 00000000..172916f6 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/query/SlbQueryCommand.java @@ -0,0 +1,70 @@ +package com.ctrip.zeus.service.query; + +import com.ctrip.zeus.exceptions.ValidationException; + +/** + * Created by zhoumy on 2016/7/15. + */ +public class SlbQueryCommand implements QueryCommand { + public final int id = 0; + public final int name = 1; + public final int ip = 2; + public final int vip = 3; + public final int vs_search_key = 4; + + private final String type; + private String[] values = new String[5]; + + public SlbQueryCommand() { + type = "slb"; + } + + @Override + public boolean add(String queryName, String queryValue) throws ValidationException { + int idx; + switch (queryName) { + case "id": + case "slbId": + idx = id; + break; + case "name": + case "slbName": + idx = name; + break; + case "ip": + idx = ip; + break; + case "vip": + return true; + default: + return false; + } + return addAtIndex(idx, queryValue); + } + + @Override + public boolean addAtIndex(int idx, String queryValue) throws ValidationException { + if (idx < values.length) { + values[idx] = queryValue; + return true; + } else { + return false; + } + } + + @Override + public boolean hasValue(int idx) { + return values[idx] != null; + } + + @Override + public String[] getValue(int idx) { + String value = values[idx]; + return value == null ? null : value.split(","); + } + + @Override + public String getType() { + return type; + } +} diff --git a/src/main/java/com/ctrip/zeus/service/query/VirtualServerCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/VirtualServerCriteriaQuery.java index 3509132c..39c315f0 100644 --- a/src/main/java/com/ctrip/zeus/service/query/VirtualServerCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/VirtualServerCriteriaQuery.java @@ -10,6 +10,8 @@ import java.util.Set; */ public interface VirtualServerCriteriaQuery { + IdVersion[] queryByCommand(QueryCommand query, SelectionMode mode) throws Exception; + Set queryAll() throws Exception; Set queryAll(SelectionMode mode) throws Exception; @@ -18,6 +20,8 @@ public interface VirtualServerCriteriaQuery { IdVersion[] queryByIdAndMode(Long vsId, SelectionMode mode) throws Exception; + Set queryByGroup(IdVersion[] searchKeys) throws Exception; + Set queryBySlbId(Long slbId) throws Exception; Set queryBySlbIds(Long[] slbIds) throws Exception; diff --git a/src/main/java/com/ctrip/zeus/service/query/VsQueryCommand.java b/src/main/java/com/ctrip/zeus/service/query/VsQueryCommand.java new file mode 100644 index 00000000..d0de6a26 --- /dev/null +++ b/src/main/java/com/ctrip/zeus/service/query/VsQueryCommand.java @@ -0,0 +1,76 @@ +package com.ctrip.zeus.service.query; + +import com.ctrip.zeus.exceptions.ValidationException; + +/** + * Created by zhoumy on 2016/7/15. + */ +public class VsQueryCommand implements QueryCommand { + public int id = 0; + public int name = 1; + public int domain = 2; + public int ssl = 3; + public int slb_id = 4; + public int group_search_key = 5; + + private String[] values = new String[6]; + private final String type; + + public VsQueryCommand() { + type = "vs"; + } + + @Override + public boolean add(String queryName, String queryValue) throws ValidationException { + int idx; + switch (queryName) { + case "id": + case "vsId": + idx = id; + break; + case "name": + case "vsName": + idx = name; + break; + case "domain": + idx = domain; + break; + case "ssl": + idx = ssl; + break; + // preserved query name + case "ip": + return true; + default: + return false; + } + values[idx] = queryValue; + return true; + } + + @Override + public boolean addAtIndex(int idx, String queryValue) throws ValidationException { + if (idx < values.length) { + values[idx] = queryValue; + return true; + } else { + return false; + } + } + + @Override + public boolean hasValue(int idx) { + return values[idx] != null; + } + + @Override + public String[] getValue(int idx) { + String value = values[idx]; + return value == null ? null : value.split(","); + } + + @Override + public String getType() { + return type; + } +} diff --git a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java index 3e8bab40..34746510 100644 --- a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultGroupCriteriaQuery.java @@ -1,10 +1,14 @@ package com.ctrip.zeus.service.query.impl; import com.ctrip.zeus.dal.core.*; +import com.ctrip.zeus.restful.filter.FilterSet; +import com.ctrip.zeus.restful.filter.QueryExecuter; import com.ctrip.zeus.service.model.SelectionMode; import com.ctrip.zeus.service.model.VersionUtils; import com.ctrip.zeus.service.query.GroupCriteriaQuery; import com.ctrip.zeus.service.model.IdVersion; +import com.ctrip.zeus.service.query.GroupQueryCommand; +import com.ctrip.zeus.service.query.QueryCommand; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -26,6 +30,107 @@ public class DefaultGroupCriteriaQuery implements GroupCriteriaQuery { @Resource private RGroupStatusDao rGroupStatusDao; + @Override + public IdVersion[] queryByCommand(QueryCommand query, final SelectionMode mode) throws Exception { + final GroupQueryCommand groupQuery = (GroupQueryCommand) query; + final Long[] filteredGroupIds = new QueryExecuter.Builder() + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return groupQuery.hasValue(groupQuery.id); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet(); + for (String s : groupQuery.getValue(groupQuery.id)) { + result.add(Long.parseLong(s)); + } + return result; + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return groupQuery.hasValue(groupQuery.name); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet(); + for (String s : groupQuery.getValue(groupQuery.name)) { + result.add(queryByName(s)); + } + return result; + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return groupQuery.hasValue(groupQuery.app_id); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet(); + for (String s : groupQuery.getValue(groupQuery.app_id)) { + result.addAll(queryByAppId(s)); + } + return result; + } + }).build(Long.class).run(); + + IdVersion[] filteredGroupKeys = new QueryExecuter.Builder() + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return filteredGroupIds != null; + } + + @Override + public Set filter() throws Exception { + Set groupIds = new HashSet(); + for (Long i : filteredGroupIds) { + groupIds.add(i); + } + return queryByIdsAndMode(groupIds.toArray(new Long[groupIds.size()]), mode); + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return groupQuery.hasValue(groupQuery.member_ip); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet(); + for (String s : groupQuery.getValue(groupQuery.member_ip)) { + result.addAll(queryByGroupServer(s)); + } + return result; + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return groupQuery.hasValue(groupQuery.vs_id); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet(); + List vsIds = new ArrayList(); + for (String s : groupQuery.getValue(groupQuery.vs_id)) { + vsIds.add(Long.parseLong(s)); + } + return queryByVsIds(vsIds.toArray(new Long[vsIds.size()])); + } + }).build(IdVersion.class).run(); + + return (filteredGroupKeys != null) ? filteredGroupKeys : queryAll(mode).toArray(new IdVersion[0]); + } + @Override public Long queryByName(String name) throws Exception { GroupDo g = groupDao.findByName(name, GroupEntity.READSET_IDONLY); diff --git a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultSlbCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultSlbCriteriaQuery.java index 8fd39737..f6d9bfdb 100644 --- a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultSlbCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultSlbCriteriaQuery.java @@ -1,9 +1,13 @@ package com.ctrip.zeus.service.query.impl; import com.ctrip.zeus.dal.core.*; +import com.ctrip.zeus.restful.filter.FilterSet; +import com.ctrip.zeus.restful.filter.QueryExecuter; import com.ctrip.zeus.service.model.SelectionMode; import com.ctrip.zeus.service.model.IdVersion; import com.ctrip.zeus.service.model.VersionUtils; +import com.ctrip.zeus.service.query.QueryCommand; +import com.ctrip.zeus.service.query.SlbQueryCommand; import com.ctrip.zeus.service.query.SlbCriteriaQuery; import org.springframework.stereotype.Component; @@ -24,6 +28,98 @@ public class DefaultSlbCriteriaQuery implements SlbCriteriaQuery { @Resource private RSlbStatusDao rSlbStatusDao; + @Override + public IdVersion[] queryByCommand(QueryCommand query, final SelectionMode mode) throws Exception { + final SlbQueryCommand slbQuery = (SlbQueryCommand) query; + final Long[] filteredSlbIds = new QueryExecuter.Builder() + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return slbQuery.hasValue(slbQuery.id); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet(); + for (String s : slbQuery.getValue(slbQuery.id)) { + result.add(Long.parseLong(s)); + } + return result; + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return slbQuery.hasValue(slbQuery.name); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet<>(); + for (String s : slbQuery.getValue(slbQuery.name)) { + result.add(queryByName(s)); + } + return result; + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return slbQuery.hasValue(slbQuery.vs_search_key); + } + + @Override + public Set filter() throws Exception { + List searchKey = new ArrayList<>(); + for (String s : slbQuery.getValue(slbQuery.vs_search_key)) { + searchKey.add(new IdVersion(s)); + } + return queryByVses(searchKey.toArray(new IdVersion[searchKey.size()])); + } + }).build(Long.class).run(); + + IdVersion[] filteredSlbKeys = new QueryExecuter.Builder() + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return filteredSlbIds != null || slbQuery.hasValue(slbQuery.id); + } + + @Override + public Set filter() throws Exception { + List slbIds = new ArrayList<>(); + if (slbQuery.hasValue(slbQuery.id)) { + for (String s : slbQuery.getValue(slbQuery.id)) { + slbIds.add(Long.parseLong(s)); + } + } + if (filteredSlbIds != null) { + for (Long i : filteredSlbIds) { + slbIds.add(i); + } + } + return queryByIdsAndMode(slbIds.toArray(new Long[slbIds.size()]), mode); + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return slbQuery.hasValue(slbQuery.ip); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet<>(); + for (String s : slbQuery.getValue(slbQuery.ip)) { + result.addAll(queryBySlbServerIp(s)); + } + return result; + } + }).build(IdVersion.class).run(); + + return (filteredSlbKeys != null) ? filteredSlbKeys : queryAll(mode).toArray(new IdVersion[0]); + } + @Override public Long queryByName(String name) throws Exception { SlbDo s = slbDao.findByName(name, SlbEntity.READSET_IDONLY); @@ -47,7 +143,7 @@ public class DefaultSlbCriteriaQuery implements SlbCriteriaQuery { if (d == null) return new IdVersion[0]; int[] v = VersionUtils.getVersionByMode(mode, d.getOfflineVersion(), d.getOnlineVersion()); - + IdVersion[] result = new IdVersion[v.length]; for (int i = 0; i < result.length && i < v.length; i++) { result[i] = new IdVersion(slbId, v[i]); diff --git a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultVirtualServerCriteriaQuery.java b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultVirtualServerCriteriaQuery.java index b43b3a17..4e390eb7 100644 --- a/src/main/java/com/ctrip/zeus/service/query/impl/DefaultVirtualServerCriteriaQuery.java +++ b/src/main/java/com/ctrip/zeus/service/query/impl/DefaultVirtualServerCriteriaQuery.java @@ -1,15 +1,18 @@ package com.ctrip.zeus.service.query.impl; import com.ctrip.zeus.dal.core.*; +import com.ctrip.zeus.restful.filter.FilterSet; +import com.ctrip.zeus.restful.filter.QueryExecuter; import com.ctrip.zeus.service.model.SelectionMode; import com.ctrip.zeus.service.model.IdVersion; import com.ctrip.zeus.service.model.VersionUtils; +import com.ctrip.zeus.service.query.QueryCommand; import com.ctrip.zeus.service.query.VirtualServerCriteriaQuery; +import com.ctrip.zeus.service.query.VsQueryCommand; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.util.HashSet; -import java.util.Set; +import java.util.*; /** * Created by zhoumy on 2015/9/11. @@ -27,6 +30,91 @@ public class DefaultVirtualServerCriteriaQuery implements VirtualServerCriteriaQ @Resource private RVsStatusDao rVsStatusDao; + @Override + public IdVersion[] queryByCommand(QueryCommand query, final SelectionMode mode) throws Exception { + final VsQueryCommand vsQuery = (VsQueryCommand) query; + final Long[] filteredVsIds = new QueryExecuter.Builder() + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return vsQuery.hasValue(vsQuery.id); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet(); + for (String s : vsQuery.getValue(vsQuery.id)) { + result.add(Long.parseLong(s)); + } + return result; + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return vsQuery.hasValue(vsQuery.group_search_key); + } + + @Override + public Set filter() throws Exception { + List searchKeys = new ArrayList<>(); + for (String s : vsQuery.getValue(vsQuery.group_search_key)) { + searchKeys.add(new IdVersion(s)); + } + return queryByGroup(searchKeys.toArray(new IdVersion[searchKeys.size()])); + } + }).build(Long.class).run(); + + IdVersion[] filteredVsKeys = new QueryExecuter.Builder() + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return filteredVsIds != null; + } + + @Override + public Set filter() throws Exception { + Set vsIds = new HashSet<>(); + for (Long i : filteredVsIds) { + vsIds.add(i); + } + return queryByIdsAndMode(vsIds.toArray(new Long[vsIds.size()]), mode); + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return vsQuery.hasValue(vsQuery.domain); + } + + @Override + public Set filter() throws Exception { + Set result = new HashSet<>(); + for (String s : vsQuery.getValue(vsQuery.domain)) { + result.addAll(queryByDomain(s)); + } + return result; + } + }) + .addFilter(new FilterSet() { + @Override + public boolean shouldFilter() throws Exception { + return vsQuery.hasValue(vsQuery.slb_id); + } + + @Override + public Set filter() throws Exception { + List slbIds = new ArrayList(); + for (String s : vsQuery.getValue(vsQuery.slb_id)) { + slbIds.add(Long.parseLong(s)); + } + return queryBySlbIds(slbIds.toArray(new Long[slbIds.size()])); + } + }).build(IdVersion.class).run(); + + return (filteredVsKeys != null) ? filteredVsKeys : queryAll(mode).toArray(new IdVersion[0]); + } + @Override public Set queryAll() throws Exception { Set result = new HashSet<>(); @@ -73,6 +161,22 @@ public class DefaultVirtualServerCriteriaQuery implements VirtualServerCriteriaQ return result; } + @Override + public Set queryByGroup(IdVersion[] searchKeys) throws Exception { + Set result = new HashSet<>(); + Map map = new HashMap(); + for (IdVersion sk : searchKeys) { + map.put(sk, sk.getId()); + } + for (RelGroupVsDo e : rGroupVsDao.findAllByGroups(map.values().toArray(new Long[map.size()]), RGroupVsEntity.READSET_FULL)) { + if (result.contains(e.getVsId())) + continue; + if (map.keySet().contains(new IdVersion(e.getVsId(), e.getGroupVersion()))) + result.add(e.getVsId()); + } + return result; + } + @Override public Set queryBySlbId(Long slbId) throws Exception { Set result = new HashSet<>();