List of usage examples for com.google.common.collect Lists partition
public static <T> List<List<T>> partition(List<T> list, int size)
From source file:com.netflix.metacat.metadata.mysql.MysqlUserMetadataService.java
private void deleteDataMetadatasWithBatch(final List<String> uris, final boolean removeDataMetadata) { try {/*w w w.j a va 2s .c o m*/ final List<List<String>> subLists = Lists.partition(uris, config.getUserMetadataMaxInClauseItems()); for (List<String> subUris : subLists) { _deleteDataMetadata(subUris, removeDataMetadata); } } catch (Exception e) { final String message = String.format("Failed deleting the data metadata for %s", uris); log.error(message, e); throw new UserMetadataServiceException(message, e); } }
From source file:org.apache.brooklyn.entity.machine.MachineInitTasks.java
private void insertIptablesRulesImpl(List<String> iptablesRules, String installCommands, SshMachineLocation machine) {/* w w w. j a v a 2s.co m*/ // Some entities, such as Riak (erlang based) have a huge range of ports, which leads to a script that // is too large to run (fails with a broken pipe). Batch the rules into batches of 100 List<List<String>> batches = Lists.partition(iptablesRules, 100); int batchNumber = 0; for (List<String> batch : batches) { batchNumber++; insertIptablesRulesOnCommandBatches(batch, machine, batchNumber); } if (installCommands != null) { serviceIptablesSave(installCommands, machine); } }
From source file:edu.cmu.cs.lti.ark.fn.identification.training.AlphabetCreationThreaded.java
/** * Splits frameElementLines into numThreads equally-sized batches and creates an alphabet * file for each one./*from ww w .j a va 2s . com*/ * * @throws IOException */ public Multiset<String> createAlphabet() throws IOException, ExecutionException, InterruptedException { final List<String> frameLines = Files.readLines(new File(frameElementsFile), Charsets.UTF_8) .subList(startIndex, endIndex); final int batchSize = (int) Math.ceil(frameLines.size() / (double) numThreads); final List<List<String>> frameLinesPartition = Lists.partition(frameLines, batchSize); final List<String> parseLines = Files.readLines(new File(parseFile), Charsets.UTF_8); final Multiset<String> alphabet = ConcurrentHashMultiset.create(); final List<Callable<Integer>> jobs = Lists.newArrayListWithExpectedSize(numThreads); for (final int i : xrange(numThreads)) { jobs.add(newJob(i, frameLinesPartition.get(i), parseLines, alphabet)); } final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads); final List<Future<Integer>> results = threadPool.invokeAll(jobs); threadPool.shutdown(); try { for (Integer i : xrange(results.size())) { logger.info(String.format("Thread %d successfully processed %d lines", i, results.get(i).get())); } } finally { threadPool.shutdownNow(); } return alphabet; }
From source file:gg.uhc.uhc.modules.team.RandomTeamsCommand.java
@Override protected boolean runCommand(CommandSender sender, OptionSet options) { int size = -1; int count = -1; if (options.has(teamCountSpec)) { count = teamCountSpec.value(options); }//from ww w. j a v a2 s .c om if (options.has(teamSizeSpec)) { size = teamSizeSpec.value(options); } if ((size == -1 && count == -1) || (size != -1 && count != -1)) { sender.sendMessage(messages.getRaw("count or size")); return true; } Set<Player> choice = Sets.newHashSet(playersSpec.values(options)); // if none are provided then add all online players if (choice.size() == 0) { choice = Sets.newHashSet(Bukkit.getOnlinePlayers()); } // parse excludes into online players Set<Player> excludes = Sets.newHashSet( Iterables.filter(Iterables.transform(excludingSpec.values(options), FunctionalUtil.ONLINE_VERSION), Predicates.notNull())); // final list with excludes removed and players that already in a team List<Player> toAssign = Lists .newArrayList(Iterables.filter(Sets.difference(choice, excludes), Predicates.not(PLAYER_HAS_TEAM))); if (toAssign.size() == 0) { sender.sendMessage(messages.getRaw("no players")); return true; } Collections.shuffle(toAssign); // calculate team sizes to fit in count teams and assign it to size and carry on as if it was a sized command if (count != -1) { size = (int) Math.ceil((double) toAssign.size() / (double) count); } // partition into teams List<List<Player>> teams = Lists.newArrayList(Lists.partition(toAssign, size)); int extras = toAssign.size() % size; // if we're excluding leftovers and there were leftovers in a final // team, then remove that team from the list. If it's the only team // then send a message saying no teams could be created. if (options.has(excludeExtrasSpec) && extras > 0) { if (teams.size() == 1) { sender.sendMessage(messages.evalTemplate("not enough players", ImmutableMap.of("size", size))); return true; } teams.remove(teams.size() - 1); } // start assigning teams for (List<Player> teamPlayers : teams) { Optional<Team> optional = module.findFirstEmptyTeam(); if (!optional.isPresent()) { sender.sendMessage(messages.getRaw("not enough teams")); return true; } Team team = optional.get(); String playerNames = Joiner.on(", ") .join(Iterables.transform(teamPlayers, FunctionalUtil.PLAYER_NAME_FETCHER)); //"teamup notification" : ${colours.command}"You were teamed up into the team {{prefix}}{{name}}"${colours.reset}${colours.command}" with: "${colours.secondary}"{{players}}"; Map<String, String> context = ImmutableMap.<String, String>builder().put("prefix", team.getPrefix()) .put("name", team.getName()).put("display name", team.getDisplayName()) .put("players", playerNames).build(); String message = messages.evalTemplate("teamup notification", context); // add each player for (Player player : teamPlayers) { team.addPlayer(player); player.sendMessage(message); } } Map<String, Integer> context = ImmutableMap.<String, Integer>builder().put("count", teams.size()) .put("size", teams.get(0).size()).put("players", toAssign.size()).build(); sender.sendMessage(messages.evalTemplate("created", context)); if (options.has(excludeExtrasSpec) && extras > 0) { sender.sendMessage(messages.evalTemplate("extras notice", ImmutableMap.of("extras", extras))); } return true; }
From source file:org.sonar.server.db.BaseDao.java
public List<DTO> getByKeys(DbSession session, Collection<KEY> keys) { if (keys.isEmpty()) { return Collections.emptyList(); }/*from w ww . j av a 2 s .co m*/ List<DTO> components = newArrayList(); List<List<KEY>> partitionList = Lists.partition(newArrayList(keys), 1000); for (List<KEY> partition : partitionList) { List<DTO> dtos = doGetByKeys(session, partition); components.addAll(dtos); } return components; }
From source file:org.sonar.core.rule.RuleDao.java
public List<RuleParamDto> selectParametersByRuleIds(List<Integer> ruleIds, SqlSession session) { List<RuleParamDto> dtos = newArrayList(); List<List<Integer>> partitionList = Lists.partition(newArrayList(ruleIds), 1000); for (List<Integer> partition : partitionList) { dtos.addAll(getMapper(session).selectParamsByRuleIds(partition)); }/* w ww.j a v a 2 s. c o m*/ return dtos; }
From source file:com.spotify.folsom.client.binary.DefaultBinaryMemcacheClient.java
private ListenableFuture<List<GetResult<V>>> multiget(List<String> keys, int ttl) { final int size = keys.size(); if (size == 0) { return Futures.immediateFuture(Collections.<GetResult<V>>emptyList()); }/*from ww w. j av a2s . co m*/ final List<List<String>> keyPartition = Lists.partition(keys, MemcacheEncoder.MAX_MULTIGET_SIZE); final List<ListenableFuture<List<GetResult<byte[]>>>> futureList = new ArrayList<>(keyPartition.size()); for (final List<String> part : keyPartition) { MultigetRequest request = MultigetRequest.create(part, charset, ttl); futureList.add(rawMemcacheClient.send(request)); } final ListenableFuture<List<GetResult<byte[]>>> future = Utils.transform(Futures.allAsList(futureList), Utils.<GetResult<byte[]>>flatten()); metrics.measureMultigetFuture(future); return transformerUtil.decodeList(future); }
From source file:com.google.cloud.pubsub.StreamingSubscriberConnection.java
@Override void sendAckOperations(List<String> acksToSend, List<PendingModifyAckDeadline> ackDeadlineExtensions) { // Send the modify ack deadlines in batches as not to exceed the max request // size./*from ww w . j ava 2 s. co m*/ List<List<String>> ackChunks = Lists.partition(acksToSend, MAX_PER_REQUEST_CHANGES); List<List<PendingModifyAckDeadline>> modifyAckDeadlineChunks = Lists.partition(ackDeadlineExtensions, MAX_PER_REQUEST_CHANGES); Iterator<List<String>> ackChunksIt = ackChunks.iterator(); Iterator<List<PendingModifyAckDeadline>> modifyAckDeadlineChunksIt = modifyAckDeadlineChunks.iterator(); while (ackChunksIt.hasNext() || modifyAckDeadlineChunksIt.hasNext()) { com.google.pubsub.v1.StreamingPullRequest.Builder requestBuilder = StreamingPullRequest.newBuilder(); if (modifyAckDeadlineChunksIt.hasNext()) { List<PendingModifyAckDeadline> modAckChunk = modifyAckDeadlineChunksIt.next(); for (PendingModifyAckDeadline modifyAckDeadline : modAckChunk) { for (String ackId : modifyAckDeadline.ackIds) { requestBuilder.addModifyDeadlineSeconds(modifyAckDeadline.deadlineExtensionSeconds) .addModifyDeadlineAckIds(ackId); } } } if (ackChunksIt.hasNext()) { List<String> ackChunk = ackChunksIt.next(); requestBuilder.addAllAckIds(ackChunk); } requestObserver.onNext(requestBuilder.build()); } }
From source file:com.sk89q.worldguard.protection.managers.storage.sql.RegionUpdater.java
private void replaceDomainUsers() throws SQLException { // Remove users Closer closer = Closer.create();//w ww . j a v a 2 s . co m try { PreparedStatement stmt = closer.register(conn.prepareStatement("DELETE FROM " + config.getTablePrefix() + "region_players " + "WHERE region_id = ? " + "AND world_id = " + worldId)); for (List<ProtectedRegion> partition : Lists.partition(domainsToReplace, StatementBatch.MAX_BATCH_SIZE)) { for (ProtectedRegion region : partition) { stmt.setString(1, region.getId()); stmt.addBatch(); } stmt.executeBatch(); } } finally { closer.closeQuietly(); } // Add users closer = Closer.create(); try { PreparedStatement stmt = closer .register(conn.prepareStatement("INSERT INTO " + config.getTablePrefix() + "region_players " + "(region_id, world_id, user_id, owner) " + "VALUES (?, " + worldId + ", ?, ?)")); StatementBatch batch = new StatementBatch(stmt, StatementBatch.MAX_BATCH_SIZE); for (ProtectedRegion region : domainsToReplace) { insertDomainUsers(stmt, batch, region, region.getMembers(), false); // owner = false insertDomainUsers(stmt, batch, region, region.getOwners(), true); // owner = true } batch.executeRemaining(); } finally { closer.closeQuietly(); } }
From source file:org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStoreJDBC.java
public boolean batchedAppendingUpdate(Connection connection, RDBTableMetaData tmd, List<String> allIds, Long modified, boolean setModifiedConditionally, String appendData) throws SQLException { boolean result = true; for (List<String> ids : Lists.partition(allIds, RDBJDBCTools.MAX_IN_CLAUSE)) { String appendDataWithComma = "," + appendData; PreparedStatementComponent stringAppend = this.dbInfo.getConcatQuery(appendDataWithComma, tmd.getDataLimitInOctets()); PreparedStatementComponent inClause = RDBJDBCTools.createInStatement("ID", ids, tmd.isIdBinary()); StringBuilder t = new StringBuilder(); t.append("update " + tmd.getName() + " set "); t.append(setModifiedConditionally ? "MODIFIED = case when ? > MODIFIED then ? else MODIFIED end, " : "MODIFIED = ?, "); t.append("MODCOUNT = MODCOUNT + 1, DSIZE = DSIZE + ?, "); t.append("DATA = " + stringAppend.getStatementComponent() + " "); t.append("where ").append(inClause.getStatementComponent()); PreparedStatement stmt = connection.prepareStatement(t.toString()); try {//from www . j av a 2s . c o m int si = 1; stmt.setObject(si++, modified, Types.BIGINT); if (setModifiedConditionally) { stmt.setObject(si++, modified, Types.BIGINT); } stmt.setObject(si++, appendDataWithComma.length(), Types.BIGINT); si = stringAppend.setParameters(stmt, si); si = inClause.setParameters(stmt, si); int count = stmt.executeUpdate(); if (count != ids.size()) { LOG.debug("DB update failed: only " + result + " of " + ids.size() + " updated. Table: " + tmd.getName() + ", IDs:" + ids); result = false; } } finally { stmt.close(); } } return result; }