Search in sources :

Example 11 with RuleKeyAppendable

use of com.facebook.buck.rules.RuleKeyAppendable in project buck by facebook.

the class RuleKeyCacheRecyclerTest method overflowWatchEventInvalidatesEverything.

@Test
public void overflowWatchEventInvalidatesEverything() {
    DefaultRuleKeyCache<Void> cache = new DefaultRuleKeyCache<>();
    // Create a rule key appendable with an input and cache it.
    RuleKeyInput input1 = RuleKeyInput.of(FILESYSTEM, FILESYSTEM.getPath("input1"));
    RuleKeyAppendable appendable1 = sink -> {
    };
    cache.get(appendable1, a -> new RuleKeyResult<>(null, ImmutableList.of(), ImmutableList.of(input1)));
    // Create another rule key appendable with an input and cache it.
    RuleKeyInput input2 = RuleKeyInput.of(FILESYSTEM, FILESYSTEM.getPath("input2"));
    RuleKeyAppendable appendable2 = sink -> {
    };
    cache.get(appendable2, a -> new RuleKeyResult<>(null, ImmutableList.of(), ImmutableList.of(input2)));
    RuleKeyCacheRecycler<Void> recycler = RuleKeyCacheRecycler.createAndRegister(EVENT_BUS, cache, ImmutableSet.of(FILESYSTEM));
    // Verify that everything is cached before the overflow event.
    assertTrue(cache.isCached(appendable1));
    assertTrue(cache.isCached(appendable2));
    // Send an overflow event and verify everything was invalidated.
    recycler.onFilesystemChange(WatchEventsForTests.createOverflowEvent());
    assertFalse(cache.isCached(appendable1));
    assertFalse(cache.isCached(appendable2));
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) ImmutableSet(com.google.common.collect.ImmutableSet) ActionGraph(com.facebook.buck.rules.ActionGraph) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Assert.assertTrue(org.junit.Assert.assertTrue) WatchEventsForTests(com.facebook.buck.testutil.WatchEventsForTests) Test(org.junit.Test) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) EventBus(com.google.common.eventbus.EventBus) FakeClock(com.facebook.buck.timing.FakeClock) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) ImmutableList(com.google.common.collect.ImmutableList) BuildId(com.facebook.buck.model.BuildId) Assert.assertFalse(org.junit.Assert.assertFalse) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable) Test(org.junit.Test)

Example 12 with RuleKeyAppendable

use of com.facebook.buck.rules.RuleKeyAppendable in project buck by facebook.

the class DefaultRuleKeyFactory method newBuilder.

private RuleKeyBuilder<RuleKeyResult<RuleKey>> newBuilder() {
    return new RuleKeyBuilder<RuleKeyResult<RuleKey>>(ruleFinder, pathResolver, hashLoader) {

        private final ImmutableList.Builder<Object> deps = ImmutableList.builder();

        private final ImmutableList.Builder<RuleKeyInput> inputs = ImmutableList.builder();

        @Override
        protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setBuildRule(BuildRule rule) {
            // Record the `BuildRule` as an immediate dep.
            deps.add(rule);
            return setBuildRuleKey(DefaultRuleKeyFactory.this.build(rule));
        }

        private RuleKeyResult<RuleKey> calculateRuleKeyAppendableKey(RuleKeyAppendable appendable) {
            RuleKeyBuilder<RuleKeyResult<RuleKey>> subKeyBuilder = newBuilder();
            appendable.appendToRuleKey(subKeyBuilder);
            return subKeyBuilder.build();
        }

        @Override
        protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setAppendableRuleKey(RuleKeyAppendable appendable) {
            // Record the `RuleKeyAppendable` as an immediate dep.
            deps.add(appendable);
            // Calculate the rule key for the rule key appendable.
            RuleKey ruleKey = ruleKeyCache.get(appendable, this::calculateRuleKeyAppendableKey);
            return setAppendableRuleKey(ruleKey);
        }

        @Override
        protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setSourcePath(SourcePath sourcePath) throws IOException {
            if (sourcePath instanceof BuildTargetSourcePath) {
                return setSourcePathAsRule((BuildTargetSourcePath<?>) sourcePath);
            } else {
                // Add `PathSourcePath`s to our tracked inputs.
                pathResolver.getPathSourcePath(sourcePath).ifPresent(path -> inputs.add(RuleKeyInput.of(path.getFilesystem(), path.getRelativePath())));
                return setSourcePathDirectly(sourcePath);
            }
        }

        @Override
        protected RuleKeyBuilder<RuleKeyResult<RuleKey>> setNonHashingSourcePath(SourcePath sourcePath) {
            try {
                // changes to dependent rulekeys.
                return setSourcePath(sourcePath);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public RuleKeyResult<RuleKey> build() {
            return new RuleKeyResult<>(buildRuleKey(), deps.build(), inputs.build());
        }
    };
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) RuleKey(com.facebook.buck.rules.RuleKey) AbstractBuildRule(com.facebook.buck.rules.AbstractBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) IOException(java.io.IOException) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath)

Aggregations

RuleKeyAppendable (com.facebook.buck.rules.RuleKeyAppendable)12 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)7 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)7 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableSet (com.google.common.collect.ImmutableSet)7 BuildRule (com.facebook.buck.rules.BuildRule)6 SourcePath (com.facebook.buck.rules.SourcePath)6 Test (org.junit.Test)6 BuckEventBus (com.facebook.buck.event.BuckEventBus)5 BuildId (com.facebook.buck.model.BuildId)5 ActionGraph (com.facebook.buck.rules.ActionGraph)5 RuleKey (com.facebook.buck.rules.RuleKey)5 WatchEventsForTests (com.facebook.buck.testutil.WatchEventsForTests)5 FakeClock (com.facebook.buck.timing.FakeClock)5 EventBus (com.google.common.eventbus.EventBus)5 StandardWatchEventKinds (java.nio.file.StandardWatchEventKinds)5 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)4 PathSourcePath (com.facebook.buck.rules.PathSourcePath)4 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)4 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)4