Example usage for com.google.common.collect Lists partition

List of usage examples for com.google.common.collect Lists partition

Introduction

In this page you can find the example usage for com.google.common.collect Lists partition.

Prototype

public static <T> List<List<T>> partition(List<T> list, int size) 

Source Link

Document

Returns consecutive List#subList(int,int) sublists of a list, each of the same size (the final list may be smaller).

Usage

From source file:org.eclipse.hawkbit.repository.jpa.OfflineDsAssignmentStrategy.java

@Override
public List<JpaTarget> findTargetsForAssignment(final List<String> controllerIDs, final long setId) {
    return Lists.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT).stream()
            .map(ids -> targetRepository.findAll(SpecificationsBuilder.combineWithAnd(Arrays.asList(
                    TargetSpecifications.hasControllerIdAndAssignedDistributionSetIdNot(ids, setId),
                    TargetSpecifications.notEqualToTargetUpdateStatus(TargetUpdateStatus.PENDING)))))
            .flatMap(List::stream).collect(Collectors.toList());
}

From source file:ddf.catalog.cache.solr.impl.CacheBulkProcessor.java

/**
 * Create a new cache bulk processor that will check added metacards for bulk processing at
 * the configured delay interval./*from   w  ww  . j  a  v  a2s  .  c  o  m*/
 *
 * @param cache target Solr cache to bulk add metacards
 * @param delay delay between decision to bulk add
 * @param delayUnit units of the delay
 */
public CacheBulkProcessor(final SolrCache cache, final long delay, final TimeUnit delayUnit) {
    batchScheduler.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            try {
                if (metacardsToCache.size() > 0 && (metacardsToCache.size() >= batchSize || timeToFlush())) {
                    LOGGER.debug("{} metacards to batch add to cache", metacardsToCache.size());

                    List<Metacard> metacards = new ArrayList<>(metacardsToCache.values());
                    for (Collection<Metacard> batch : Lists.partition(metacards, batchSize)) {
                        LOGGER.debug("Caching a batch of {} metacards", batch.size());
                        cache.create(batch);

                        for (Metacard metacard : batch) {
                            metacardsToCache.remove(metacard.getId());
                        }
                    }

                    lastBulkAdd = new Date();
                }
            } catch (Throwable throwable) {
                LOGGER.warn("Scheduled bulk ingest to cache failed", throwable);
            }
        }
    }, delay, delay, delayUnit);
}

From source file:org.locationtech.geogig.storage.postgresql.v9.PGObjectStoreGetAllIterator.java

@Override
protected T computeNext() {
    if (nextBatch != null && nextBatch.hasNext()) {
        return nextBatch.next();
    }/*from w  w  w  .  ja va 2  s . c  om*/
    if (!ids.hasNext()) {
        return endOfData();
    }
    {
        T obj = tryNextCached();
        if (obj != null) {
            return obj;
        }
    }

    final int queryBatchSize = store.getAllBatchSize;
    final int superPartitionBatchSize = 10 * queryBatchSize;

    List<T> hits = new LinkedList<>();
    List<ObjectId> cacheMisses = new ArrayList<>(superPartitionBatchSize);
    for (int i = 0; i < superPartitionBatchSize && ids.hasNext(); i++) {
        ObjectId id = ids.next();
        RevObject cached = cache.getIfPresent(id);
        if (cached == null) {
            cacheMisses.add(id);
        } else {
            T obj = cacheHit(id, cached);
            if (obj != null) {
                hits.add(obj);
            }
        }
    }
    List<List<ObjectId>> partitions = Lists.partition(cacheMisses, queryBatchSize);
    List<Future<List<T>>> futures = new ArrayList<>(partitions.size());
    for (List<ObjectId> partition : partitions) {
        Future<List<T>> dbBatch;
        dbBatch = store.getAll(partition, listener, type);
        futures.add(dbBatch);
    }

    final Function<Future<List<T>>, List<T>> futureGetter = (objs) -> {
        try {
            return objs.get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    };

    Iterable<List<T>> lists = Iterables.transform(futures, futureGetter);
    Iterable<T> concat = Iterables.concat(lists);
    Iterator<T> iterator = concat.iterator();

    nextBatch = Iterators.concat(hits.iterator(), iterator);
    return computeNext();
}

From source file:org.sonar.core.issue.db.ActionPlanDao.java

public List<ActionPlanDto> findByKeys(Collection<String> keys) {
    if (keys.isEmpty()) {
        return Collections.emptyList();
    }/*from ww w  . j  a  v a  2 s  . c  o m*/
    SqlSession session = mybatis.openSession();
    try {
        List<ActionPlanDto> dtosList = newArrayList();
        List<List<String>> keysPartition = Lists.partition(newArrayList(keys), 1000);
        for (List<String> partition : keysPartition) {
            List<ActionPlanDto> dtos = session.getMapper(ActionPlanMapper.class).findByKeys(partition);
            dtosList.addAll(dtos);
        }
        return dtosList;
    } finally {
        MyBatis.closeQuietly(session);
    }
}

From source file:org.fenixedu.cms.ui.AdminSites.java

@RequestMapping(value = "manage/{page}", method = RequestMethod.GET)
public String list(@PathVariable(value = "page") int page, Model model) {
    List<List<Site>> pages = Lists.partition(getSites(), ITEMS_PER_PAGE);
    int currentPage = normalize(page, pages);
    model.addAttribute("numberOfPages", pages.size());
    model.addAttribute("currentPage", currentPage);
    model.addAttribute("sites", pages.isEmpty() ? Collections.emptyList() : pages.get(currentPage));
    model.addAttribute("isManager", DynamicGroup.get("managers").isMember(Authenticate.getUser()));
    return "fenixedu-cms/manage";
}

From source file:de.thm.arsnova.persistence.couchdb.CouchDbUserRepository.java

@Override
public int deleteInactiveUsers(final long lastActivityBefore) {
    final ViewQuery q = createQuery("by_creationtimestamp_for_inactive").endKey(lastActivityBefore);
    final List<ViewResult.Row> rows = db.queryView(q).getRows();

    int count = 0;
    final List<List<ViewResult.Row>> partitions = Lists.partition(rows, BULK_PARTITION_SIZE);
    for (final List<ViewResult.Row> partition : partitions) {
        final List<BulkDeleteDocument> newDocs = new ArrayList<>();
        for (final ViewResult.Row oldDoc : partition) {
            final BulkDeleteDocument newDoc = new BulkDeleteDocument(oldDoc.getId(), oldDoc.getValue());
            newDocs.add(newDoc);/*from   ww  w  .  ja va 2 s .  com*/
            logger.debug("Marked user document {} for deletion.", oldDoc.getId());
        }

        if (newDocs.size() > 0) {
            final List<DocumentOperationResult> results = db.executeBulk(newDocs);
            if (!results.isEmpty()) {
                /* TODO: This condition should be improved so that it checks the operation results. */
                count += newDocs.size();
            }
        }
    }

    if (count > 0) {
        logger.info("Deleted {} inactive users.", count);
        log("cleanup", "type", "user", "count", count);
    }

    return count;
}

From source file:org.eclipse.hawkbit.repository.jpa.OnlineDsAssignmentStrategy.java

@Override
List<JpaTarget> findTargetsForAssignment(final List<String> controllerIDs, final long setId) {
    return Lists.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT).stream()
            .map(ids -> targetRepository
                    .findAll(TargetSpecifications.hasControllerIdAndAssignedDistributionSetIdNot(ids, setId)))
            .flatMap(List::stream).collect(Collectors.toList());
}

