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:com.spotify.helios.rollingupdate.DefaultRolloutPlanner.java

@Override
public List<RolloutTask> plan(final Map<String, HostStatus> hostsAndStatuses) {
    // we only care about hosts that are UP
    final List<String> hosts = Lists.newArrayList();
    for (final Map.Entry<String, HostStatus> entry : hostsAndStatuses.entrySet()) {
        if (entry.getValue().getStatus().equals(HostStatus.Status.UP)) {
            hosts.add(entry.getKey());/* w w  w  .jav a2s. c o m*/
        }
    }

    // generate the rollout tasks
    final List<RolloutTask> rolloutTasks = Lists.newArrayList();
    final int parallelism = deploymentGroup.getRolloutOptions() != null
            ? deploymentGroup.getRolloutOptions().getParallelism()
            : 1;
    final boolean overlap = deploymentGroup.getRolloutOptions() != null
            && deploymentGroup.getRolloutOptions().getOverlap();

    for (final List<String> partition : Lists.partition(hosts, parallelism)) {
        rolloutTasks.addAll(overlap ? rolloutTasksWithOverlap(partition) : rolloutTasks(partition));
    }

    return ImmutableList.copyOf(rolloutTasks);
}

From source file:org.sonar.server.measure.persistence.MeasureDao.java

public List<MeasureDto> findByComponentKeyAndMetricKeys(String componentKey, List<String> metricKeys,
        DbSession session) {/*  w w w. j  a  va 2s  .  c o  m*/
    if (metricKeys.isEmpty()) {
        return Collections.emptyList();
    }
    List<MeasureDto> dtos = newArrayList();
    List<List<String>> partitions = Lists.partition(newArrayList(metricKeys), 1000);
    for (List<String> partition : partitions) {
        dtos.addAll(mapper(session).selectByComponentAndMetrics(componentKey, partition));
    }
    return dtos;
}

From source file:com.amediamanager.metrics.MetricBatcher.java

protected void sendBatch(Map<String, Collection<MetricDatum>> datums) {
    for (final Map.Entry<String, Collection<MetricDatum>> e : datums.entrySet()) {
        for (final List<MetricDatum> batch : Lists.partition(Lists.newLinkedList(e.getValue()), BATCH_SIZE)) {
            cloudWatch.putMetricDataAsync(
                    new PutMetricDataRequest().withNamespace(e.getKey()).withMetricData(batch),
                    new AsyncHandler<PutMetricDataRequest, Void>() {

                        @Override
                        public void onError(Exception exception) {
                            LOG.error("PutMetricData failed", exception);
                            LOG.info("Requeueing metric data.");
                            queuedDatums.putAll(e.getKey(), batch);
                        }/*from  ww w .j a  v  a 2  s  . c  o  m*/

                        @Override
                        public void onSuccess(PutMetricDataRequest request, Void result) {
                            LOG.info("Successfully put " + request.getMetricData().size()
                                    + " datums for namespace " + request.getNamespace());
                            LOG.debug("Request", request);
                        }

                    });
        }
    }
}

From source file:com.dangdang.ddframe.job.api.AbstractPerpetualElasticJob.java

