/** * List分页函数 */ public static <T> List<List<T>> partition(List<T> list, int size) { return Lists.partition(list, size); }
private Collection<ShardingExecuteGroup<DataNode>> getDataNodeGroups(final List<DataNode> dataNodes) { Collection<ShardingExecuteGroup<DataNode>> result = new LinkedList<>(); for (List<DataNode> each : Lists.partition(dataNodes, Math.max(dataNodes.size() / maxConnectionsSizePerQuery, 1))) { result.add(new ShardingExecuteGroup<>(each)); } return result; }
private List<ShardingExecuteGroup<StatementExecuteUnit>> getSQLExecuteGroups( final String dataSourceName, final List<SQLUnit> sqlUnits, final SQLExecutePrepareCallback callback) throws SQLException { List<ShardingExecuteGroup<StatementExecuteUnit>> result = new LinkedList<>(); int desiredPartitionSize = Math.max(0 == sqlUnits.size() % maxConnectionsSizePerQuery ? sqlUnits.size() / maxConnectionsSizePerQuery : sqlUnits.size() / maxConnectionsSizePerQuery + 1, 1); List<List<SQLUnit>> sqlUnitPartitions = Lists.partition(sqlUnits, desiredPartitionSize); ConnectionMode connectionMode = maxConnectionsSizePerQuery < sqlUnits.size() ? ConnectionMode.CONNECTION_STRICTLY : ConnectionMode.MEMORY_STRICTLY; List<Connection> connections = callback.getConnections(connectionMode, dataSourceName, sqlUnitPartitions.size()); int count = 0; for (List<SQLUnit> each : sqlUnitPartitions) { result.add(getSQLExecuteGroup(connectionMode, connections.get(count++), dataSourceName, each, callback)); } return result; }
private void updateAndDeleteCache() { List<Long> ids = Lists.newArrayList(appNamespaceIdCache.keySet()); if (CollectionUtils.isEmpty(ids)) { return; } List<List<Long>> partitionIds = Lists.partition(ids, 500); for (List<Long> toRebuild : partitionIds) { Iterable<AppNamespace> appNamespaces = appNamespaceRepository.findAllById(toRebuild); if (appNamespaces == null) { continue; } //handle updated Set<Long> foundIds = handleUpdatedAppNamespaces(appNamespaces); //handle deleted handleDeletedAppNamespaces(Sets.difference(Sets.newHashSet(toRebuild), foundIds)); } }
public void testPartition_badSize() { List<Integer> source = Collections.singletonList(1); try { Lists.partition(source, 0); fail(); } catch (IllegalArgumentException expected) { } }
void deleteComponents(List<String> componentUuids) { if (componentUuids.isEmpty()) { return; } profiler.start("deleteComponents (projects)"); Lists.partition(componentUuids, MAX_RESOURCES_PER_QUERY).forEach(purgeMapper::deleteComponentsByUuids); session.commit(); profiler.stop(); }
void deleteComponentMeasures(List<String> componentUuids) { if (componentUuids.isEmpty()) { return; } profiler.start("deleteComponentMeasures (project_measures)"); Lists.partition(componentUuids, MAX_RESOURCES_PER_QUERY).forEach(purgeMapper::fullDeleteComponentMeasures); session.commit(); profiler.stop(); }
public void testPartition_1_1() { List<Integer> source = Collections.singletonList(1); List<List<Integer>> partitions = Lists.partition(source, 1); assertEquals(1, partitions.size()); assertEquals(Collections.singletonList(1), partitions.get(0)); }
public void testPartition_1_2() { List<Integer> source = Collections.singletonList(1); List<List<Integer>> partitions = Lists.partition(source, 2); assertEquals(1, partitions.size()); assertEquals(Collections.singletonList(1), partitions.get(0)); }
public void testPartition_empty() { List<Integer> source = Collections.emptyList(); List<List<Integer>> partitions = Lists.partition(source, 1); assertTrue(partitions.isEmpty()); assertEquals(0, partitions.size()); }
@GwtIncompatible // cannot do such a big explicit copy public void testPartitionSize_2() { assertEquals(2, Lists.partition(Collections.nCopies(0x40000001, 1), 0x40000000).size()); } }
public void testPartition_2_1() { List<Integer> source = asList(1, 2); List<List<Integer>> partitions = Lists.partition(source, 1); assertEquals(2, partitions.size()); assertEquals(Collections.singletonList(1), partitions.get(0)); assertEquals(Collections.singletonList(2), partitions.get(1)); }
public void testPartition_3_2() { List<Integer> source = asList(1, 2, 3); List<List<Integer>> partitions = Lists.partition(source, 2); assertEquals(2, partitions.size()); assertEquals(asList(1, 2), partitions.get(0)); assertEquals(asList(3), partitions.get(1)); }
public void testPartition_view() { List<Integer> list = asList(1, 2, 3); List<List<Integer>> partitions = Lists.partition(list, 3); // Changes before the partition is retrieved are reflected list.set(0, 3); Iterator<List<Integer>> iterator = partitions.iterator(); // Changes before the partition is retrieved are reflected list.set(1, 4); List<Integer> first = iterator.next(); // Changes after are too (unlike Iterables.partition) list.set(2, 5); assertEquals(asList(3, 4, 5), first); // Changes to a sublist also write through to the original list first.set(1, 6); assertEquals(asList(3, 6, 5), list); }
/** * Creates a {@link PairedStatsAccumulator} filled with the given lists of {@code x} and {@code y} * values, which must be of the same size, added in groups of {@code partitionSize} using {@link * PairedStatsAccumulator#addAll(PairedStats)}. */ static PairedStatsAccumulator createPartitionedFilledPairedStatsAccumulator( List<Double> xValues, List<Double> yValues, int partitionSize) { checkArgument(xValues.size() == yValues.size()); checkArgument(partitionSize > 0); PairedStatsAccumulator accumulator = new PairedStatsAccumulator(); List<List<Double>> xPartitions = Lists.partition(xValues, partitionSize); List<List<Double>> yPartitions = Lists.partition(yValues, partitionSize); for (int index = 0; index < xPartitions.size(); index++) { accumulator.addAll(createPairedStatsOf(xPartitions.get(index), yPartitions.get(index))); } return accumulator; }
public void testPartitionRandomAccessFalse() { List<Integer> source = Lists.newLinkedList(asList(1, 2, 3)); List<List<Integer>> partitions = Lists.partition(source, 2); assertFalse(partitions instanceof RandomAccess); assertFalse(partitions.get(0) instanceof RandomAccess); assertFalse(partitions.get(1) instanceof RandomAccess); }
@GwtIncompatible // ArrayList.subList doesn't implement RandomAccess in GWT. public void testPartitionRandomAccessTrue() { List<Integer> source = asList(1, 2, 3); List<List<Integer>> partitions = Lists.partition(source, 2); assertTrue( "partition should be RandomAccess, but not: " + partitions.getClass(), partitions instanceof RandomAccess); assertTrue( "partition[0] should be RandomAccess, but not: " + partitions.get(0).getClass(), partitions.get(0) instanceof RandomAccess); assertTrue( "partition[1] should be RandomAccess, but not: " + partitions.get(1).getClass(), partitions.get(1) instanceof RandomAccess); }
@Override public List<RolloutTask> plan(final List<String> hosts) { // generate the rollout tasks final List<RolloutTask> rolloutTasks = Lists.newArrayList(); final int parallelism = deploymentGroup.getRolloutOptions() != null && deploymentGroup.getRolloutOptions().getParallelism() != null ? deploymentGroup.getRolloutOptions().getParallelism() : 1; Lists.partition(hosts, parallelism) .forEach(partition -> rolloutTasks.addAll(rolloutTasks(partition))); return ImmutableList.copyOf(rolloutTasks); }
void deleteByRootAndModulesOrSubviews(List<IdUuidPair> rootAndModulesOrSubviewsIds) { if (rootAndModulesOrSubviewsIds.isEmpty()) { return; } List<List<Long>> idPartitions = Lists.partition(IdUuidPairs.ids(rootAndModulesOrSubviewsIds), MAX_RESOURCES_PER_QUERY); List<List<String>> uuidsPartitions = Lists.partition(IdUuidPairs.uuids(rootAndModulesOrSubviewsIds), MAX_RESOURCES_PER_QUERY); profiler.start("deleteByRootAndModulesOrSubviews (properties)"); idPartitions.forEach(purgeMapper::deletePropertiesByComponentIds); session.commit(); profiler.stop(); profiler.start("deleteByRootAndModulesOrSubviews (manual_measures)"); uuidsPartitions.forEach(purgeMapper::deleteManualMeasuresByComponentUuids); session.commit(); profiler.stop(); }