Search in sources :

Example 1 with RuleKeyAppendable

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

the class RuleKeyBuilder method setReflectively.

/** Recursively serializes the value. Serialization of the key is handled outside. */
protected RuleKeyBuilder<RULE_KEY> setReflectively(@Nullable Object val) {
    if (val instanceof RuleKeyAppendable) {
        return setAppendableRuleKey((RuleKeyAppendable) val);
    }
    if (val instanceof BuildRule) {
        return setBuildRule((BuildRule) val);
    }
    if (val instanceof Supplier) {
        try (Scope containerScope = scopedHasher.wrapperScope(Wrapper.SUPPLIER)) {
            Object newVal = ((Supplier<?>) val).get();
            return setReflectively(newVal);
        }
    }
    if (val instanceof Optional) {
        Object o = ((Optional<?>) val).orElse(null);
        try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.OPTIONAL)) {
            return setReflectively(o);
        }
    }
    if (val instanceof Either) {
        Either<?, ?> either = (Either<?, ?>) val;
        if (either.isLeft()) {
            try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_LEFT)) {
                return setReflectively(either.getLeft());
            }
        } else {
            try (Scope wraperScope = scopedHasher.wrapperScope(Wrapper.EITHER_RIGHT)) {
                return setReflectively(either.getRight());
            }
        }
    }
    // Note {@link java.nio.file.Path} implements "Iterable", so we explicitly exclude it here.
    if (val instanceof Iterable && !(val instanceof Path)) {
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
            for (Object element : (Iterable<?>) val) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(element);
                }
            }
            return this;
        }
    }
    if (val instanceof Iterator) {
        Iterator<?> iterator = (Iterator<?>) val;
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.LIST)) {
            while (iterator.hasNext()) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(iterator.next());
                }
            }
        }
        return this;
    }
    if (val instanceof Map) {
        if (!(val instanceof SortedMap || val instanceof ImmutableMap)) {
            logger.warn("Adding an unsorted map to the rule key. " + "Expect unstable ordering and caches misses: %s", val);
        }
        try (ContainerScope containerScope = scopedHasher.containerScope(Container.MAP)) {
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) val).entrySet()) {
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(entry.getKey());
                }
                try (Scope elementScope = containerScope.elementScope()) {
                    setReflectively(entry.getValue());
                }
            }
        }
        return this;
    }
    if (val instanceof Path) {
        throw new HumanReadableException("It's not possible to reliably disambiguate Paths. They are disallowed from rule keys");
    }
    if (val instanceof SourcePath) {
        try {
            return setSourcePath((SourcePath) val);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (val instanceof NonHashableSourcePathContainer) {
        SourcePath sourcePath = ((NonHashableSourcePathContainer) val).getSourcePath();
        return setNonHashingSourcePath(sourcePath);
    }
    if (val instanceof SourceWithFlags) {
        SourceWithFlags source = (SourceWithFlags) val;
        try {
            setSourcePath(source.getSourcePath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        setReflectively(source.getFlags());
        return this;
    }
    return setSingleValue(val);
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Optional(java.util.Optional) ContainerScope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.ContainerScope) NonHashableSourcePathContainer(com.facebook.buck.rules.NonHashableSourcePathContainer) IOException(java.io.IOException) SourceWithFlags(com.facebook.buck.rules.SourceWithFlags) ImmutableMap(com.google.common.collect.ImmutableMap) SourcePath(com.facebook.buck.rules.SourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) Scope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.Scope) ContainerScope(com.facebook.buck.rules.keys.RuleKeyScopedHasher.ContainerScope) HumanReadableException(com.facebook.buck.util.HumanReadableException) SortedMap(java.util.SortedMap) Iterator(java.util.Iterator) Either(com.facebook.buck.model.Either) BuildRule(com.facebook.buck.rules.BuildRule) Supplier(com.google.common.base.Supplier) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

Example 2 with RuleKeyAppendable

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

the class RuleKeyFieldLoader method setFields.

public void setFields(BuildRule buildRule, RuleKeyBuilder<?> builder) {
    // "." is not a valid first character for a field name, nor a valid character for rule attribute
    // name and so the following fields will never collide with other stuff.
    builder.setReflectively(".seed", seed);
    builder.setReflectively(".name", buildRule.getBuildTarget().getFullyQualifiedName());
    builder.setReflectively(".type", buildRule.getType());
    builder.setReflectively(".version", BuckVersion.getVersion());
    // We currently cache items using their full buck-out path, so make sure this is reflected in
    // the rule key.
    Path buckOutPath = buildRule.getProjectFilesystem().getBuckPaths().getConfiguredBuckOut();
    builder.setReflectively(".out", buckOutPath.toString());
    // Add in any extra details to the rule key via the rule's `appendToRuleKey` method.
    buildRule.appendToRuleKey(builder);
    // We used to require build rules to piggyback on the `RuleKeyAppendable` type to add in
    // additional details, but have since switched to using a method in the build rule class, so
    // error out if we see the `RuleKeyAppendable` being used improperly.
    Preconditions.checkArgument(!(builder instanceof RuleKeyAppendable));
    for (AlterRuleKey alterRuleKey : cache.getUnchecked(buildRule.getClass())) {
        alterRuleKey.amendKey(builder, buildRule);
    }
}
Also used : Path(java.nio.file.Path) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

Example 3 with RuleKeyAppendable

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

the class RuleKeyBuilderTest method newBuilder.

private RuleKeyBuilder<RuleKey> newBuilder() {
    Map<BuildTarget, BuildRule> ruleMap = ImmutableMap.of(TARGET_1, RULE_1, TARGET_2, RULE_2);
    Map<BuildRule, RuleKey> ruleKeyMap = ImmutableMap.of(RULE_1, RULE_KEY_1, RULE_2, RULE_KEY_2);
    Map<RuleKeyAppendable, RuleKey> appendableKeys = ImmutableMap.of(APPENDABLE_1, RULE_KEY_1, APPENDABLE_2, RULE_KEY_2);
    BuildRuleResolver ruleResolver = new FakeBuildRuleResolver(ruleMap);
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(ruleResolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    FakeFileHashCache hashCache = new FakeFileHashCache(ImmutableMap.of(FILESYSTEM.resolve(PATH_1), HashCode.fromInt(0), FILESYSTEM.resolve(PATH_2), HashCode.fromInt(42)), ImmutableMap.of(pathResolver.getAbsoluteArchiveMemberPath(ARCHIVE_PATH_1), HashCode.fromInt(0), pathResolver.getAbsoluteArchiveMemberPath(ARCHIVE_PATH_2), HashCode.fromInt(42)), ImmutableMap.of());
    RuleKeyHasher<HashCode> hasher = new GuavaRuleKeyHasher(Hashing.sha1().newHasher());
    return new RuleKeyBuilder<RuleKey>(ruleFinder, pathResolver, hashCache, hasher) {

        @Override
        protected RuleKeyBuilder<RuleKey> setBuildRule(BuildRule rule) {
            if (rule == IGNORED_RULE) {
                return this;
            }
            return setBuildRuleKey(ruleKeyMap.get(rule));
        }

        @Override
        public RuleKeyBuilder<RuleKey> setAppendableRuleKey(RuleKeyAppendable appendable) {
            if (appendable == IGNORED_APPENDABLE) {
                return this;
            }
            return setAppendableRuleKey(appendableKeys.get(appendable));
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setSourcePath(SourcePath sourcePath) throws IOException {
            if (sourcePath instanceof BuildTargetSourcePath) {
                return setSourcePathAsRule((BuildTargetSourcePath<?>) sourcePath);
            } else {
                return setSourcePathDirectly(sourcePath);
            }
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setNonHashingSourcePath(SourcePath sourcePath) {
            return setNonHashingSourcePathDirectly(sourcePath);
        }

        @Override
        public RuleKey build() {
            return buildRuleKey();
        }
    };
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildTargetSourcePath(com.facebook.buck.rules.BuildTargetSourcePath) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) HashCode(com.google.common.hash.HashCode) BuildTarget(com.facebook.buck.model.BuildTarget) BuildRule(com.facebook.buck.rules.BuildRule) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

Example 4 with RuleKeyAppendable

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

the class RuleKeyCacheRecyclerTest method getCacheWithIdenticalSettingsDoesNotInvalidate.

@Test
public void getCacheWithIdenticalSettingsDoesNotInvalidate() {
    DefaultRuleKeyCache<Void> cache = new DefaultRuleKeyCache<>();
    RuleKeyCacheRecycler<Void> recycler = RuleKeyCacheRecycler.createAndRegister(EVENT_BUS, cache, ImmutableSet.of(FILESYSTEM));
    RuleKeyAppendable appendable = sink -> {
    };
    recycler.withRecycledCache(BUCK_EVENT_BUS, SETTINGS, c -> {
        cache.get(appendable, a -> new RuleKeyResult<>(null, ImmutableList.of(), ImmutableList.of()));
    });
    assertTrue(cache.isCached(appendable));
    recycler.withRecycledCache(BUCK_EVENT_BUS, SETTINGS, c -> {
    });
    assertTrue(cache.isCached(appendable));
}
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 5 with RuleKeyAppendable

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

the class FakeInputBasedRuleKeyFactory method newInstance.

private RuleKeyBuilder<RuleKey> newInstance(final BuildRule buildRule) {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver resolver = new SourcePathResolver(ruleFinder);
    return new RuleKeyBuilder<RuleKey>(ruleFinder, resolver, fileHashCache) {

        @Override
        protected RuleKeyBuilder<RuleKey> setBuildRule(BuildRule rule) {
            return this;
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setReflectively(@Nullable Object val) {
            return this;
        }

        @Override
        public RuleKeyBuilder<RuleKey> setAppendableRuleKey(RuleKeyAppendable appendable) {
            return this;
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setSourcePath(SourcePath sourcePath) {
            return this;
        }

        @Override
        protected RuleKeyBuilder<RuleKey> setNonHashingSourcePath(SourcePath sourcePath) {
            return setNonHashingSourcePathDirectly(sourcePath);
        }

        @Override
        public RuleKey build() {
            if (oversized.contains(buildRule.getBuildTarget())) {
                throw new SizeLimiter.SizeLimitException();
            }
            return ruleKeys.get(buildRule.getBuildTarget());
        }
    };
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) RuleKey(com.facebook.buck.rules.RuleKey) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Nullable(javax.annotation.Nullable) RuleKeyAppendable(com.facebook.buck.rules.RuleKeyAppendable)

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