Skip to content

Commit ce43205

Browse files
committed
refactor to use searchbuilder/critera for distinct pair
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent c98ef69 commit ce43205

File tree

6 files changed

+42
-53
lines changed

6 files changed

+42
-53
lines changed

engine/schema/src/main/java/com/cloud/dc/dao/ClusterDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public interface ClusterDao extends GenericDao<ClusterVO, Long> {
3434

3535
List<HypervisorType> getAvailableHypervisorInZone(Long zoneId);
3636

37-
List<Pair<HypervisorType, String>> getDistinctHypervisorsArchAcrossClusters(Long zoneId);
37+
List<Pair<HypervisorType, String>> listDistinctHypervisorsArchAcrossClusters(Long zoneId);
3838

3939
List<ClusterVO> listByDcHyType(long dcId, String hyType);
4040

engine/schema/src/main/java/com/cloud/dc/dao/ClusterDaoImpl.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
2222
import java.util.ArrayList;
23-
import java.util.Collections;
2423
import java.util.HashMap;
2524
import java.util.List;
2625
import java.util.Map;
@@ -170,26 +169,17 @@ public List<HypervisorType> getAvailableHypervisorInZone(Long zoneId) {
170169
}
171170

172171
@Override
173-
public List<Pair<HypervisorType, String>> getDistinctHypervisorsArchAcrossClusters(Long zoneId) {
174-
List<Pair<HypervisorType, String>> hypervisorArchList = new ArrayList<>();
175-
String selectSql = "SELECT DISTINCT hypervisor_type, arch FROM cloud.cluster WHERE removed IS NULL";
176-
if (zoneId != null) {
177-
selectSql += " AND data_center_id=" + zoneId;
178-
}
179-
TransactionLegacy txn = TransactionLegacy.currentTxn();
180-
try {
181-
PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql);
182-
ResultSet rs = stmt.executeQuery();
183-
while (rs.next()) {
184-
HypervisorType hypervisorType = HypervisorType.valueOf(rs.getString("hypervisor_type"));
185-
String arch = rs.getString("arch");
186-
hypervisorArchList.add(new Pair<>(hypervisorType, arch));
187-
}
188-
} catch (SQLException ex) {
189-
logger.error("DB exception {}", ex.getMessage(), ex);
190-
return Collections.emptyList();
191-
}
192-
return hypervisorArchList;
172+
public List<Pair<HypervisorType, String>> listDistinctHypervisorsArchAcrossClusters(Long zoneId) {
173+
SearchBuilder<ClusterVO> sb = createSearchBuilder();
174+
sb.select(null, Func.DISTINCT_PAIR, sb.entity().getHypervisorType(), sb.entity().getArch());
175+
sb.and("zoneId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
176+
sb.done();
177+
SearchCriteria<ClusterVO> sc = sb.create();
178+
sc.setParameters("zoneId", zoneId);
179+
final List<ClusterVO> clusters = search(sc, null);
180+
return clusters.stream()
181+
.map(c -> new Pair<>(c.getHypervisorType(), c.getArch().getType()))
182+
.collect(Collectors.toList());
193183
}
194184

195185
@Override

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.sql.SQLException;
2222
import java.util.ArrayList;
2323
import java.util.Arrays;
24-
import java.util.Collections;
2524
import java.util.Date;
2625
import java.util.HashMap;
2726
import java.util.HashSet;
@@ -1760,25 +1759,18 @@ public List<HypervisorType> listDistinctHypervisorTypes(final Long zoneId) {
17601759

17611760
@Override
17621761
public List<Pair<HypervisorType, CPU.CPUArch>> listDistinctHypervisorArchTypes(final Long zoneId) {
1763-
List<Pair<HypervisorType, CPU.CPUArch>> hypervisorArchList = new ArrayList<>();
1764-
String selectSql = "SELECT DISTINCT hypervisor_type, arch FROM cloud.host WHERE removed IS NULL";
1765-
if (zoneId != null) {
1766-
selectSql += " AND data_center_id=" + zoneId;
1767-
}
1768-
TransactionLegacy txn = TransactionLegacy.currentTxn();
1769-
try {
1770-
PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql);
1771-
ResultSet rs = stmt.executeQuery();
1772-
while (rs.next()) {
1773-
HypervisorType hypervisorType = HypervisorType.valueOf(rs.getString("hypervisor_type"));
1774-
CPU.CPUArch arch = CPU.CPUArch.fromType(rs.getString("arch"));
1775-
hypervisorArchList.add(new Pair<>(hypervisorType, arch));
1776-
}
1777-
} catch (SQLException ex) {
1778-
logger.error("DB exception {}", ex.getMessage(), ex);
1779-
return Collections.emptyList();
1780-
}
1781-
return hypervisorArchList;
1762+
SearchBuilder<HostVO> sb = createSearchBuilder();
1763+
sb.select(null, Func.DISTINCT_PAIR, sb.entity().getHypervisorType(), sb.entity().getArch());
1764+
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
1765+
sb.and("zoneId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
1766+
sb.done();
1767+
SearchCriteria<HostVO> sc = sb.create();
1768+
sc.setParameters("type", Type.Routing);
1769+
sc.setParameters("zoneId", zoneId);
1770+
final List<HostVO> hosts = search(sc, null);
1771+
return hosts.stream()
1772+
.map(h -> new Pair<>(h.getHypervisorType(), h.getArch()))
1773+
.collect(Collectors.toList());
17821774
}
17831775

17841776
@Override

engine/schema/src/main/java/com/cloud/upgrade/SystemVmTemplateRegistration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ protected void registerTemplatesForZone(long zoneId, String filePath) {
841841
String nfsVersion = getNfsVersion(storeUrlAndId.second());
842842
mountStore(storeUrlAndId.first(), filePath, nfsVersion);
843843
List<Pair<Hypervisor.HypervisorType, String>> hypervisorArchList =
844-
clusterDao.getDistinctHypervisorsArchAcrossClusters(zoneId);
844+
clusterDao.listDistinctHypervisorsArchAcrossClusters(zoneId);
845845
for (Pair<Hypervisor.HypervisorType, String> hypervisorArch : hypervisorArchList) {
846846
Hypervisor.HypervisorType hypervisorType = hypervisorArch.first();
847847
MetadataTemplateDetails templateDetails = getMetadataTemplateDetails(hypervisorType,
@@ -946,7 +946,7 @@ public void updateSystemVmTemplates(final Connection conn) {
946946
public void doInTransactionWithoutResult(final TransactionStatus status) {
947947
List<Pair<Hypervisor.HypervisorType, String>> hypervisorsInUse;
948948
try {
949-
hypervisorsInUse = clusterDao.getDistinctHypervisorsArchAcrossClusters(null);
949+
hypervisorsInUse = clusterDao.listDistinctHypervisorsArchAcrossClusters(null);
950950
} catch (final Exception e) {
951951
LOGGER.error("updateSystemVmTemplates: Exception caught while getting hypervisor types from clusters: {}", e.getMessage());
952952
throw new CloudRuntimeException("updateSystemVmTemplates:Exception while getting hypervisor types from clusters", e);

framework/db/src/main/java/com/cloud/utils/db/SearchBase.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import net.sf.cglib.proxy.Factory;
3737
import net.sf.cglib.proxy.MethodInterceptor;
3838
import net.sf.cglib.proxy.MethodProxy;
39+
40+
import org.apache.commons.collections.CollectionUtils;
3941
import org.apache.commons.lang3.StringUtils;
4042

4143
/**
@@ -122,7 +124,7 @@ public J select(final String fieldName, final Func func, final Object field, fin
122124
if (_entity == null) {
123125
throw new RuntimeException("SearchBuilder cannot be modified once it has been setup");
124126
}
125-
if (_specifiedAttrs.size() > 1) {
127+
if (func.getCount() <= 1 && _specifiedAttrs.size() > 1) {
126128
throw new RuntimeException("You can't specify more than one field to search on");
127129
}
128130
if (func.getCount() != -1 && (func.getCount() != (params.length + 1))) {
@@ -150,7 +152,7 @@ public J select(final String fieldName, final Func func, final Object field, fin
150152
}
151153
}
152154

153-
final Select select = new Select(func, _specifiedAttrs.size() == 0 ? null : _specifiedAttrs.get(0), declaredField, params);
155+
final Select select = new Select(func, _specifiedAttrs, declaredField, params);
154156
_selects.add(select);
155157

156158
_specifiedAttrs.clear();
@@ -185,7 +187,7 @@ public J selectFields(final Object... fields) {
185187
} catch (final SecurityException e) {
186188
} catch (final NoSuchFieldException e) {
187189
}
188-
_selects.add(new Select(Func.NATIVE, attr, field, null));
190+
_selects.add(new Select(Func.NATIVE, List.of(attr), field, null));
189191
}
190192

191193
_specifiedAttrs.clear();
@@ -528,16 +530,18 @@ public boolean equals(final Object obj) {
528530

529531
protected static class Select {
530532
public Func func;
531-
public Attribute attr;
533+
public List<Attribute> attributes = new ArrayList<>();
532534
public Object[] params;
533535
public Field field;
534536

535537
protected Select() {
536538
}
537539

538-
public Select(final Func func, final Attribute attr, final Field field, final Object[] params) {
540+
public Select(final Func func, final List<Attribute> attributes, final Field field, final Object[] params) {
539541
this.func = func;
540-
this.attr = attr;
542+
if (CollectionUtils.isNotEmpty(attributes)) {
543+
this.attributes.addAll(attributes);
544+
}
541545
this.params = params;
542546
this.field = field;
543547
}

framework/db/src/main/java/com/cloud/utils/db/SearchCriteria.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import org.apache.commons.collections.CollectionUtils;
2627
import org.apache.commons.lang3.ArrayUtils;
2728

2829
import com.cloud.utils.Pair;
@@ -59,7 +60,7 @@ public int getParams() {
5960
}
6061

6162
public enum Func {
62-
NATIVE("@", 1), MAX("MAX(@)", 1), MIN("MIN(@)", 1), FIRST("FIRST(@)", 1), LAST("LAST(@)", 1), SUM("SUM(@)", 1), COUNT("COUNT(@)", 1), DISTINCT("DISTINCT(@)", 1);
63+
NATIVE("@", 1), MAX("MAX(@)", 1), MIN("MIN(@)", 1), FIRST("FIRST(@)", 1), LAST("LAST(@)", 1), SUM("SUM(@)", 1), COUNT("COUNT(@)", 1), DISTINCT("DISTINCT(@)", 1), DISTINCT_PAIR("DISTINCT @, @", 2);
6364

6465
private String func;
6566
private int count;
@@ -135,10 +136,12 @@ public void getSelect(StringBuilder str, int insertAt) {
135136

136137
for (Select select : _selects) {
137138
String func = select.func.toString() + ",";
138-
if (select.attr == null) {
139+
if (CollectionUtils.isEmpty(select.attributes)) {
139140
func = func.replace("@", "*");
140141
} else {
141-
func = func.replace("@", select.attr.table + "." + select.attr.columnName);
142+
for (Attribute attribute : select.attributes) {
143+
func = func.replaceFirst("@", attribute.table + "." + attribute.columnName);
144+
}
142145
}
143146
str.insert(insertAt, func);
144147
insertAt += func.length();

0 commit comments

Comments
 (0)