Example usage for com.google.common.util.concurrent Futures allAsList

List of usage examples for com.google.common.util.concurrent Futures allAsList

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures allAsList.

Prototype

@Beta
@CheckReturnValue
public static <V> ListenableFuture<List<V>> allAsList(
        Iterable<? extends ListenableFuture<? extends V>> futures) 

Source Link

Document

Creates a new ListenableFuture whose value is a list containing the values of all its input futures, if all succeed.

Usage

From source file:org.onos.yangtools.yang.parser.repo.SharedSchemaContextFactory.java

@Override
public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
        final Collection<SourceIdentifier> requiredSources) {
    // Make sources unique
    final List<SourceIdentifier> uniqueSourceIdentifiers = deDuplicateSources(requiredSources);

    final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
    if (existing != null) {
        LOG.debug("Returning cached context {}", existing);
        return Futures.immediateCheckedFuture(existing);
    }//from  w  ww. ja  v a 2s . co m

    // Request all sources be loaded
    ListenableFuture<List<ASTSchemaSource>> sf = Futures
            .allAsList(Collections2.transform(uniqueSourceIdentifiers, requestSources));

    // Detect mismatch between requested Source IDs and IDs that are extracted from parsed source
    // Also remove duplicates if present
    // We are relying on preserved order of uniqueSourceIdentifiers as well as sf
    sf = Futures.transform(sf, new SourceIdMismatchDetector(uniqueSourceIdentifiers));

    // Assemble sources into a schema context
    final ListenableFuture<SchemaContext> cf = Futures.transform(sf, assembleSources);

    // Populate cache when successful
    Futures.addCallback(cf, new FutureCallback<SchemaContext>() {
        @Override
        public void onSuccess(final SchemaContext result) {
            cache.put(uniqueSourceIdentifiers, result);
        }

        @Override
        public void onFailure(final Throwable t) {
            LOG.debug("Failed to assemble sources", t);
        }
    });

    return Futures.makeChecked(cf, MAPPER);
}

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());
    }/* w w  w. j  a  v  a2  s  .c o  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:de.blizzy.documentr.search.PageFinder.java

private SearchResult findPages(String searchText, int page, Authentication authentication,
        IndexSearcher searcher) throws ParseException, IOException, TimeoutException {

    Future<Query> queryFuture = taskExecutor.submit(new ParseQueryTask(searchText, analyzer));
    ListenableFuture<Bits> visibleDocIdsFuture = taskExecutor.submit(
            new GetVisibleDocIdsTask(searcher, authentication, userStore, permissionEvaluator, taskExecutor));

    Query query;/*from  w ww.java  2s  .co m*/
    TopDocs docs;
    try {
        query = queryFuture.get(DocumentrConstants.INTERACTIVE_TIMEOUT, TimeUnit.SECONDS);
        Bits visibleDocIds = visibleDocIdsFuture.get(DocumentrConstants.INTERACTIVE_TIMEOUT, TimeUnit.SECONDS);
        docs = searcher.search(query, new PagePermissionFilter(visibleDocIds), HITS_PER_PAGE * page);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof ParseException) {
            throw (ParseException) cause;
        } else {
            throw Util.toRuntimeException(cause);
        }
    } finally {
        queryFuture.cancel(false);
        visibleDocIdsFuture.cancel(false);
    }

    int start = HITS_PER_PAGE * (page - 1);
    int end = Math.min(HITS_PER_PAGE * page, docs.scoreDocs.length);
    IndexReader reader = searcher.getIndexReader();
    List<ListenableFuture<SearchHit>> hitFutures = Lists.newArrayList();
    for (int i = start; i < end; i++) {
        ListenableFuture<SearchHit> hitFuture = taskExecutor
                .submit(new GetSearchHitTask(query, reader, docs.scoreDocs[i].doc, analyzer));
        hitFutures.add(hitFuture);
    }

    try {
        ListenableFuture<List<SearchHit>> allHitsFuture = Futures.allAsList(hitFutures);
        List<SearchHit> hits = allHitsFuture.get(DocumentrConstants.INTERACTIVE_TIMEOUT, TimeUnit.SECONDS);
        return new SearchResult(hits, docs.totalHits, HITS_PER_PAGE);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof IOException) {
            throw (IOException) cause;
        } else {
            throw Util.toRuntimeException(cause);
        }
    } finally {
        for (ListenableFuture<SearchHit> hitFuture : hitFutures) {
            hitFuture.cancel(false);
        }
    }
}

From source file:org.thingsboard.server.dao.attributes.CassandraBaseAttributesDao.java

@Override
public ListenableFuture<List<Void>> removeAll(TenantId tenantId, EntityId entityId, String attributeType,
        List<String> keys) {
    List<ListenableFuture<Void>> futures = keys.stream()
            .map(key -> delete(tenantId, entityId, attributeType, key)).collect(Collectors.toList());
    return Futures.allAsList(futures);
}

