Skip to content

Commit 9c71dae

Browse files
committed
Refactoring for showUnique templates
1 parent c580555 commit 9c71dae

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
@@ -3426,29 +3426,7 @@ else if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYP
34263426
}
34273427
}
34283428

3429-
Integer count = uniqueTmplPair.second();
3430-
if (count.intValue() == 0) {
3431-
// empty result
3432-
return uniqueTmplPair;
3433-
}
3434-
List<TemplateJoinVO> uniqueTmpls = uniqueTmplPair.first();
3435-
int i = 0;
3436-
List<TemplateJoinVO> vrs = null;
3437-
if (showUnique) {
3438-
Long[] tzIds = new Long[uniqueTmpls.size()];
3439-
for (TemplateJoinVO v : uniqueTmpls) {
3440-
tzIds[i++] = v.getId();
3441-
}
3442-
vrs = _templateJoinDao.findByDistinctIds(tzIds);
3443-
} else {
3444-
String[] tzIds = new String[uniqueTmpls.size()];
3445-
for (TemplateJoinVO v : uniqueTmpls) {
3446-
tzIds[i++] = v.getTempZonePair();
3447-
}
3448-
vrs = _templateJoinDao.searchByTemplateZonePair(showRemovedTmpl, tzIds);
3449-
}
3450-
3451-
return new Pair<List<TemplateJoinVO>, Integer>(vrs, count);
3429+
return findTemplatesByIdOrTempZonePair(uniqueTmplPair, showRemovedTmpl, showUnique);
34523430

34533431
// TODO: revisit the special logic for iso search in
34543432
// VMTemplateDaoImpl.searchForTemplates and understand why we need to
@@ -3457,6 +3435,25 @@ else if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYP
34573435

34583436
}
34593437

3438+
// findTemplatesByIdOrTempZonePair returns the templates with the given ids if showUnique is true, or else by the TempZonePair
3439+
private Pair<List<TemplateJoinVO>, Integer> findTemplatesByIdOrTempZonePair(Pair<List<TemplateJoinVO>, Integer> templateDataPair, boolean showRemoved, boolean showUnique) {
3440+
Integer count = templateDataPair.second();
3441+
if (count.intValue() == 0) {
3442+
// empty result
3443+
return templateDataPair;
3444+
}
3445+
List<TemplateJoinVO> templateData = templateDataPair.first();
3446+
List<TemplateJoinVO> templates = null;
3447+
if (showUnique) {
3448+
Long[] templateIds = templateData.stream().map(template -> template.getId()).toArray(Long[]::new);
3449+
templates = _templateJoinDao.findByDistinctIds(templateIds);
3450+
} else {
3451+
String[] templateZonePairs = templateData.stream().map(template -> template.getTempZonePair()).toArray(String[]::new);
3452+
templates = _templateJoinDao.searchByTemplateZonePair(showRemoved, templateZonePairs);
3453+
}
3454+
return new Pair<List<TemplateJoinVO>, Integer>(templates, templateDataPair.second());
3455+
}
3456+
34603457
@Override
34613458
public ListResponse<TemplateResponse> listIsos(ListIsosCmd cmd) {
34623459
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)