Search in sources :

Example 1 with NonHashableSourcePathContainer

use of com.facebook.buck.rules.NonHashableSourcePathContainer 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 NonHashableSourcePathContainer

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

the class InputBasedRuleKeyFactoryTest method ruleKeyDoesNotChangeIfNonHashingSourcePathContentChanges.

@Test
public void ruleKeyDoesNotChangeIfNonHashingSourcePathContentChanges() throws NoSuchBuildTargetException {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    final ProjectFilesystem fileSystem = new FakeProjectFilesystem();
    // Build a rule key with a particular hash set for the output for the above rule.
    Path filePath = fileSystem.getPath("file.txt");
    FakeFileHashCache hashCache = new FakeFileHashCache(new HashMap<>());
    hashCache.set(filePath.toAbsolutePath(), HashCode.fromInt(0));
    PathSourcePath sourcePath = new PathSourcePath(fileSystem, filePath);
    NonHashableSourcePathContainer nonHashablePath = new NonHashableSourcePathContainer(sourcePath);
    RuleKey inputKey1 = computeRuleKey(hashCache, pathResolver, ruleFinder, nonHashablePath);
    hashCache.set(filePath.toAbsolutePath(), HashCode.fromInt(1));
    RuleKey inputKey2 = computeRuleKey(hashCache, pathResolver, ruleFinder, nonHashablePath);
    assertThat(inputKey1, Matchers.equalTo(inputKey2));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) NonHashableSourcePathContainer(com.facebook.buck.rules.NonHashableSourcePathContainer) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Test(org.junit.Test)

Aggregations

NonHashableSourcePathContainer (com.facebook.buck.rules.NonHashableSourcePathContainer)2 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 Path (java.nio.file.Path)2 ArchiveMemberPath (com.facebook.buck.io.ArchiveMemberPath)1 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Either (com.facebook.buck.model.Either)1 AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)1 ArchiveMemberSourcePath (com.facebook.buck.rules.ArchiveMemberSourcePath)1 BuildRule (com.facebook.buck.rules.BuildRule)1 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)1 BuildTargetSourcePath (com.facebook.buck.rules.BuildTargetSourcePath)1 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)1 RuleKey (com.facebook.buck.rules.RuleKey)1 RuleKeyAppendable (com.facebook.buck.rules.RuleKeyAppendable)1 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)1 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)1 SourceWithFlags (com.facebook.buck.rules.SourceWithFlags)1 ContainerScope (com.facebook.buck.rules.keys.RuleKeyScopedHasher.ContainerScope)1 Scope (com.facebook.buck.rules.keys.RuleKeyScopedHasher.Scope)1