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;
}
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();
}
}
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();
}
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);
}
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();
}
Aggregations