From source file:com.google.gerrit.pgm.RebuildNoteDb.java

@Override
public int run() throws Exception {
    mustHaveValidSite();//from  w  w  w . j  a va2s  .  c  om
    dbInjector = createDbInjector(MULTI_USER);
    threads = ThreadLimiter.limitThreads(dbInjector, threads);

    LifecycleManager dbManager = new LifecycleManager();
    dbManager.add(dbInjector);
    dbManager.start();

    sysInjector = createSysInjector();
    sysInjector.injectMembers(this);
    if (!notesMigration.enabled()) {
        throw die("NoteDb is not enabled.");
    }
    LifecycleManager sysManager = new LifecycleManager();
    sysManager.add(sysInjector);
    sysManager.start();

    ListeningExecutorService executor = newExecutor();
    System.out.println("Rebuilding the NoteDb");

    ImmutableListMultimap<Project.NameKey, Change.Id> changesByProject = getChangesByProject();
    boolean ok;
    Stopwatch sw = Stopwatch.createStarted();
    try (Repository allUsersRepo = repoManager.openRepository(allUsersName)) {
        deleteRefs(RefNames.REFS_DRAFT_COMMENTS, allUsersRepo);

        List<ListenableFuture<Boolean>> futures = new ArrayList<>();
        List<Project.NameKey> projectNames = Ordering.usingToString().sortedCopy(changesByProject.keySet());
        for (Project.NameKey project : projectNames) {
            ListenableFuture<Boolean> future = executor.submit(() -> {
                try (ReviewDb db = unwrapDb(schemaFactory.open())) {
                    return rebuildProject(db, changesByProject, project, allUsersRepo);
                } catch (Exception e) {
                    log.error("Error rebuilding project " + project, e);
                    return false;
                }
            });
            futures.add(future);
        }

        try {
            ok = Iterables.all(Futures.allAsList(futures).get(), Predicates.equalTo(true));
        } catch (InterruptedException | ExecutionException e) {
            log.error("Error rebuilding projects", e);
            ok = false;
        }
    }

    double t = sw.elapsed(TimeUnit.MILLISECONDS) / 1000d;
    System.out.format("Rebuild %d changes in %.01fs (%.01f/s)\n", changesByProject.size(), t,
            changesByProject.size() / t);
    return ok ? 0 : 1;
}

From source file:com.b2international.snowowl.core.events.util.Promise.java

/**
 * @param promises//from w  ww. jav  a 2 s  . com
 * @return
 * @since 4.6
 */
@Beta
public static Promise<List<Object>> all(Promise<?>... promises) {
    return Promise.wrap(Futures.allAsList(promises));
}

From source file:io.crate.action.sql.DDLAnalysisDispatcher.java

@Override
public ListenableFuture<Long> visitAddColumnAnalysis(final AddColumnAnalysis analysis, Void context) {
    final SettableFuture<Long> result = SettableFuture.create();
    if (analysis.newPrimaryKeys()) {
        Plan plan = genCountStarPlan(analysis.table());
        Job job = executorProvider.get().newJob(plan);
        ListenableFuture<List<TaskResult>> resultFuture = Futures
                .allAsList(executorProvider.get().execute(job));
        Futures.addCallback(resultFuture, new FutureCallback<List<TaskResult>>() {
            @Override//from   w w w  . jav  a 2 s . com
            public void onSuccess(@Nullable List<TaskResult> resultList) {
                assert resultList != null && resultList.size() == 1;
                Object[][] rows = resultList.get(0).rows();
                if ((Long) rows[0][0] == 0L) {
                    addColumnToTable(analysis, result);
                } else {
                    result.setException(new UnsupportedOperationException(
                            "Cannot add a primary key column to a table that isn't empty"));
                }
            }

            @Override
            public void onFailure(@Nonnull Throwable t) {
                result.setException(t);
            }
        });
    } else {
        addColumnToTable(analysis, result);
    }
    return result;
}

From source file:io.crate.executor.transport.task.UpsertByIdTask.java

@Override
public ListenableFuture<List<Long>> executeBulk() {
    try {/*  w  ww .ja  v a 2  s .c om*/
        List<SettableFuture<Long>> resultList = executeBulkShardProcessor();
        return Futures.allAsList(resultList);
    } catch (Throwable throwable) {
        return Futures.immediateFailedFuture(throwable);
    }
}

From source file:com.facebook.buck.distributed.build_client.PostBuildPhase.java

