use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class DirArtifactCacheTest method testDeleteAfterStoreIfFull.
@Test
public void testDeleteAfterStoreIfFull() throws IOException {
Path cacheDir = tmpDir.newFolder();
Path fileX = tmpDir.newFile("x");
Path fileY = tmpDir.newFile("y");
Path fileZ = tmpDir.newFile("z");
fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0), fileY, HashCode.fromInt(1), fileZ, HashCode.fromInt(2)));
// The reason max size is 9 bytes is because a 1-byte entry actually takes 6 bytes to store.
// If the cache trims the size down to 2/3 (6 bytes) every time it hits the max it means after
// every store only the most recent artifact should be left.
dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), cacheDir, /* doStore */
true, /* maxCacheSizeBytes */
Optional.of(9L));
Files.write(fileX, "x".getBytes(UTF_8));
Files.write(fileY, "y".getBytes(UTF_8));
Files.write(fileZ, "z".getBytes(UTF_8));
BuildRule inputRuleX = new BuildRuleForTest(fileX);
BuildRule inputRuleY = new BuildRuleForTest(fileY);
BuildRule inputRuleZ = new BuildRuleForTest(fileZ);
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
ruleResolver.addToIndex(inputRuleX);
ruleResolver.addToIndex(inputRuleY);
ruleResolver.addToIndex(inputRuleZ);
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
DefaultRuleKeyFactory fakeRuleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder);
RuleKey ruleKeyX = fakeRuleKeyFactory.build(inputRuleX);
RuleKey ruleKeyY = fakeRuleKeyFactory.build(inputRuleY);
RuleKey ruleKeyZ = fakeRuleKeyFactory.build(inputRuleZ);
dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.notBorrowablePath(fileX));
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyX, Optional.empty()), "lastAccessTime", FileTime.fromMillis(0));
Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyX, Optional.of(".metadata")), "lastAccessTime", FileTime.fromMillis(0));
dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyY).build(), BorrowablePath.notBorrowablePath(fileY));
assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyY, Optional.empty()), "lastAccessTime", FileTime.fromMillis(1000));
Files.setAttribute(dirArtifactCache.getPathForRuleKey(ruleKeyY, Optional.of(".metadata")), "lastAccessTime", FileTime.fromMillis(1000));
dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyZ).build(), BorrowablePath.notBorrowablePath(fileZ));
assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyZ, LazyPath.ofInstance(fileZ)).getType());
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class DirArtifactCacheTest method testCacheStoresAndFetchHits.
@Test
public void testCacheStoresAndFetchHits() throws IOException {
Path cacheDir = tmpDir.newFolder();
Path fileX = tmpDir.newFile("x");
Path fileY = tmpDir.newFile("y");
Path fileZ = tmpDir.newFile("z");
fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0), fileY, HashCode.fromInt(1), fileZ, HashCode.fromInt(2)));
dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), Paths.get("."), /* doStore */
true, /* maxCacheSizeBytes */
Optional.empty());
Files.write(fileX, "x".getBytes(UTF_8));
Files.write(fileY, "y".getBytes(UTF_8));
Files.write(fileZ, "x".getBytes(UTF_8));
BuildRule inputRuleX = new BuildRuleForTest(fileX);
BuildRule inputRuleY = new BuildRuleForTest(fileY);
BuildRule inputRuleZ = new BuildRuleForTest(fileZ);
assertFalse(inputRuleX.equals(inputRuleY));
assertFalse(inputRuleX.equals(inputRuleZ));
assertFalse(inputRuleY.equals(inputRuleZ));
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
ruleResolver.addToIndex(inputRuleX);
ruleResolver.addToIndex(inputRuleY);
ruleResolver.addToIndex(inputRuleZ);
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
DefaultRuleKeyFactory fakeRuleKeyFactory = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder);
RuleKey ruleKeyX = fakeRuleKeyFactory.build(inputRuleX);
RuleKey ruleKeyY = fakeRuleKeyFactory.build(inputRuleY);
RuleKey ruleKeyZ = fakeRuleKeyFactory.build(inputRuleZ);
assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
assertEquals(CacheResultType.MISS, dirArtifactCache.fetch(ruleKeyZ, LazyPath.ofInstance(fileZ)).getType());
dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.notBorrowablePath(fileX));
dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyY).build(), BorrowablePath.notBorrowablePath(fileY));
dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyZ).build(), BorrowablePath.notBorrowablePath(fileZ));
Files.delete(fileX);
Files.delete(fileY);
Files.delete(fileZ);
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyY, LazyPath.ofInstance(fileY)).getType());
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyZ, LazyPath.ofInstance(fileZ)).getType());
assertEquals(inputRuleX, new BuildRuleForTest(fileX));
assertEquals(inputRuleY, new BuildRuleForTest(fileY));
assertEquals(inputRuleZ, new BuildRuleForTest(fileZ));
ImmutableList<Path> cachedFiles = ImmutableList.copyOf(dirArtifactCache.getAllFilesInCache());
assertEquals(6, cachedFiles.size());
ImmutableSet<String> filenames = cachedFiles.stream().map(input -> input.getFileName().toString()).collect(MoreCollectors.toImmutableSet());
for (RuleKey ruleKey : ImmutableSet.of(ruleKeyX, ruleKeyY, ruleKeyZ)) {
assertThat(filenames, Matchers.hasItem(ruleKey.toString()));
assertThat(filenames, Matchers.hasItem(ruleKey.toString() + ".metadata"));
}
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class DirArtifactCacheTest method testCacheStoreAndFetchHit.
@Test
public void testCacheStoreAndFetchHit() throws IOException {
Path cacheDir = tmpDir.newFolder();
Path fileX = tmpDir.newFile("x");
fileHashCache = new FakeFileHashCache(ImmutableMap.of(fileX, HashCode.fromInt(0)));
dirArtifactCache = new DirArtifactCache("dir", new ProjectFilesystem(cacheDir), Paths.get("."), /* doStore */
true, /* maxCacheSizeBytes */
Optional.empty());
Files.write(fileX, "x".getBytes(UTF_8));
BuildRule inputRuleX = new BuildRuleForTest(fileX);
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
ruleResolver.addToIndex(inputRuleX);
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
RuleKey ruleKeyX = new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder).build(inputRuleX);
dirArtifactCache.store(ArtifactInfo.builder().addRuleKeys(ruleKeyX).build(), BorrowablePath.notBorrowablePath(fileX));
// Test that artifact overwrite works.
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
assertEquals(inputRuleX, new BuildRuleForTest(fileX));
// Test that artifact creation works.
Files.delete(fileX);
assertEquals(CacheResultType.HIT, dirArtifactCache.fetch(ruleKeyX, LazyPath.ofInstance(fileX)).getType());
assertEquals(inputRuleX, new BuildRuleForTest(fileX));
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class AndroidResourceTest method testRuleKeyForDifferentInputFilenames.
@Test
public void testRuleKeyForDifferentInputFilenames() throws Exception {
BuildTarget buildTarget = BuildTargetFactory.newInstance("//java/src/com/facebook/base:res");
Function<Path, BuildRuleResolver> createResourceRule = (Path resourcePath) -> {
FakeProjectFilesystem projectFilesystem = new FakeProjectFilesystem();
projectFilesystem.createNewFile(resourcePath);
projectFilesystem.createNewFile(Paths.get("java/src/com/facebook/base/assets/drawable/B.xml"));
TargetNode<?, ?> resourceNode = AndroidResourceBuilder.createBuilder(buildTarget, projectFilesystem).setRes(new FakeSourcePath(projectFilesystem, "java/src/com/facebook/base/res")).setRDotJavaPackage("com.facebook").setAssets(new FakeSourcePath(projectFilesystem, "java/src/com/facebook/base/assets")).setManifest(new PathSourcePath(projectFilesystem, Paths.get("java/src/com/facebook/base/AndroidManifest.xml"))).build();
TargetGraph targetGraph = TargetGraphFactory.newInstance(resourceNode);
return new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
};
FakeFileHashCache hashCache = FakeFileHashCache.createFromStrings(ImmutableMap.of("java/src/com/facebook/base/AndroidManifest.xml", "bbbbbbbbbb", "java/src/com/facebook/base/assets/drawable/B.xml", "aaaaaaaaaaaa", "java/src/com/facebook/base/res/drawable/A.xml", "dddddddddd", "java/src/com/facebook/base/res/drawable/C.xml", "eeeeeeeeee"));
BuildRuleResolver resolver1 = createResourceRule.apply(Paths.get("java/src/com/facebook/base/res/drawable/A.xml"));
BuildRuleResolver resolver2 = createResourceRule.apply(Paths.get("java/src/com/facebook/base/res/drawable/C.xml"));
BuildRule androidResource1 = resolver1.requireRule(buildTarget);
SourcePathRuleFinder ruleFinder1 = new SourcePathRuleFinder(resolver1);
SourcePathResolver pathResolver1 = new SourcePathResolver(ruleFinder1);
BuildRule androidResource2 = resolver2.requireRule(buildTarget);
SourcePathRuleFinder ruleFinder2 = new SourcePathRuleFinder(resolver2);
SourcePathResolver pathResolver2 = new SourcePathResolver(ruleFinder2);
RuleKey ruleKey1 = new DefaultRuleKeyFactory(0, hashCache, pathResolver1, ruleFinder1).build(androidResource1);
RuleKey ruleKey2 = new DefaultRuleKeyFactory(0, hashCache, pathResolver2, ruleFinder2).build(androidResource2);
assertNotEquals("The two android_resource rules should have different rule keys.", ruleKey1, ruleKey2);
}
use of com.facebook.buck.rules.keys.DefaultRuleKeyFactory in project buck by facebook.
the class NdkCxxPlatformTest method constructLinkRuleKeys.
// Create and return some rule keys from a dummy source for the given platforms.
private ImmutableMap<NdkCxxPlatforms.TargetCpuType, RuleKey> constructLinkRuleKeys(ImmutableMap<NdkCxxPlatforms.TargetCpuType, NdkCxxPlatform> cxxPlatforms) throws NoSuchBuildTargetException {
BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
DefaultRuleKeyFactory ruleKeyFactory = new DefaultRuleKeyFactory(0, FakeFileHashCache.createFromStrings(ImmutableMap.<String, String>builder().put("input.o", Strings.repeat("a", 40)).build()), pathResolver, ruleFinder);
BuildTarget target = BuildTargetFactory.newInstance("//:target");
ImmutableMap.Builder<NdkCxxPlatforms.TargetCpuType, RuleKey> ruleKeys = ImmutableMap.builder();
for (Map.Entry<NdkCxxPlatforms.TargetCpuType, NdkCxxPlatform> entry : cxxPlatforms.entrySet()) {
BuildRule rule = CxxLinkableEnhancer.createCxxLinkableBuildRule(CxxPlatformUtils.DEFAULT_CONFIG, entry.getValue().getCxxPlatform(), new FakeBuildRuleParamsBuilder(target).build(), resolver, pathResolver, ruleFinder, target, Linker.LinkType.EXECUTABLE, Optional.empty(), Paths.get("output"), Linker.LinkableDepType.SHARED, ImmutableList.of(), Optional.empty(), Optional.empty(), ImmutableSet.of(), NativeLinkableInput.builder().setArgs(SourcePathArg.from(new FakeSourcePath("input.o"))).build());
ruleKeys.put(entry.getKey(), ruleKeyFactory.build(rule));
}
return ruleKeys.build();
}
Aggregations