From source file:com.dangdang.ddframe.job.plugin.job.type.AbstractThroughputDataFlowElasticJob.java

private void concurrentProcessData(final JobExecutionMultipleShardingContext shardingContext,
        final List<T> data) {
    int threadCount = getConfigService().getConcurrentDataProcessThreadCount();
    if (threadCount <= 1 || data.size() <= threadCount) {
        processDataWithStatistics(shardingContext, data);
        return;//  www.  j a  v a 2s  .com
    }
    List<List<T>> splitedData = Lists.partition(data, data.size() / threadCount);
    final CountDownLatch latch = new CountDownLatch(splitedData.size());
    for (final List<T> each : splitedData) {
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    processDataWithStatistics(shardingContext, each);
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    latchAwait(latch);
}

From source file:org.apache.hadoop.metrics2.cloudwatch.CloudWatchSink.java

@Override
public void flush() {
    for (List<MetricDatum> partition : Lists.partition(_metrics, _batch)) {
        PutMetricDataRequest req = new PutMetricDataRequest().withNamespace(_namespace)
                .withMetricData(partition);
        _client.putMetricData(req);//from   w w  w  .ja va  2s .co m
    }
    _metrics.clear();
}

From source file:com.netflix.metacat.main.services.MetadataService.java

/**
 * Deletes all the data metadata marked for deletion.
 *///  ww  w. j a v a 2s .co m
public void processDeletedDataMetadata() {
    // Get the data metadata that were marked deleted a number of days back
    // Check if the uri is being used
    // If uri is not used then delete the entry from data_metadata
    log.info("Start deleting data metadata");
    try {
        final DateTime priorTo = DateTime.now().minusDays(config.getDataMetadataDeleteMarkerLifetimeInDays());
        final int limit = 100000;
        final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
        while (true) {
            final List<String> urisToDelete = userMetadataService.getDeletedDataMetadataUris(priorTo.toDate(),
                    0, limit);
            log.info("Count of deleted marked data metadata: {}", urisToDelete.size());
            if (urisToDelete.size() > 0) {
                final List<String> uris = urisToDelete.parallelStream().filter(uri -> !uri.contains("="))
                        .map(uri -> userMetadataService.getDescendantDataUris(uri)).flatMap(Collection::stream)
                        .collect(Collectors.toList());
                uris.addAll(urisToDelete);
                log.info("Count of deleted marked data metadata (including descendants) : {}", uris.size());
                final List<List<String>> subListsUris = Lists.partition(uris, 1000);
                subListsUris.parallelStream().forEach(subUris -> {
                    MetacatContextManager.setContext(metacatRequestContext);
                    final Map<String, List<QualifiedName>> uriQualifiedNames = partitionService
                            .getQualifiedNames(subUris, false);
                    final List<String> canDeleteMetadataForUris = subUris.parallelStream()
                            .filter(s -> !Strings.isNullOrEmpty(s)).filter(s -> uriQualifiedNames.get(s) == null
                                    || uriQualifiedNames.get(s).size() == 0)
                            .collect(Collectors.toList());
                    log.info("Start deleting data metadata: {}", canDeleteMetadataForUris.size());
                    userMetadataService.deleteDataMetadatas(canDeleteMetadataForUris);
                    MetacatContextManager.removeContext();
                });
            }
            if (urisToDelete.size() < limit) {
                break;
            }
        }
    } catch (Exception e) {
        CounterWrapper.incrementCounter("dse.metacat.processDeletedDataMetadata");
        log.warn("Failed deleting data metadata", e);
    }
    log.info("End deleting data metadata");
}