public <KeyType> String chooseNode(Set<String> nodeIds, byte[] key) { if (nodeIds.isEmpty()) { return null; } final HashCode keyHash = HASH_FN.hashBytes(key); long maxHash = Long.MIN_VALUE; String maxNode = null; for (String nodeId : nodeIds) { HashCode nodeHash = HASH_FN.hashString(nodeId, StandardCharsets.UTF_8); List<HashCode> hashes = Lists.newArrayList(nodeHash, keyHash); long combinedHash = Hashing.combineOrdered(hashes).asLong(); if (maxNode == null) { maxHash = combinedHash; maxNode = nodeId; } else if (combinedHash > maxHash) { maxHash = combinedHash; maxNode = nodeId; } } return maxNode; } }
public void testCombineOrdered_empty() { try { Hashing.combineOrdered(Collections.<HashCode>emptySet()); fail(); } catch (IllegalArgumentException expected) { } }
public void testCombineOrdered_randomHashCodes() { Random random = new Random(7); List<HashCode> hashCodes = Lists.newArrayList(); for (int i = 0; i < 10; i++) { hashCodes.add(HashCode.fromLong(random.nextLong())); } HashCode hashCode1 = Hashing.combineOrdered(hashCodes); Collections.shuffle(hashCodes, random); HashCode hashCode2 = Hashing.combineOrdered(hashCodes); assertFalse(hashCode1.equals(hashCode2)); }
public void testCombineOrdered() { HashCode hash31 = HashCode.fromInt(31); HashCode hash32 = HashCode.fromInt(32); assertEquals(hash32, Hashing.combineOrdered(ImmutableList.of(hash32))); assertEquals( HashCode.fromBytes(new byte[] {(byte) 0x80, 0, 0, 0}), Hashing.combineOrdered(ImmutableList.of(hash32, hash32))); assertEquals( HashCode.fromBytes(new byte[] {(byte) 0xa0, 0, 0, 0}), Hashing.combineOrdered(ImmutableList.of(hash32, hash32, hash32))); assertFalse( Hashing.combineOrdered(ImmutableList.of(hash31, hash32)) .equals(Hashing.combineOrdered(ImmutableList.of(hash32, hash31)))); }
public void testCombineOrdered_differentBitLengths() { try { HashCode unused = Hashing.combineOrdered(ImmutableList.of(HashCode.fromInt(32), HashCode.fromLong(32L))); fail(); } catch (IllegalArgumentException expected) { } }
private String calculateVersionHash(final Collection<ApplicationRole> applicationRoles) { HashFunction hf = Hashing.md5(); List<ApplicationRole> appRoles = new ArrayList<>(applicationRoles); Collections.sort(appRoles, ApplicationRoleNameSorter); List<HashCode> hashCodes = appRoles.stream() .map(appRole -> hf.newHasher() .putObject(appRole, applicationRoleFunnel).hash()) .collect(Collectors.toList()); if (hashCodes.isEmpty()) { return "0"; } return Hashing.combineOrdered(hashCodes).toString(); }
public <KeyType> String chooseNode(Set<String> nodeIds, byte[] key) { if (nodeIds.isEmpty()) { return null; } final HashCode keyHash = HASH_FN.hashBytes(key); long maxHash = Long.MIN_VALUE; String maxNode = null; for (String nodeId : nodeIds) { HashCode nodeHash = HASH_FN.hashString(nodeId, StandardCharsets.UTF_8); List<HashCode> hashes = Lists.newArrayList(nodeHash, keyHash); long combinedHash = Hashing.combineOrdered(hashes).asLong(); if (maxNode == null) { maxHash = combinedHash; maxNode = nodeId; } else if (combinedHash > maxHash) { maxHash = combinedHash; maxNode = nodeId; } } return maxNode; } }
public <KeyType> String chooseNode(Set<String> nodeIds, byte[] key) { if (nodeIds.isEmpty()) { return null; } final HashCode keyHash = HASH_FN.hashBytes(key); long maxHash = Long.MIN_VALUE; String maxNode = null; for (String nodeId : nodeIds) { HashCode nodeHash = HASH_FN.hashString(nodeId, Charsets.UTF_8); List<HashCode> hashes = Lists.newArrayList(nodeHash, keyHash); long combinedHash = Hashing.combineOrdered(hashes).asLong(); if (maxNode == null) { maxHash = combinedHash; maxNode = nodeId; } else if (combinedHash > maxHash) { maxHash = combinedHash; maxNode = nodeId; } } return maxNode; } }
public int hashToBucket(UUID uuid, int numBuckets) { return Hashing.consistentHash( Hashing.combineOrdered(Arrays.asList(hash.hashLong(uuid.getMostSignificantBits()), hash.hashLong(uuid.getLeastSignificantBits()))), numBuckets); }
public void testCombineOrdered_empty() { try { Hashing.combineOrdered(Collections.<HashCode>emptySet()); fail(); } catch (IllegalArgumentException expected) { } }
hashCodes.add(hashFunc.newHasher().putLong(checksum(child, hashFunc)).hash()); return hashCodes.size() > 0 ? Hashing.combineOrdered(hashCodes).padToLong() : 0;
public void testCombineOrdered_randomHashCodes() { Random random = new Random(7); List<HashCode> hashCodes = Lists.newArrayList(); for (int i = 0; i < 10; i++) { hashCodes.add(HashCode.fromLong(random.nextLong())); } HashCode hashCode1 = Hashing.combineOrdered(hashCodes); Collections.shuffle(hashCodes, random); HashCode hashCode2 = Hashing.combineOrdered(hashCodes); assertFalse(hashCode1.equals(hashCode2)); }
public void testCombineOrdered() { HashCode hash31 = HashCode.fromInt(31); HashCode hash32 = HashCode.fromInt(32); assertEquals(hash32, Hashing.combineOrdered(ImmutableList.of(hash32))); assertEquals(HashCode.fromBytes(new byte[] { (byte) 0x80, 0, 0, 0 }), Hashing.combineOrdered(ImmutableList.of(hash32, hash32))); assertEquals(HashCode.fromBytes(new byte[] { (byte) 0xa0, 0, 0, 0 }), Hashing.combineOrdered(ImmutableList.of(hash32, hash32, hash32))); assertFalse( Hashing.combineOrdered(ImmutableList.of(hash31, hash32)).equals( Hashing.combineOrdered(ImmutableList.of(hash32, hash31)))); }
public void testCombineOrdered_differentBitLengths() { try { HashCode unused = Hashing.combineOrdered(ImmutableList.of(HashCode.fromInt(32), HashCode.fromLong(32L))); fail(); } catch (IllegalArgumentException expected) { } }