Search in sources :

Example 1 with ThreadSafe

use of com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe in project bazel by bazelbuild.

the class SkyQueryEnvironment method makePackageKeyToTargetKeyMap.

@ThreadSafe
Multimap<SkyKey, SkyKey> makePackageKeyToTargetKeyMap(Iterable<SkyKey> keys) {
    Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap = ArrayListMultimap.create();
    for (SkyKey key : keys) {
        Label label = SKYKEY_TO_LABEL.apply(key);
        if (label == null) {
            continue;
        }
        packageKeyToTargetKeyMap.put(PackageValue.key(label.getPackageIdentifier()), key);
    }
    return packageKeyToTargetKeyMap;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) Label(com.google.devtools.build.lib.cmdline.Label) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 2 with ThreadSafe

use of com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe in project bazel by bazelbuild.

the class SkyQueryEnvironment method getRBuildFiles.

/**
   * Calculates the set of {@link Package} objects, represented as source file targets, that depend
   * on the given list of BUILD files and subincludes (other files are filtered out).
   */
@ThreadSafe
QueryTaskFuture<Void> getRBuildFiles(Collection<PathFragment> fileIdentifiers, Callback<Target> callback) {
    try {
        Collection<SkyKey> files = getSkyKeysForFileFragments(fileIdentifiers);
        Uniquifier<SkyKey> keyUniquifier = new UniquifierImpl<>(SkyKeyKeyExtractor.INSTANCE, /*concurrencyLevel=*/
        1);
        Collection<SkyKey> current = keyUniquifier.unique(graph.getSuccessfulValues(files).keySet());
        Set<SkyKey> resultKeys = CompactHashSet.create();
        while (!current.isEmpty()) {
            Collection<Iterable<SkyKey>> reverseDeps = graph.getReverseDeps(current).values();
            current = new HashSet<>();
            for (SkyKey rdep : Iterables.concat(reverseDeps)) {
                if (rdep.functionName().equals(SkyFunctions.PACKAGE)) {
                    resultKeys.add(rdep);
                    // too.
                    if (rdep.equals(PackageValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER))) {
                        if (keyUniquifier.unique(rdep)) {
                            current.add(rdep);
                        }
                    }
                } else if (!rdep.functionName().equals(SkyFunctions.PACKAGE_LOOKUP)) {
                    // to rbuildfiles.
                    if (keyUniquifier.unique(rdep)) {
                        current.add(rdep);
                    }
                }
            }
            if (resultKeys.size() >= BATCH_CALLBACK_SIZE) {
                for (Iterable<SkyKey> batch : Iterables.partition(resultKeys, BATCH_CALLBACK_SIZE)) {
                    callback.process(getBuildFilesForPackageValues(graph.getSuccessfulValues(batch).values()));
                }
                resultKeys.clear();
            }
        }
        callback.process(getBuildFilesForPackageValues(graph.getSuccessfulValues(resultKeys).values()));
        return immediateSuccessfulFuture(null);
    } catch (QueryException e) {
        return immediateFailedFuture(e);
    } catch (InterruptedException e) {
        return immediateCancelledFuture();
    }
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) MinDepthUniquifierImpl(com.google.devtools.build.lib.query2.engine.QueryUtil.MinDepthUniquifierImpl) UniquifierImpl(com.google.devtools.build.lib.query2.engine.QueryUtil.UniquifierImpl) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 3 with ThreadSafe

use of com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe in project bazel by bazelbuild.

the class SkyQueryEnvironment method bulkGetPackages.

@ThreadSafe
public Map<PackageIdentifier, Package> bulkGetPackages(Iterable<PackageIdentifier> pkgIds) throws InterruptedException {
    Set<SkyKey> pkgKeys = ImmutableSet.copyOf(PackageValue.keys(pkgIds));
    ImmutableMap.Builder<PackageIdentifier, Package> pkgResults = ImmutableMap.builder();
    Map<SkyKey, SkyValue> packages = graph.getSuccessfulValues(pkgKeys);
    for (Map.Entry<SkyKey, SkyValue> pkgEntry : packages.entrySet()) {
        PackageIdentifier pkgId = (PackageIdentifier) pkgEntry.getKey().argument();
        PackageValue pkgValue = (PackageValue) pkgEntry.getValue();
        pkgResults.put(pkgId, Preconditions.checkNotNull(pkgValue.getPackage(), pkgId));
    }
    return pkgResults.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) SkyValue(com.google.devtools.build.skyframe.SkyValue) PackageIdentifier(com.google.devtools.build.lib.cmdline.PackageIdentifier) PackageValue(com.google.devtools.build.lib.skyframe.PackageValue) Package(com.google.devtools.build.lib.packages.Package) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ImmutableMap(com.google.common.collect.ImmutableMap) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Example 4 with ThreadSafe

