use of org.jboss.pnc.spi.datastore.repositories.api.Predicate in project pnc by project-ncl.
the class BuildProviderImpl method getBuilds.
/**
* Returns the page of builds (running or finished) filtered by given BuildPageInfo and predicate.
*/
private Page<Build> getBuilds(BuildPageInfo pageInfo, java.util.function.Predicate<BuildTask> predicate, Predicate<BuildRecord> dbPredicate) {
List<Build> runningBuilds = readRunningBuilds(pageInfo, predicate);
int firstPossibleDBIndex = pageInfo.getPageIndex() * pageInfo.getPageSize() - runningBuilds.size();
int lastPossibleDBIndex = (pageInfo.getPageIndex() + 1) * pageInfo.getPageSize() - 1;
int toSkip = min(runningBuilds.size(), pageInfo.getPageIndex() * pageInfo.getPageSize());
Predicate<BuildRecord>[] predicates = preparePredicates(dbPredicate, pageInfo.getQ(), pageInfo.getBuildConfigName());
Comparator<Build> comparing = Comparator.comparing(Build::getSubmitTime).reversed();
if (!StringUtils.isEmpty(pageInfo.getSort())) {
comparing = rsqlPredicateProducer.getComparator(pageInfo.getSort());
}
SortInfo sortInfo = rsqlPredicateProducer.getSortInfo(type, pageInfo.getSort());
MergeIterator<Build> builds = new MergeIterator(runningBuilds.iterator(), new BuildIterator(firstPossibleDBIndex, lastPossibleDBIndex, pageInfo.getPageSize(), sortInfo, predicates), comparing);
List<Build> resultList = StreamSupport.stream(Spliterators.spliteratorUnknownSize(builds, Spliterator.ORDERED | Spliterator.SORTED), false).skip(toSkip).limit(pageInfo.getPageSize()).collect(Collectors.toList());
int hits = repository.count(predicates) + runningBuilds.size();
return new Page<>(pageInfo.getPageIndex(), pageInfo.getPageSize(), (int) Math.ceil((double) hits / pageInfo.getPageSize()), hits, resultList);
}
use of org.jboss.pnc.spi.datastore.repositories.api.Predicate in project pnc by project-ncl.
the class BuildProviderImpl method getByAttribute.
public Page<Build> getByAttribute(BuildPageInfo buildPageInfo, Map<String, String> attributeConstraints) {
Set<Predicate<BuildRecord>> predicates = new HashSet<>();
for (Map.Entry<String, String> entry : attributeConstraints.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key.startsWith("!")) {
predicates.add(withoutAttribute(key.substring(1)));
} else {
predicates.add(withAttribute(key, value));
}
}
Predicate<BuildRecord> queryPredicate = rsqlPredicateProducer.getCriteriaPredicate(BuildRecord.class, buildPageInfo.getQ());
predicates.add(queryPredicate);
Predicate<BuildRecord>[] predicatesArray = predicates.toArray(new Predicate[predicates.size()]);
PageInfo pageInfo = toPageInfo(buildPageInfo);
SortInfo sortInfo = rsqlPredicateProducer.getSortInfo(type, buildPageInfo.getSort());
List<BuildRecord> resultList = ((BuildRecordRepository) BuildProviderImpl.this.repository).queryWithPredicatesUsingCursor(pageInfo, sortInfo, predicatesArray);
int hits = repository.count(predicatesArray);
return new Page<>(buildPageInfo.getPageIndex(), buildPageInfo.getPageSize(), hits, resultList.stream().map(b -> mapper.toDTO(b)).collect(Collectors.toList()));
}
use of org.jboss.pnc.spi.datastore.repositories.api.Predicate in project pnc by project-ncl.
the class ArtifactProviderImpl method getAllFiltered.
@Override
public Page<ArtifactInfo> getAllFiltered(int pageIndex, int pageSize, Optional<String> identifierPattern, Set<ArtifactQuality> qualities, Optional<RepositoryType> repoType, Set<BuildCategory> buildCategories) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> query = artifactInfoQuery(cb, identifierPattern, qualities, repoType, buildCategories);
int offset = pageIndex * pageSize;
List<ArtifactInfo> artifacts = em.createQuery(query).setMaxResults(pageSize).setFirstResult(offset).getResultList().stream().map(this::mapTupleToArtifactInfo).collect(Collectors.toList());
Predicate<Artifact>[] predicates = getPredicates(identifierPattern, qualities, repoType, buildCategories);
int totalHits = repository.count(predicates);
int totalPages = (totalHits + pageSize - 1) / pageSize;
return new Page<>(pageIndex, pageSize, totalPages, totalHits, artifacts);
}
Aggregations