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.romeikat.datamessie.core.sync.service.template.withIdAndVersion.EntityWithIdAndVersionSynchronizer.java
private Map<Long, Long> loadRhsIdsWithVersion(final Map<Long, Long> lhsIdsWithVersion) { final Map<Long, Long> rhsIdsWithVersion = new ConcurrentHashMap<>(lhsIdsWithVersion.size()); final List<Long> lhsIds = Lists.newArrayList(lhsIdsWithVersion.keySet()); final List<List<Long>> lhsIdsBatches = Lists.partition(lhsIds, batchSizeEntities); new ParallelProcessing<List<Long>>(sessionFactory, lhsIdsBatches, parallelismFactor) { @Override//from www . jav a 2 s .co m public void doProcessing(final HibernateSessionProvider rhsSessionProvider, final List<Long> lhsIdsBatch) { final Map<Long, Long> rhsIdsWithVersionBatch = dao .getIdsWithVersion(rhsSessionProvider.getStatelessSession(), lhsIdsBatch); rhsIdsWithVersion.putAll(rhsIdsWithVersionBatch); } }; return rhsIdsWithVersion; }
From source file:com.netflix.metacat.connector.hive.sql.DirectSqlSavePartition.java
/** * Updates the existing partitions. This method assumes that the partitions already exists and so does not * validate to check if it exists./*from w w w . ja v a 2 s. c om*/ * Note: Column descriptor of the partitions will not be updated. * * @param tableQName table name * @param partitionHolders list of partitions */ public void update(final QualifiedName tableQName, final List<PartitionHolder> partitionHolders) { final long start = registry.clock().wallTime(); try { final List<List<PartitionHolder>> subPartitionDetailList = Lists.partition(partitionHolders, batchSize); final long currentTimeInEpoch = Instant.now().getEpochSecond(); for (List<PartitionHolder> subPartitionHolders : subPartitionDetailList) { _update(tableQName, subPartitionHolders, currentTimeInEpoch); } } finally { this.fastServiceMetric.recordTimer(HiveMetrics.TagAlterPartitions.getMetricName(), registry.clock().wallTime() - start); } }
From source file:org.flockdata.engine.track.MediationFacadeNeo.java
@Override public Collection<TrackResultBean> trackEntities(final FortressSegment segment, final List<EntityInputBean> inputBeans, int splitListInTo) throws FlockException, IOException, ExecutionException, InterruptedException { String id = Thread.currentThread().getName() + "/" + DateTime.now().getMillis(); if (segment == null) { throw new FlockException("No fortress supplied. Unable to process work without a valid fortress"); }//from ww w. ja va 2 s. c om Future<Collection<DocumentType>> docType = docTypeRetryService.createDocTypes(segment, inputBeans); Future<Collection<TagResultBean>> tagResults = createTagsAsync(segment.getCompany(), getTags(inputBeans)); logger.debug("About to create docTypes"); EntityInputBean first = inputBeans.iterator().next(); logger.debug("Dispatched request to create tags"); try { // A long time, but this is to avoid test issues on the low spec build box List<List<EntityInputBean>> splitList = Lists.partition(inputBeans, splitListInTo); StopWatch watch = new StopWatch(); watch.start(); logger.trace("Starting Batch [{}] - size [{}]", id, inputBeans.size()); Collection<TrackResultBean> allResults = new ArrayList<>(); // We have to wait for the docType before proceeding to create entities Collection<DocumentType> docs = docType.get(10, TimeUnit.SECONDS); // assert docs.size()!=0; // for (List<EntityInputBean> entityInputBeans : splitList) { DocumentType documentType = null; if (docs.iterator().hasNext()) documentType = docs.iterator().next(); Iterable<TrackResultBean> loopResults = entityRetry.track(documentType, segment, entityInputBeans, tagResults); logger.debug("Tracked requests"); distributeChanges(segment.getFortress(), loopResults); for (TrackResultBean theResult : loopResults) { allResults.add(theResult); } } watch.stop(); logger.debug("Completed Batch [{}] - secs= {}, RPS={}", id, f.format(watch.getTotalTimeSeconds()), f.format(inputBeans.size() / watch.getTotalTimeSeconds())); return allResults; } catch (TimeoutException e) { logger.error("Time out looking/creating docType " + first.getDocumentType().getName()); throw new FlockException("Time out looking/creating docType " + first.getDocumentType().getName()); } }
From source file:ru.runa.wfe.security.logic.AuthorizationLogic.java
private void setPermissionsImpl(User user, String executorName, Map<SecuredObjectType, Set<String>> objectNames, Set<Permission> permissions, boolean deleteExisting) { Executor executor = executorDao.getExecutor(executorName); // [QSL] Only id is needed, or maybe even join would be enough. permissionDao.checkAllowed(user, Permission.LIST, executor); QPermissionMapping pm = QPermissionMapping.permissionMapping; for (Map.Entry<SecuredObjectType, Set<String>> kv : objectNames.entrySet()) { SecuredObjectType type = kv.getKey(); Set<String> names = kv.getValue(); if (type.isSingleton()) { // To handle both singletons and non-singletons in the same for(namesPart...) loop and thus avoid `q` construction duplication. // I'd rather have inner function (closure) for `q` construction, but this is java. names = new HashSet<>(1); names.add(null);// ww w .ja va 2s. co m } for (List<String> namesPart : Lists.partition(new ArrayList<>(names), SystemProperties.getDatabaseNameParametersCount())) { List<Long> objectIds; if (type.isSingleton()) { // Ignore namesPart: it contains single null element added above, in single loop iteration. objectIds = Collections.singletonList(0L); } else { objectIds = securedObjectFactory.getIdsByNames(type, new HashSet<>(namesPart)); } permissionDao.checkAllowedForAll(user, Permission.UPDATE_PERMISSIONS, type, objectIds); HashSet<IdAndPermission> existing = new HashSet<>(); try (CloseableIterator<Tuple> i = queryFactory.select(pm.objectId, pm.permission).from(pm) .where(pm.executor.eq(executor).and(pm.objectType.eq(type)).and(pm.objectId.in(objectIds)) .and(pm.permission.in(permissions))) .iterate()) { while (i.hasNext()) { Tuple t = i.next(); existing.add(new IdAndPermission(t.get(0, Long.class), t.get(1, Permission.class))); } } for (Permission perm : permissions) { for (Long id : objectIds) { if (!existing.remove(new IdAndPermission(id, perm))) { // [SQL] Optimizable: for(perm) { insert-select from executor where id in (objectIds) } sessionFactory.getCurrentSession() .save(new PermissionMapping(executor, type, id, perm)); } } } if (deleteExisting && !existing.isEmpty()) { // Delete in single statement; getDatabaseNameParametersCount() is much less than getDatabaseParametersCount(), so should be OK. BooleanExpression cond = Expressions.FALSE; for (IdAndPermission ip : existing) { cond = cond.or(pm.objectId.eq(ip.id).and(pm.permission.eq(ip.permission))); } queryFactory.delete(pm).where(pm.executor.eq(executor).and(pm.objectType.eq(type)).and(cond)) .execute(); } } } }
From source file:com.baasbox.service.push.PushService.java
public boolean[] send(String message, List<String> usernames, List<Integer> pushProfiles, JsonNode bodyJson, boolean[] withError) throws Exception { PushLogger pushLogger = PushLogger.getInstance(); List<String> iosToken = new ArrayList<String>(); List<String> androidToken = new ArrayList<String>(); com.baasbox.db.DbHelper.reconnectAsAdmin(); for (String username : usernames) { pushLogger.addMessage("Processing user %s ...", username); if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("Try to send a message (" + message + ") to " + username); UserDao udao = UserDao.getInstance(); ODocument user = udao.getByUserName(username); if (user == null) { pushLogger.addMessage("+++ ERROR: User %s does not exist!", username); if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("User " + username + " does not exist"); throw new UserNotFoundException("User " + username + " does not exist"); }//w ww . j a va 2s.co m ODocument userSystemProperties = user.field(UserDao.ATTRIBUTES_SYSTEM); if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("userSystemProperties: " + userSystemProperties); pushLogger.addMessage("... system properties %s ...", userSystemProperties); List<ODocument> loginInfos = userSystemProperties.field(UserDao.USER_LOGIN_INFO); if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("Sending to " + loginInfos.size() + " devices"); pushLogger.addMessage("... the message will be sent to %d device(s)...", loginInfos.size()); pushLogger.addMessage("... retrieving device(s) info..."); for (ODocument loginInfo : loginInfos) { pushLogger.addMessage("...... login info: %s ...", loginInfo); String pushToken = loginInfo.field(UserDao.USER_PUSH_TOKEN); String vendor = loginInfo.field(UserDao.USER_DEVICE_OS); pushLogger.addMessage("......... device token: %s ...", pushToken); pushLogger.addMessage("......... os/vendor: %s ...", vendor); if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("push token: " + pushToken); if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("vendor: " + vendor); if (!StringUtils.isEmpty(vendor) && !StringUtils.isEmpty(pushToken)) { VendorOS vos = VendorOS.getVendorOs(vendor); if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("vos: " + vos); if (vos != null) { switch (vos) { case IOS: iosToken.add(pushToken); break; case ANDROID: androidToken.add(pushToken); break; } } //vos!=null } //(!StringUtils.isEmpty(vendor) && !StringUtils.isEmpty(deviceId) } //for (ODocument loginInfo : loginInfos) } //for (String username : usernames) int i = 0; pushLogger.addMessage("... retrieving app(s) push configurations and sending notifications..."); for (Integer pushProfile : pushProfiles) { pushLogger.addMessage("...... profile %d ...", pushProfile); HashMap<Factory.VendorOS, IPushServer> allVendors = Factory.getAllIstances(); ImmutableMap<ConfigurationKeys, String> pushParam = getPushParameters(pushProfile); IPushServer apnServer = allVendors.get(VendorOS.IOS); apnServer.setConfiguration(pushParam); IPushServer gcmServer = allVendors.get(VendorOS.ANDROID); gcmServer.setConfiguration(pushParam); pushLogger.addMessage("......... sending to %d iOS device(s)...", iosToken.size()); if (iosToken.size() > 0) { for (List<String> thousandUsersApple : Lists.partition(iosToken, 1)) { withError[i] = apnServer.send(message, thousandUsersApple, bodyJson); if (withError[i]) pushLogger.addMessage( "........... WARNING: something went wrong sending this batch (%d) of messages to iOS devices", i); } i++; } pushLogger.addMessage("......... sending to %d Android device(s)...", androidToken.size()); if (androidToken.size() > 0) { for (List<String> thousandUsersAndroid : Lists.partition(androidToken, 1000)) { //needed for the GCM sending limit withError[i] = gcmServer.send(message, thousandUsersAndroid, bodyJson); if (withError[i]) pushLogger.addMessage( "........... WARNING: something went wrong sending this batch (%d) of messages to Android devices", i); } i++; } } com.baasbox.db.DbHelper.reconnectAsAuthenticatedUser(); return withError; }
From source file:com.sk89q.worldguard.protection.managers.storage.sql.RegionUpdater.java
private void updateRegionTypes() throws SQLException { Closer closer = Closer.create();/*from w w w. j av a2 s . c o m*/ try { PreparedStatement stmt = closer.register(conn.prepareStatement( "UPDATE " + config.getTablePrefix() + "region " + "SET type = ?, priority = ?, parent = NULL " + "WHERE id = ? AND world_id = " + worldId)); for (List<ProtectedRegion> partition : Lists.partition(typesToUpdate, StatementBatch.MAX_BATCH_SIZE)) { for (ProtectedRegion region : partition) { stmt.setString(1, SQLRegionDatabase.getRegionTypeName(region)); stmt.setInt(2, region.getPriority()); stmt.setString(3, region.getId()); stmt.addBatch(); } stmt.executeBatch(); } } finally { closer.closeQuietly(); } }
From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.FourStoreDriver.java
public void updateProperties(final Map<String, Object> properties, final String type) throws IOException { if (properties.isEmpty()) { return;/*from w ww. j a v a 2s . c o m*/ } final List<String> vertexURIs = new ArrayList<>(properties.keySet()); final List<List<String>> vertexURIpartitions = Lists.partition(vertexURIs, PARTITION_SIZE); for (final List<String> vertexURIpartition : vertexURIpartitions) { final Map<String, Object> propertyPartition = new HashMap<>(); for (final String vertexURI : vertexURIpartition) { final Object value = properties.get(vertexURI); propertyPartition.put(vertexURI, value); } updatePropertiesPartition(propertyPartition, type); } }
From source file:com.netflix.metacat.usermetadata.mysql.MysqlUserMetadataService.java
@Nonnull @Override/*from ww w .j a v a2s . c om*/ public Map<String, ObjectNode> getDataMetadataMap(@Nonnull final List<String> uris) { final Map<String, ObjectNode> result = Maps.newHashMap(); if (!uris.isEmpty()) { final List<List<String>> parts = Lists.partition(uris, config.getUserMetadataMaxInClauseItems()); parts.stream().forEach(keys -> result.putAll(_getMetadataMap(keys, SQL.GET_DATA_METADATAS))); } return result; }
From source file:com.netflix.spinnaker.cats.redis.cache.RedisCache.java
private Collection<CacheData> getItems(String type, List<String> ids, List<String> knownRels) { final int singleResultSize = knownRels.size() + 1; final List<String> keysToGet = new ArrayList<>(singleResultSize * ids.size()); for (String id : ids) { keysToGet.add(attributesId(type, id)); for (String rel : knownRels) { keysToGet.add(relationshipId(type, id, rel)); }//from w w w . j a v a 2s. co m } final List<String> keyResult = new ArrayList<>(keysToGet.size()); int mgetOperations = 0; try (Jedis jedis = source.getJedis()) { for (List<String> part : Lists.partition(keysToGet, options.getMaxMgetSize())) { mgetOperations++; keyResult.addAll(jedis.mget(part.toArray(new String[part.size()]))); } } if (keyResult.size() != keysToGet.size()) { throw new RuntimeException("Expected same size result as request"); } Collection<CacheData> results = new ArrayList<>(ids.size()); Iterator<String> idIterator = ids.iterator(); for (int ofs = 0; ofs < keyResult.size(); ofs += singleResultSize) { CacheData item = extractItem(idIterator.next(), keyResult.subList(ofs, ofs + singleResultSize), knownRels); if (item != null) { results.add(item); } } cacheMetrics.get(prefix, type, results.size(), ids.size(), keysToGet.size(), knownRels.size(), mgetOperations); return results; }
From source file:com.cloudant.sync.datastore.BasicDatastore.java
/** * Get list of documents for given list of numeric ids. The result list is ordered by sequence number, * and only the current revisions are returned. * * @param docIds given list of internal ids * @return list of documents ordered by sequence number *///from w w w . ja v a 2 s . c om List<DocumentRevision> getDocumentsWithInternalIds(List<Long> docIds) { Preconditions.checkNotNull(docIds, "Input document internal id list can not be null"); if (docIds.size() == 0) { return Collections.emptyList(); } final String GET_DOCUMENTS_BY_INTERNAL_IDS = "SELECT " + FULL_DOCUMENT_COLS + " FROM revs, docs " + "WHERE revs.doc_id IN ( %s ) AND current = 1 AND docs.doc_id = revs.doc_id"; // Split into batches because SQLite has a limit on the number // of placeholders we can use in a single query. 999 is the default // value, but it can be lower. It's hard to find this out from Java, // so we use a value much lower. List<DocumentRevision> result = new ArrayList<DocumentRevision>(docIds.size()); List<List<Long>> batches = Lists.partition(docIds, SQLITE_QUERY_PLACEHOLDERS_LIMIT); for (List<Long> batch : batches) { String sql = String.format(GET_DOCUMENTS_BY_INTERNAL_IDS, SQLDatabaseUtils.makePlaceholders(batch.size())); String[] args = new String[batch.size()]; for (int i = 0; i < batch.size(); i++) { args[i] = Long.toString(batch.get(i)); } result.addAll(getRevisionsFromRawQuery(sql, args)); } // Contract is to sort by sequence number, which we need to do // outside the sqlDb as we're batching requests. Collections.sort(result, new Comparator<DocumentRevision>() { @Override public int compare(DocumentRevision documentRevision, DocumentRevision documentRevision2) { long a = documentRevision.getSequence(); long b = documentRevision2.getSequence(); return (int) (a - b); } }); return result; }