private void processDataInMultipleThreads(final JobExecutionMultipleShardingContext shardingContext,
        final int threadCount, final List<T> data) {
    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//from  w ww  .  jav a  2s .c  om
            public void run() {
                try {
                    processDataList(shardingContext, each);
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    try {
        latch.await();
    } catch (final InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
}

From source file:com.publicuhc.uhcaddons.teammanager.commands.RandomTeamsCommand.java

/**
 * Splits the list into even sized lists
 *
 * @param list  the list to split//from ww w .j  a  va2  s.  com
 * @param count the number of lists to make
 * @param <T>   type
 * @return a list of the split lists
 */
public static <T> List<List<T>> split(List<T> list, int count) {
    Validate.isTrue(list.size() >= count, "List must be >= to the amount of requested lists");

    int amountPerList = (int) Math.ceil((double) list.size() / (double) count);

    return Lists.partition(list, amountPerList);
}

From source file:eu.mondo.driver.fourstore.FourStoreGraphDriverReadWrite.java

@Override
public void insertVertices(final List<String> uris, final String type) throws IOException {
    if (uris.isEmpty()) {
        return;//from ww  w  .  j a v a2 s  . c  o m
    }

    final List<List<String>> partitions = Lists.partition(uris, PARTITION_SIZE);
    for (final List<String> partition : partitions) {
        insertVerticesPartition(partition, type);
    }
}

From source file:google.registry.tools.server.KillAllCommitLogsAction.java

@Override
public void run() {
    checkArgument( // safety
            RegistryEnvironment.get() == RegistryEnvironment.CRASH
                    || RegistryEnvironment.get() == RegistryEnvironment.UNITTEST,
            "DO NOT RUN ANYWHERE ELSE EXCEPT CRASH OR TESTS.");
    // Create a in-memory input, assigning each bucket to its own shard for maximum parallelization,
    // with one extra shard for the CommitLogCheckpointRoot.
    Input<Key<?>> input = new InMemoryInput<>(
            Lists.partition(FluentIterable.from(Arrays.<Key<?>>asList(CommitLogCheckpointRoot.getKey()))
                    .append(CommitLogBucket.getAllBucketKeys()).toList(), 1));
    response.sendJavaScriptRedirect(/*from  www  .  java 2s  .  c o  m*/
            createJobPath(mrRunner.setJobName("Delete all commit logs").setModuleName("tools").runMapreduce(
                    new KillAllCommitLogsMapper(), new KillAllEntitiesReducer(), ImmutableList.of(input))));
}

From source file:org.sonar.core.user.UserDao.java

public List<UserDto> selectUsersByLogins(List<String> logins) {
    List<UserDto> users = Lists.newArrayList();
    if (!logins.isEmpty()) {
        SqlSession session = mybatis.openSession();
        try {/*from  www  .  j  a  v a  2s.co m*/
            UserMapper mapper = session.getMapper(UserMapper.class);
            List<List<String>> partitions = Lists.partition(logins, 1000);
            for (List<String> partition : partitions) {
                users.addAll(mapper.selectUsersByLogins(partition));
            }
        } finally {
            MyBatis.closeQuietly(session);
        }
    }
    return users;
}

From source file:jp.classmethod.aws.gradle.sqs.AmazonSQSSendMessagesTask.java

@TaskAction
public void sendMessages() {
    String queueUrl = getQueueUrl();
    Stream<String> messages = getMessages();

    if (queueUrl == null) {
        throw new GradleException("Must specify either queueName or queueUrl");
    }/*from w  ww . j  a  va2s . c  o  m*/
    if (messages == null) {
        throw new GradleException("Must provide messages to send to SQS");
    }

    AmazonSQSPluginExtension ext = getProject().getExtensions().getByType(AmazonSQSPluginExtension.class);
    AmazonSQS sqs = ext.getClient();

    final AtomicInteger counter = new AtomicInteger(0);
    List<SendMessageBatchRequestEntry> messageEntries = messages
            .map(message -> new SendMessageBatchRequestEntry()
                    .withId("gradle_message_index_" + counter.getAndIncrement()).withMessageBody(message))
            .collect(Collectors.toList());

    getLogger().info("Sending {} messages to {}", messageEntries.size(), queueUrl);
    Lists.partition(messageEntries, MAX_MESSAGE_SEND_BATCH_SIZE).parallelStream()
            .forEach(messagesToSend -> sqs.sendMessageBatch(
                    new SendMessageBatchRequest().withQueueUrl(queueUrl).withEntries(messagesToSend)));
}

From source file:com.netflix.spinnaker.clouddriver.google.batch.GoogleBatchRequest.java

public void execute() {
    if (queuedRequests.size() == 0) {
        log.debug("No requests queued in batch, exiting.");
        return;/*from w  ww  . ja va 2 s.  co  m*/
    }

    List<BatchRequest> queuedBatches = new ArrayList<>();
    List<List<QueuedRequest>> requestPartitions = Lists.partition(queuedRequests, MAX_BATCH_SIZE);
    requestPartitions.forEach(requestPart -> {
        BatchRequest newBatch = newBatch();
        requestPart.forEach(qr -> {
            try {
                qr.getRequest().queue(newBatch, qr.getCallback());
            } catch (IOException ioe) {
                log.error("Queueing request {} in batch failed.", qr);
                throw new RuntimeException(ioe);
            }
        });
        queuedBatches.add(newBatch);
    });

    ExecutorService threadPool = new ForkJoinPool(10);
    try {
        threadPool.submit(() -> queuedBatches.stream().parallel().forEach(this::executeInternalBatch)).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
    threadPool.shutdown();
}