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

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

Introduction

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

Prototype

public static <I, O> ListenableFuture<O> transformAsync(ListenableFuture<I> input,
        AsyncFunction<? super I, ? extends O> function) 

Source Link

Document

Returns a new ListenableFuture whose result is asynchronously derived from the result of the given Future .

Usage

From source file:com.google.gapid.models.ApiContext.java

@Override
protected ListenableFuture<FilteringContext[]> doLoad(Path.Any path) {
    return Futures.transform(Futures.transformAsync(client.get(path), val -> {
        List<ListenableFuture<ApiContext.IdAndContext>> contexts = Lists.newArrayList();
        for (Path.Context ctx : val.getContexts().getListList()) {
            contexts.add(Futures.transform(client.get(Path.Any.newBuilder().setContext(ctx).build()),
                    value -> new IdAndContext(ctx, value.getContext())));
        }//from w  ww  .java  2 s .c o m
        return Futures.allAsList(contexts);
    }), this::unbox);
}

From source file:com.facebook.presto.split.BufferingSplitSource.java

private ListenableFuture<?> fetchSplits(int min, int max, List<Split> output) {
    checkArgument(min <= max, "Min splits greater than max splits");
    if (source.isFinished() || output.size() >= min) {
        return immediateFuture(null);
    }//from w w w.  ja  v a 2  s.  c  o m
    ListenableFuture<List<Split>> future = source.getNextBatch(max - output.size());
    return Futures.transformAsync(future, splits -> {
        output.addAll(splits);
        return fetchSplits(min, max, output);
    });
}

From source file:com.facebook.buck.artifact_cache.RetryingCacheDecorator.java

@Override
public ListenableFuture<CacheResult> fetchAsync(@Nullable BuildTarget target, RuleKey ruleKey,
        LazyPath output) {/*  w ww .j  av  a 2 s  . c  o m*/
    List<String> allCacheErrors = new ArrayList<>();
    ListenableFuture<CacheResult> resultFuture = delegate.fetchAsync(target, ruleKey, output);
    for (int retryCount = 1; retryCount < maxFetchRetries; retryCount++) {
        int retryCountForLambda = retryCount;
        resultFuture = Futures.transformAsync(resultFuture, result -> {
            if (result.getType() != CacheResultType.ERROR) {
                return Futures.immediateFuture(result);
            }
            result.cacheError().ifPresent(allCacheErrors::add);
            LOG.info("Failed to fetch %s after %d/%d attempts, exception: %s", ruleKey, retryCountForLambda + 1,
                    maxFetchRetries, result.cacheError());
            return delegate.fetchAsync(target, ruleKey, output);
        });
    }
    return Futures.transform(resultFuture, result -> {
        if (result.getType() != CacheResultType.ERROR) {
            return result;
        }
        String msg = String.join("\n", allCacheErrors);
        if (!msg.contains(NoHealthyServersException.class.getName())) {
            buckEventBus.post(ConsoleEvent.warning("Failed to fetch %s over %s after %d attempts.", ruleKey,
                    cacheMode.name(), maxFetchRetries));
        }
        return CacheResult.builder().from(result).setCacheError(msg).build();
    }, MoreExecutors.directExecutor());
}

From source file:com.facebook.buck.remoteexecution.util.OutputsMaterializer.java

/** Materialize the outputs of an action into a directory. */
public ListenableFuture<Void> materialize(Collection<OutputDirectory> outputDirectories,
        Collection<Protocol.OutputFile> outputFiles, Path root) throws IOException {
    ImmutableList.Builder<ListenableFuture<Void>> pending = ImmutableList.builder();

    for (Protocol.OutputFile file : outputFiles) {
        Path path = root.resolve(file.getPath());
        ensureParent(path);// w w w  .  j av  a 2  s. com
        pending.add(fetchAndMaterialize(file.getDigest(), file.getIsExecutable(), path));
    }

    for (Protocol.OutputDirectory directory : outputDirectories) {
        Path dirRoot = root.resolve(directory.getPath());
        pending.add(Futures.transformAsync(fetcher.fetch(directory.getTreeDigest()), data -> {
            Protocol.Tree tree = protocol.parseTree(data);
            Map<Protocol.Digest, Protocol.Directory> childMap = new HashMap<>();
            // TODO(cjhopman): If a Tree contains multiple duplicate Directory nodes, is that
            // valid? Should that be rejected?
            for (Directory child : tree.getChildrenList()) {
                childMap.put(protocol.computeDigest(child), child);
            }
            ImmutableList.Builder<ListenableFuture<Void>> pendingFilesBuilder = ImmutableList.builder();
            materializeDirectory(childMap, tree.getRoot(), dirRoot, pendingFilesBuilder::add);
            return Futures.whenAllSucceed(pendingFilesBuilder.build()).call(() -> null);
        }));
    }

    return Futures.whenAllSucceed(pending.build()).call(() -> null);
}

From source file:com.orangerhymelabs.helenus.cassandra.table.ViewService.java

public ListenableFuture<List<View>> readAll(String database, String table, Object... parms) {
    ListenableFuture<Boolean> tableFuture = tables.exists(database, table);
    return Futures.transformAsync(tableFuture, new AsyncFunction<Boolean, List<View>>() {
        @Override//from  w  w  w . j a  va 2  s .c o m
        public ListenableFuture<List<View>> apply(Boolean exists) throws Exception {
            if (exists) {
                return views.readAll(parms);
            } else {
                return Futures.immediateFailedFuture(new ItemNotFoundException("Table not found: " + table));
            }
        }
    });
}

From source file:com.google.gapid.models.ApiState.java

