diff options
author | 2017-03-08 22:42:01 +0000 | |
---|---|---|
committer | 2017-03-09 10:30:27 +0000 | |
commit | 7a5a236dfd099eb78e019482e9fc428b5b1182fd (patch) | |
tree | bec8540f49cca6bc8b63b0ef893f4c6ef80f730d /src/main/java/com/google/devtools/build/lib/cmdline | |
parent | b35a0c086152ac6e5e0e695b21cacfd61de68b51 (diff) |
Description redacted.
--
PiperOrigin-RevId: 149585165
MOS_MIGRATED_REVID=149585165
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/cmdline')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java | 62 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/cmdline/TargetPatternResolver.java | 52 |
2 files changed, 83 insertions, 31 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java index e997b9f78d..88f891c1f3 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java @@ -19,6 +19,9 @@ import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; import com.google.devtools.build.lib.cmdline.LabelValidator.BadLabelException; import com.google.devtools.build.lib.cmdline.LabelValidator.PackageAndTarget; import com.google.devtools.build.lib.util.BatchCallback; @@ -26,15 +29,12 @@ import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.util.StringUtilities; import com.google.devtools.build.lib.util.ThreadSafeBatchCallback; import com.google.devtools.build.lib.vfs.PathFragment; - import java.io.Serializable; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Objects; -import java.util.concurrent.ForkJoinPool; import java.util.regex.Pattern; - import javax.annotation.concurrent.Immutable; /** @@ -157,17 +157,48 @@ public abstract class TargetPattern implements Serializable { throws TargetParsingException, E, InterruptedException; /** - * Same as {@link #eval}, but optionally making use of the given {@link ForkJoinPool} to achieve - * parallelism. + * Evaluates this {@link TargetPattern} synchronously, feeding the result to the given + * {@code callback}, and then returns an appropriate immediate {@link ListenableFuture}. + * + * <p>If the returned {@link ListenableFuture}'s {@link ListenableFuture#get} throws an + * {@link ExecutionException}, the cause will be an instance of either + * {@link TargetParsingException} or the given {@code exceptionClass}. + */ + public final <T, E extends Exception> ListenableFuture<Void> evalAdaptedForAsync( + TargetPatternResolver<T> resolver, + ImmutableSet<PathFragment> excludedSubdirectories, + ThreadSafeBatchCallback<T, E> callback, + Class<E> exceptionClass) { + try { + eval(resolver, excludedSubdirectories, callback, exceptionClass); + return Futures.immediateFuture(null); + } catch (TargetParsingException e) { + return Futures.immediateFailedFuture(e); + } catch (InterruptedException e) { + return Futures.immediateCancelledFuture(); + } catch (Exception e) { + if (exceptionClass.isInstance(e)) { + return Futures.immediateFailedFuture(exceptionClass.cast(e)); + } + throw new IllegalStateException(e); + } + } + + /** + * Returns a {@link ListenableFuture} representing the asynchronous evaluation of this + * {@link TargetPattern} that feeds the results to the given {@code callback}. + * + * <p>If the returned {@link ListenableFuture}'s {@link ListenableFuture#get} throws an + * {@link ExecutionException}, the cause will be an instance of either + * {@link TargetParsingException} or the given {@code exceptionClass}. */ - public <T, E extends Exception> void parEval( + public <T, E extends Exception> ListenableFuture<Void> evalAsync( TargetPatternResolver<T> resolver, ImmutableSet<PathFragment> excludedSubdirectories, ThreadSafeBatchCallback<T, E> callback, Class<E> exceptionClass, - ForkJoinPool forkJoinPool) - throws TargetParsingException, E, InterruptedException { - eval(resolver, excludedSubdirectories, callback, exceptionClass); + ListeningExecutorService executor) { + return evalAdaptedForAsync(resolver, excludedSubdirectories, callback, exceptionClass); } /** @@ -252,8 +283,8 @@ public abstract class TargetPattern implements Serializable { public <T, E extends Exception> void eval( TargetPatternResolver<T> resolver, ImmutableSet<PathFragment> excludedSubdirectories, - BatchCallback<T, E> callback, Class<E> exceptionClass) - throws TargetParsingException, E, InterruptedException { + BatchCallback<T, E> callback, + Class<E> exceptionClass) throws TargetParsingException, E, InterruptedException { Preconditions.checkArgument(excludedSubdirectories.isEmpty(), "Target pattern \"%s\" of type %s cannot be evaluated with excluded subdirectories: %s.", getOriginalPattern(), getType(), excludedSubdirectories); @@ -518,14 +549,13 @@ public abstract class TargetPattern implements Serializable { } @Override - public <T, E extends Exception> void parEval( + public <T, E extends Exception> ListenableFuture<Void> evalAsync( TargetPatternResolver<T> resolver, ImmutableSet<PathFragment> excludedSubdirectories, ThreadSafeBatchCallback<T, E> callback, Class<E> exceptionClass, - ForkJoinPool forkJoinPool) - throws TargetParsingException, E, InterruptedException { - resolver.findTargetsBeneathDirectoryPar( + ListeningExecutorService executor) { + return resolver.findTargetsBeneathDirectoryAsync( directory.getRepository(), getOriginalPattern(), directory.getPackageFragment().getPathString(), @@ -533,7 +563,7 @@ public abstract class TargetPattern implements Serializable { excludedSubdirectories, callback, exceptionClass, - forkJoinPool); + executor); } @Override diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPatternResolver.java b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPatternResolver.java index b6b384c882..38b866b68e 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPatternResolver.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPatternResolver.java @@ -15,35 +15,38 @@ package com.google.devtools.build.lib.cmdline; import com.google.common.collect.ImmutableSet; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; import com.google.devtools.build.lib.util.BatchCallback; import com.google.devtools.build.lib.util.ThreadSafeBatchCallback; import com.google.devtools.build.lib.vfs.PathFragment; import java.util.concurrent.ForkJoinPool; /** - * A callback interface that is used during the process of converting target patterns (such as + * A callback that is used during the process of converting target patterns (such as * <code>//foo:all</code>) into one or more lists of targets (such as <code>//foo:foo, * //foo:bar</code>). During a call to {@link TargetPattern#eval}, the {@link TargetPattern} makes * calls to this interface to implement the target pattern semantics. The generic type {@code T} is * only for compile-time type safety; there are no requirements to the actual type. */ -public interface TargetPatternResolver<T> { +public abstract class TargetPatternResolver<T> { /** * Reports the given warning. */ - void warn(String msg); + public abstract void warn(String msg); /** * Returns a single target corresponding to the given label, or null. This method may only throw * an exception if the current thread was interrupted. */ - T getTargetOrNull(Label label) throws InterruptedException; + public abstract T getTargetOrNull(Label label) throws InterruptedException; /** * Returns a single target corresponding to the given label, or an empty or failed result. */ - ResolvedTargets<T> getExplicitTarget(Label label) + public abstract ResolvedTargets<T> getExplicitTarget(Label label) throws TargetParsingException, InterruptedException; /** @@ -55,7 +58,7 @@ public interface TargetPatternResolver<T> { * @param packageIdentifier the identifier of the package * @param rulesOnly whether to return rules only */ - ResolvedTargets<T> getTargetsInPackage(String originalPattern, + public abstract ResolvedTargets<T> getTargetsInPackage(String originalPattern, PackageIdentifier packageIdentifier, boolean rulesOnly) throws TargetParsingException, InterruptedException; @@ -84,7 +87,7 @@ public interface TargetPatternResolver<T> { * @param exceptionClass The class type of the parameterized exception. * @throws TargetParsingException under implementation-specific failure conditions */ - <E extends Exception> void findTargetsBeneathDirectory( + public abstract <E extends Exception> void findTargetsBeneathDirectory( RepositoryName repository, String originalPattern, String directory, @@ -98,7 +101,7 @@ public interface TargetPatternResolver<T> { * Same as {@link #findTargetsBeneathDirectory}, but optionally making use of the given * {@link ForkJoinPool} to achieve parallelism. */ - <E extends Exception> void findTargetsBeneathDirectoryPar( + public <E extends Exception> ListenableFuture<Void> findTargetsBeneathDirectoryAsync( RepositoryName repository, String originalPattern, String directory, @@ -106,19 +109,38 @@ public interface TargetPatternResolver<T> { ImmutableSet<PathFragment> excludedSubdirectories, ThreadSafeBatchCallback<T, E> callback, Class<E> exceptionClass, - ForkJoinPool forkJoinPool) - throws TargetParsingException, E, InterruptedException; + ListeningExecutorService executor) { + try { + findTargetsBeneathDirectory( + repository, + originalPattern, + directory, + rulesOnly, + excludedSubdirectories, + callback, + exceptionClass); + return Futures.immediateFuture(null); + } catch (TargetParsingException e) { + return Futures.immediateFailedFuture(e); + } catch (InterruptedException e) { + return Futures.immediateCancelledFuture(); + } catch (Exception e) { + if (exceptionClass.isInstance(e)) { + return Futures.immediateFailedFuture(e); + } + throw new IllegalStateException(e); + } + } /** * Returns true, if and only if the given package identifier corresponds to a package, i.e., a - * file with the name {@code packageName/BUILD} exists in the appropriat repository. + * file with the name {@code packageName/BUILD} exists in the appropriate repository. */ - boolean isPackage(PackageIdentifier packageIdentifier) throws InterruptedException; + public abstract boolean isPackage(PackageIdentifier packageIdentifier) + throws InterruptedException; /** * Returns the target kind of the given target, for example {@code cc_library rule}. */ - String getTargetKind(T target); - - + public abstract String getTargetKind(T target); } |