@VisibleForTesting
ListenableFuture<BuildSlaveStats> publishBuildSlaveFinishedStatsEvent(BuildJob job,
        ListeningExecutorService executor, ConsoleEventsDispatcher consoleEventsDispatcher) {
    if (!job.isSetBuildSlaves()) {
        return Futures.immediateFuture(null);
    }//from  w  ww.java  2  s  . c  o m

    List<ListenableFuture<Pair<BuildSlaveRunId, Optional<BuildSlaveFinishedStats>>>> slaveFinishedStatsFutures = new ArrayList<>(
            job.getBuildSlavesSize());
    for (BuildSlaveInfo info : job.getBuildSlaves()) {
        BuildSlaveRunId runId = info.getBuildSlaveRunId();
        slaveFinishedStatsFutures.add(executor.submit(() -> {
            Optional<BuildSlaveFinishedStats> stats = fetchStatsForIndividualSlave(job, runId);
            return new Pair<BuildSlaveRunId, Optional<BuildSlaveFinishedStats>>(runId, stats);
        }));
    }

    Builder builder = BuildSlaveStats.builder().setStampedeId(job.getStampedeId());
    return Futures.transform(Futures.allAsList(slaveFinishedStatsFutures),
            statsList -> createAndPublishBuildSlaveStats(builder, statsList, consoleEventsDispatcher),
            MoreExecutors.directExecutor());
}

From source file:com.google.idea.blaze.java.sync.jdeps.JdepsFileReader.java

private JdepsState doLoadJdepsFiles(Project project, BlazeContext context,
        ArtifactLocationDecoder artifactLocationDecoder, @Nullable JdepsState oldState,
        Iterable<TargetIdeInfo> targetsToLoad) {
    JdepsState state = new JdepsState();
    if (oldState != null) {
        state.targetToJdeps = Maps.newHashMap(oldState.targetToJdeps);
        state.fileToTargetMap = Maps.newHashMap(oldState.fileToTargetMap);
    }//from   w  ww  .j a  v a2  s .com

    Map<File, TargetKey> fileToTargetMap = Maps.newHashMap();
    for (TargetIdeInfo target : targetsToLoad) {
        assert target != null;
        JavaIdeInfo javaIdeInfo = target.javaIdeInfo;
        if (javaIdeInfo != null) {
            ArtifactLocation jdepsFile = javaIdeInfo.jdepsFile;
            if (jdepsFile != null) {
                fileToTargetMap.put(artifactLocationDecoder.decode(jdepsFile), target.key);
            }
        }
    }

    List<File> updatedFiles = Lists.newArrayList();
    List<File> removedFiles = Lists.newArrayList();
    state.fileState = FileDiffer.updateFiles(oldState != null ? oldState.fileState : null,
            fileToTargetMap.keySet(), updatedFiles, removedFiles);

    ListenableFuture<?> fetchFuture = PrefetchService.getInstance().prefetchFiles(project, updatedFiles);
    if (!FutureUtil.waitForFuture(context, fetchFuture).timed("FetchJdeps")
            .withProgressMessage("Reading jdeps files...").run().success()) {
        return null;
    }

    for (File removedFile : removedFiles) {
        TargetKey targetKey = state.fileToTargetMap.remove(removedFile);
        if (targetKey != null) {
            state.targetToJdeps.remove(targetKey);
        }
    }

    AtomicLong totalSizeLoaded = new AtomicLong(0);

    List<ListenableFuture<Result>> futures = Lists.newArrayList();
    for (File updatedFile : updatedFiles) {
        futures.add(submit(() -> {
            totalSizeLoaded.addAndGet(updatedFile.length());
            try (InputStream inputStream = new FileInputStream(updatedFile)) {
                Deps.Dependencies dependencies = Deps.Dependencies.parseFrom(inputStream);
                if (dependencies != null) {
                    List<String> dependencyStringList = Lists.newArrayList();
                    for (Deps.Dependency dependency : dependencies.getDependencyList()) {
                        // We only want explicit or implicit deps that were
                        // actually resolved by the compiler, not ones that are
                        // available for use in the same package
                        if (dependency.getKind() == Deps.Dependency.Kind.EXPLICIT
                                || dependency.getKind() == Deps.Dependency.Kind.IMPLICIT) {
                            dependencyStringList.add(dependency.getPath());
                        }
                    }
                    TargetKey targetKey = fileToTargetMap.get(updatedFile);
                    return new Result(updatedFile, targetKey, dependencyStringList);
                }
            } catch (FileNotFoundException e) {
                LOG.info("Could not open jdeps file: " + updatedFile);
            }
            return null;
        }));
    }
    try {
        for (Result result : Futures.allAsList(futures).get()) {
            if (result != null) {
                state.fileToTargetMap.put(result.file, result.targetKey);
                state.targetToJdeps.put(result.targetKey, result.dependencies);
            }
        }
        context.output(PrintOutput.log(String.format("Loaded %d jdeps files, total size %dkB",
                updatedFiles.size(), totalSizeLoaded.get() / 1024)));
    } catch (InterruptedException | ExecutionException e) {
        LOG.error(e);
        return null;
    }
    return state;
}