@Override
protected ListenableFuture<Node> doLoad(Path.Any path) {
    return Futures.transformAsync(client.get(path),
            tree -> Futures.transform(client.get(Paths.any(tree.getStateTree().getRoot())),
                    val -> new RootNode(tree.getStateTree().getRoot().getTree(), val.getStateTreeNode())));
}

From source file:com.facebook.buck.parser.PipelineNodeCache.java

/**
 * Helper for de-duping jobs against the cache.
 *
 * @param jobSupplier a supplier to use to create the actual job.
 * @return future describing the job. It can either be an immediate future (result cache hit),
 *         ongoing job (job cache hit) or a new job (miss).
 *//*  www .j ava 2 s .  c  o m*/
protected final ListenableFuture<T> getJobWithCacheLookup(final Cell cell, final K key,
        JobSupplier<T> jobSupplier) throws BuildTargetException {
    Optional<T> cacheLookupResult = cache.lookupComputedNode(cell, key);
    if (cacheLookupResult.isPresent()) {
        return Futures.immediateFuture(cacheLookupResult.get());
    }

    ListenableFuture<T> speculativeCacheLookupResult = jobsCache.get(key);
    if (speculativeCacheLookupResult != null) {
        return speculativeCacheLookupResult;
    }

    // We use a SettableFuture to resolve any races between threads that are trying to create the
    // job for the given key. The SettableFuture is cheap to throw away in case we didn't "win" and
    // can be easily "connected" to a future that actually does work in case we did.
    SettableFuture<T> resultFutureCandidate = SettableFuture.create();
    ListenableFuture<T> resultFutureInCache = jobsCache.putIfAbsent(key, resultFutureCandidate);
    if (resultFutureInCache != null) {
        // Another thread succeeded in putting the new value into the cache.
        return resultFutureInCache;
    }
    // Ok, "our" candidate future went into the jobsCache, schedule the job and 'chain' the result
    // to the SettableFuture, so that anyone else waiting on it will get the same result.
    final SettableFuture<T> resultFuture = resultFutureCandidate;
    try {
        ListenableFuture<T> nodeJob = Futures.transformAsync(jobSupplier.get(),
                input -> Futures.immediateFuture(cache.putComputedNodeIfNotPresent(cell, key, input)));
        resultFuture.setFuture(nodeJob);
    } catch (Throwable t) {
        resultFuture.setException(t);
        throw t;
    }
    return resultFuture;
}

From source file:com.google.gerrit.server.index.ReindexAfterUpdate.java

@Override
public void onGitReferenceUpdated(final Event event) {
    if (event.getRefName().startsWith(RefNames.REFS_CHANGES)
            || event.getRefName().startsWith(RefNames.REFS_DRAFT_COMMENTS)
            || event.getRefName().startsWith(RefNames.REFS_USERS)) {
        return;/*from   w  w w.  j  a  va  2 s .  c o  m*/
    }
    Futures.transformAsync(executor.submit(new GetChanges(event)),
            new AsyncFunction<List<Change>, List<Void>>() {
                @Override
                public ListenableFuture<List<Void>> apply(List<Change> changes) {
                    List<ListenableFuture<Void>> result = Lists.newArrayListWithCapacity(changes.size());
                    for (Change c : changes) {
                        result.add(executor.submit(new Index(event, c.getId())));
                    }
                    return Futures.allAsList(result);
                }
            });
}

From source file:com.facebook.buck.rules.modern.builders.OutputsMaterializer.java

/** Materialize the outputs of an action into a directory. */
public void materialize(Collection<OutputDirectory> outputDirectories,
        Collection<Protocol.OutputFile> outputFiles, Path root) throws IOException {
    ImmutableList.Builder<ListenableFuture<Void>> pending = ImmutableList.builder();

    for (Protocol.OutputFile file : outputFiles) {
        Path path = root.resolve(file.getPath());
        ensureParent(path);/*from   w w w.  j a va2 s.c  o m*/
        ByteBuffer content = file.getContent();
        if (content != null) {
            try (FileChannel output = FileChannel.open(path, StandardOpenOption.WRITE,
                    StandardOpenOption.CREATE)) {
                output.write(content);
            }
            setExecutable(file.getIsExecutable(), path);
        } else {
            pending.add(fetchAndMaterialize(file.getDigest(), file.getIsExecutable(), path));
        }
    }

    for (Protocol.OutputDirectory directory : outputDirectories) {
        Path dirRoot = root.resolve(directory.getPath());
        pending.add(Futures.transformAsync(fetcher.fetch(directory.getTreeDigest()), data -> {
            Protocol.Tree tree = protocol.parseTree(data);
            Map<Protocol.Digest, Protocol.Directory> childMap = RichStream.from(tree.getChildrenList())
                    .collect(ImmutableMap.toImmutableMap(ThrowingFunction.asFunction(protocol::computeDigest),
                            child -> child));

            ImmutableList.Builder<ListenableFuture<Void>> pendingFilesBuilder = ImmutableList.builder();
            materializeDirectory(childMap, tree.getRoot(), dirRoot, pendingFilesBuilder::add);
            return Futures.whenAllSucceed(pendingFilesBuilder.build()).call(() -> null);
        }));
    }

    try {
        Futures.whenAllSucceed(pending.build()).call(() -> null).get();
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.google.gapid.server.Client.java

public ListenableFuture<ServerInfo> getSeverInfo() {
    LOG.log(FINE, "RPC->getServerInfo()");
    return Futures.transformAsync(client.getServerInfo(GetServerInfoRequest.newBuilder().build()),
            in -> Futures.immediateFuture(throwIfError(in.getInfo(), in.getError())));
}