use of com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe in project bazel by bazelbuild.

the class FileSystemUtils method moveFile.

/**
   * Moves the file from location "from" to location "to", while overwriting a
   * potentially existing "to". File's last modified time, executable and
   * writable bits are also preserved.
   *
   * <p>If no error occurs, the method returns normally. If a parent directory does
   * not exist, a FileNotFoundException is thrown. An IOException is thrown when
   * other erroneous situations occur. (e.g. read errors)
   */
// but not atomic
@ThreadSafe
public static void moveFile(Path from, Path to) throws IOException {
    long mtime = from.getLastModifiedTime();
    boolean writable = from.isWritable();
    boolean executable = from.isExecutable();
    // We don't try-catch here for better performance.
    to.delete();
    try {
        from.renameTo(to);
    } catch (IOException e) {
        asByteSource(from).copyTo(asByteSink(to));
        if (!from.delete()) {
            if (!to.delete()) {
                throw new IOException("Unable to delete " + to);
            }
            throw new IOException("Unable to delete " + from);
        }
    }
    // Preserve mtime.
    to.setLastModifiedTime(mtime);
    if (!writable) {
        // Make file read-only if original was read-only.
        to.setWritable(false);
    }
    // Copy executable bit.
    to.setExecutable(executable);
}
Also used : IOException(java.io.IOException) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe) ConditionallyThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ConditionallyThreadSafe)

Example 5 with ThreadSafe

use of com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe in project bazel by bazelbuild.

the class TargetPatternValue method keys.

/**
   * Returns an iterable of {@link TargetPatternSkyKeyOrException}, with {@link TargetPatternKey}
   * arguments, in the same order as the list of patterns provided as input. If a provided pattern
   * fails to parse, the element in the returned iterable corresponding to it will throw when its
   * {@link TargetPatternSkyKeyOrException#getSkyKey} method is called.
   *
   * @param patterns The list of patterns, eg "-foo/biz...". If a pattern's first character is "-",
   *     it is treated as a negative pattern.
   * @param policy The filtering policy, eg "only return test targets"
   * @param offset The offset to apply to relative target patterns.
   */
@ThreadSafe
public static Iterable<TargetPatternSkyKeyOrException> keys(List<String> patterns, FilteringPolicy policy, String offset) {
    TargetPattern.Parser parser = new TargetPattern.Parser(offset);
    ImmutableList.Builder<TargetPatternSkyKeyOrException> builder = ImmutableList.builder();
    for (String pattern : patterns) {
        boolean positive = !pattern.startsWith("-");
        String absoluteValueOfPattern = positive ? pattern : pattern.substring(1);
        TargetPattern targetPattern;
        try {
            targetPattern = parser.parse(absoluteValueOfPattern);
        } catch (TargetParsingException e) {
            builder.add(new TargetPatternSkyKeyException(e, absoluteValueOfPattern));
            continue;
        }
        TargetPatternKey targetPatternKey = new TargetPatternKey(targetPattern, positive ? policy : FilteringPolicies.NO_FILTER, /*isNegative=*/
        !positive, offset, ImmutableSet.<PathFragment>of());
        SkyKey skyKey = SkyKey.create(SkyFunctions.TARGET_PATTERN, targetPatternKey);
        builder.add(new TargetPatternSkyKeyValue(skyKey));
    }
    return builder.build();
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) ImmutableList(com.google.common.collect.ImmutableList) TargetPattern(com.google.devtools.build.lib.cmdline.TargetPattern) TargetParsingException(com.google.devtools.build.lib.cmdline.TargetParsingException) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)

Aggregations

ThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe)12 SkyKey (com.google.devtools.build.skyframe.SkyKey)6 TargetParsingException (com.google.devtools.build.lib.cmdline.TargetParsingException)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Artifact (com.google.devtools.build.lib.actions.Artifact)2 Label (com.google.devtools.build.lib.cmdline.Label)2 TargetPattern (com.google.devtools.build.lib.cmdline.TargetPattern)2 CompactHashSet (com.google.devtools.build.lib.collect.CompactHashSet)2 ConditionallyThreadSafe (com.google.devtools.build.lib.concurrent.ThreadSafety.ConditionallyThreadSafe)2 Package (com.google.devtools.build.lib.packages.Package)2 Target (com.google.devtools.build.lib.packages.Target)2 QueryException (com.google.devtools.build.lib.query2.engine.QueryException)2 PackageValue (com.google.devtools.build.lib.skyframe.PackageValue)2 SkyValue (com.google.devtools.build.skyframe.SkyValue)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2