Search in sources :

Example 1 with MergeIterator

use of org.jboss.pnc.facade.util.MergeIterator 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);
}
Also used : BuildRecordPredicates.temporaryBuild(org.jboss.pnc.spi.datastore.predicates.BuildRecordPredicates.temporaryBuild) Build(org.jboss.pnc.dto.Build) Page(org.jboss.pnc.dto.response.Page) Predicate(org.jboss.pnc.spi.datastore.repositories.api.Predicate) SortInfo(org.jboss.pnc.spi.datastore.repositories.api.SortInfo) MergeIterator(org.jboss.pnc.facade.util.MergeIterator)

Aggregations

Build (org.jboss.pnc.dto.Build)1 Page (org.jboss.pnc.dto.response.Page)1 MergeIterator (org.jboss.pnc.facade.util.MergeIterator)1 BuildRecordPredicates.temporaryBuild (org.jboss.pnc.spi.datastore.predicates.BuildRecordPredicates.temporaryBuild)1 Predicate (org.jboss.pnc.spi.datastore.repositories.api.Predicate)1 SortInfo (org.jboss.pnc.spi.datastore.repositories.api.SortInfo)1