private String configurationToEtag(Configuration configuration) { return Hashing.md5() .hashInt(configuration.hashCode()) // avoid negative values .toString(); }
private String collectorsToEtag(CollectorListResponse collectors) { return Hashing.md5() .hashInt(collectors.hashCode()) // avoid negative values .toString(); } }
public void testHashIntReverseBytesVsHashBytesIntsToByteArray() { int input = 42; assertEquals( Hashing.md5().hashBytes(Ints.toByteArray(input)), Hashing.md5().hashInt(Integer.reverseBytes(input))); }
@Override public void hashValues(BaseFloatColumnValueSelector selector, HyperLogLogCollector collector) { if (NullHandling.replaceWithDefault() || !selector.isNull()) { collector.add(CardinalityAggregator.hashFn.hashInt(Float.floatToIntBits(selector.getFloat())).asBytes()); } } }
public void testHashIntVsForLoop() { int input = 42; HashCode expected = Hashing.md5().hashInt(input); Hasher hasher = Hashing.md5().newHasher(); for (int i = 0; i < 32; i += 8) { hasher.putByte((byte) (input >> i)); } HashCode actual = hasher.hash(); assertEquals(expected, actual); }
private static void assertHashIntEquivalence(HashFunction hashFunction, Random random) { int i = random.nextInt(); assertEquals(hashFunction.hashInt(i), hashFunction.newHasher().putInt(i).hash()); }
public static short getShard(int integerValue, int totalShards) { if (totalShards <= 1) { return 0; } long hash = hashFunc.hashInt(integerValue).asLong(); return _getShard(hash, totalShards); }
public void add(int value) { add(hashFunc.hashInt(value).asLong()); }
public void add(int value) { add(hashFunc.hashInt(value).asLong()); }
public void testKnownIntegerInputs() { assertHash(593689054, murmur3_32().hashInt(0)); assertHash(-189366624, murmur3_32().hashInt(-42)); assertHash(-1134849565, murmur3_32().hashInt(42)); assertHash(-1718298732, murmur3_32().hashInt(Integer.MIN_VALUE)); assertHash(-1653689534, murmur3_32().hashInt(Integer.MAX_VALUE)); }
/** * Checks that a Hasher returns the same HashCode when given the same input, and also that the * collision rate looks sane. */ static void assertInvariants(HashFunction hashFunction) { int objects = 100; Set<HashCode> hashcodes = Sets.newHashSetWithExpectedSize(objects); Random random = new Random(314159); for (int i = 0; i < objects; i++) { int value = random.nextInt(); HashCode hashcode1 = hashFunction.hashInt(value); HashCode hashcode2 = hashFunction.hashInt(value); Assert.assertEquals(hashcode1, hashcode2); // idempotent Assert.assertEquals(hashFunction.bits(), hashcode1.bits()); Assert.assertEquals(hashFunction.bits(), hashcode1.asBytes().length * 8); hashcodes.add(hashcode1); } Assert.assertTrue(hashcodes.size() > objects * 0.95); // quite relaxed test assertHashBytesThrowsCorrectExceptions(hashFunction); assertIndependentHashers(hashFunction); assertShortcutsAreEquivalent(hashFunction, 512); }
HyperLogLogCollector c = HyperLogLogCollector.makeLatestCollector(); for (int k = 0; k < 40; ++k) { c.add(fn.hashInt(++val).asBytes());
public void add(int value) { add(hashFunc.hashInt(value).asLong()); }
/** * Hash the given int in order to generate a uniform etag hash. * * @param key * Key which should be hashed * @return Computed hash */ public static String hash(int key) { return Hashing.crc32c().hashInt(key).toString(); }
/** * Computes a 32bit hash. * * @param data * the data to hash * @return a 32bit hash */ public static int hash32(final float data) { return MURMUR32.hashInt(Float.floatToRawIntBits(data)).asInt(); }
@Override public boolean apply(final Instance<AttributesMap, Serializable> instance) { int hc = hashFunction.hashInt(instance.getAttributes().hashCode()).asInt(); return Math.abs(hc) % every == 0; } }