Search in sources :

Example 46 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class HttpArtifactCacheBinaryProtocol method createMetadataHeader.

@VisibleForTesting
static byte[] createMetadataHeader(ImmutableSet<RuleKey> ruleKeys, ImmutableMap<String, String> metadata, ByteSource data) throws IOException {
    ByteArrayOutputStream rawOut = new ByteArrayOutputStream();
    Hasher hasher = HASH_FUNCTION.newHasher();
    try (DataOutputStream out = new DataOutputStream(new HasherOutputStream(hasher, rawOut))) {
        // Write the rule keys to the raw metadata, including them in the end-to-end checksum.
        out.writeInt(ruleKeys.size());
        for (RuleKey ruleKey : ruleKeys) {
            out.writeUTF(ruleKey.toString());
        }
        // Write out the metadata map to the raw metadata, including it in the end-to-end checksum.
        out.writeInt(metadata.size());
        for (Map.Entry<String, String> ent : metadata.entrySet()) {
            out.writeUTF(ent.getKey());
            byte[] val = ent.getValue().getBytes(Charsets.UTF_8);
            out.writeInt(val.length);
            out.write(val);
            if (out.size() > MAX_METADATA_HEADER_SIZE) {
                throw new IOException("Metadata header too big.");
            }
        }
    }
    // Add the file data contents to the end-to-end checksum.
    data.copyTo(new HasherOutputStream(hasher, ByteStreams.nullOutputStream()));
    // Finish the checksum, adding it to the raw metadata
    rawOut.write(hasher.hash().asBytes());
    // Finally, base64 encode the raw bytes to make usable in a HTTP header.
    byte[] bytes = rawOut.toByteArray();
    if (bytes.length > MAX_METADATA_HEADER_SIZE) {
        throw new IOException("Metadata header too big.");
    }
    return bytes;
}
Also used : Hasher(com.google.common.hash.Hasher) HasherOutputStream(com.facebook.buck.util.hash.HasherOutputStream) RuleKey(com.facebook.buck.rules.RuleKey) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 47 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class StringHashingTest method separateHashDifferentFromSameStringsConcatenated.

@Test
public void separateHashDifferentFromSameStringsConcatenated() {
    Hasher separateHasher = Hashing.sha1().newHasher();
    StringHashing.hashStringAndLength(separateHasher, "foo");
    StringHashing.hashStringAndLength(separateHasher, "bar");
    Hasher concatenatedHasher = Hashing.sha1().newHasher();
    StringHashing.hashStringAndLength(separateHasher, "foobar");
    assertThat(separateHasher.hash(), not(equalTo(concatenatedHasher.hash())));
}
Also used : Hasher(com.google.common.hash.Hasher) Test(org.junit.Test)

Example 48 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class StringHashingTest method emptyStringHasExpectedHash.

@Test
public void emptyStringHasExpectedHash() {
    Hasher hasher = Hashing.sha1().newHasher();
    StringHashing.hashStringAndLength(hasher, "");
    assertThat(hasher.hash(), equalTo(HashCode.fromString("9069ca78e7450a285173431b3e52c5c25299e473")));
}
Also used : Hasher(com.google.common.hash.Hasher) Test(org.junit.Test)

Example 49 with Hasher

use of com.google.common.hash.Hasher in project druid by druid-io.

the class CardinalityAggregator method hashRow.

static void hashRow(ColumnSelectorPlus<CardinalityAggregatorColumnSelectorStrategy>[] selectorPluses, HyperLogLogCollector collector) {
    final Hasher hasher = hashFn.newHasher();
    for (int k = 0; k < selectorPluses.length; ++k) {
        if (k != 0) {
            hasher.putByte((byte) 0);
        }
        ColumnSelectorPlus<CardinalityAggregatorColumnSelectorStrategy> selectorPlus = selectorPluses[k];
        selectorPlus.getColumnSelectorStrategy().hashRow(selectorPlus.getSelector(), hasher);
    }
    collector.add(hasher.hash().asBytes());
}
Also used : Hasher(com.google.common.hash.Hasher) CardinalityAggregatorColumnSelectorStrategy(io.druid.query.aggregation.cardinality.types.CardinalityAggregatorColumnSelectorStrategy)

Example 50 with Hasher

use of com.google.common.hash.Hasher in project elastic-job by dangdangdotcom.

the class RegistryCenterFactory method createCoordinatorRegistryCenter.

/**
     * 创建注册中心.
     *
     * @param connectString 注册中心连接字符串
     * @param namespace 注册中心命名空间
     * @param digest 注册中心凭证
     * @return 注册中心对象
     */
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(final String connectString, final String namespace, final Optional<String> digest) {
    Hasher hasher = Hashing.md5().newHasher().putString(connectString, Charsets.UTF_8).putString(namespace, Charsets.UTF_8);
    if (digest.isPresent()) {
        hasher.putString(digest.get(), Charsets.UTF_8);
    }
    HashCode hashCode = hasher.hash();
    CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
    if (null != result) {
        return result;
    }
    ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
    if (digest.isPresent()) {
        zkConfig.setDigest(digest.get());
    }
    result = new ZookeeperRegistryCenter(zkConfig);
    result.init();
    REG_CENTER_REGISTRY.put(hashCode, result);
    return result;
}
Also used : ZookeeperRegistryCenter(com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) ZookeeperConfiguration(com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration) CoordinatorRegistryCenter(com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter)

Aggregations

Hasher (com.google.common.hash.Hasher)59 IOException (java.io.IOException)10 HashCode (com.google.common.hash.HashCode)9 Test (org.junit.Test)9 InputStream (java.io.InputStream)5 Map (java.util.Map)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 HashFunction (com.google.common.hash.HashFunction)3 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)2 ArchiveMemberPath (com.facebook.buck.io.ArchiveMemberPath)2 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)2 BuildRuleType (com.facebook.buck.rules.BuildRuleType)2 Cell (com.facebook.buck.rules.Cell)2 ParamInfoException (com.facebook.buck.rules.ParamInfoException)2