Skip to content

Commit 35b5bfa

Browse files
committed
Refactoring for showUnique templates
1 parent 4ec14f9 commit 35b5bfa

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

server/src/main/java/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,29 +3508,7 @@ else if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYP
35083508
}
35093509
}
35103510

3511-
Integer count = uniqueTmplPair.second();
3512-
if (count.intValue() == 0) {
3513-
// empty result
3514-
return uniqueTmplPair;
3515-
}
3516-
List<TemplateJoinVO> uniqueTmpls = uniqueTmplPair.first();
3517-
int i = 0;
3518-
List<TemplateJoinVO> vrs = null;
3519-
if (showUnique) {
3520-
Long[] tzIds = new Long[uniqueTmpls.size()];
3521-
for (TemplateJoinVO v : uniqueTmpls) {
3522-
tzIds[i++] = v.getId();
3523-
}
3524-
vrs = _templateJoinDao.findByDistinctIds(tzIds);
3525-
} else {
3526-
String[] tzIds = new String[uniqueTmpls.size()];
3527-
for (TemplateJoinVO v : uniqueTmpls) {
3528-
tzIds[i++] = v.getTempZonePair();
3529-
}
3530-
vrs = _templateJoinDao.searchByTemplateZonePair(showRemovedTmpl, tzIds);
3531-
}
3532-
3533-
return new Pair<List<TemplateJoinVO>, Integer>(vrs, count);
3511+
return findTemplatesByIdOrTempZonePair(uniqueTmplPair, showRemovedTmpl, showUnique);
35343512

35353513
// TODO: revisit the special logic for iso search in
35363514
// VMTemplateDaoImpl.searchForTemplates and understand why we need to
@@ -3539,6 +3517,25 @@ else if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYP
35393517

35403518
}
35413519

3520+
// findTemplatesByIdOrTempZonePair returns the templates with the given ids if showUnique is true, or else by the TempZonePair
3521+
private Pair<List<TemplateJoinVO>, Integer> findTemplatesByIdOrTempZonePair(Pair<List<TemplateJoinVO>, Integer> templateDataPair, boolean showRemoved, boolean showUnique) {
3522+
Integer count = templateDataPair.second();
3523+
if (count.intValue() == 0) {
3524+
// empty result
3525+
return templateDataPair;
3526+
}
3527+
List<TemplateJoinVO> templateData = templateDataPair.first();
3528+
List<TemplateJoinVO> templates = null;
3529+
if (showUnique) {
3530+
Long[] templateIds = templateData.stream().map(template -> template.getId()).toArray(Long[]::new);
3531+
templates = _templateJoinDao.findByDistinctIds(templateIds);
3532+
} else {
3533+
String[] templateZonePairs = templateData.stream().map(template -> template.getTempZonePair()).toArray(String[]::new);
3534+
templates = _templateJoinDao.searchByTemplateZonePair(showRemoved, templateZonePairs);
3535+
}
3536+
return new Pair<List<TemplateJoinVO>, Integer>(templates, count);
3537+
}
3538+
35423539
@Override
35433540
public ListResponse<TemplateResponse> listIsos(ListIsosCmd cmd) {
35443541
Pair<List<TemplateJoinVO>, Integer> result = searchForIsosInternal(cmd);

server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public Pair<List<TemplateJoinVO>, Integer> searchIncludingRemovedAndCount(final
490490

491491
@Override
492492
public List<TemplateJoinVO> findByDistinctIds(Long... ids) {
493-
if (ids.length == 0) {
493+
if (ids == null || ids.length == 0) {
494494
return new ArrayList<TemplateJoinVO>();
495495
}
496496
SearchCriteria<TemplateJoinVO> sc = tmpltIdsSearch.create();

0 commit comments

Comments
 (0)