Example usage for com.google.common.hash Hashing combineOrdered

List of usage examples for com.google.common.hash Hashing combineOrdered

Introduction

In this page you can find the example usage for com.google.common.hash Hashing combineOrdered.

Prototype

public static HashCode combineOrdered(Iterable<HashCode> hashCodes) 

Source Link

Document

Returns a hash code, having the same bit length as each of the input hash codes, that combines the information of these hash codes in an ordered fashion.

Usage

From source file:io.druid.server.router.RendezvousHasher.java

public <KeyType> String chooseNode(Set<String> nodeIds, byte[] key) {
    if (nodeIds.isEmpty()) {
        return null;
    }//from  w  w w.j  a v a 2 s.c  o m

    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;
}

From source file:org.apache.druid.server.router.RendezvousHasher.java

public <KeyType> String chooseNode(Set<String> nodeIds, byte[] key) {
    if (nodeIds.isEmpty()) {
        return null;
    }/*  w  w w . j a v  a2 s . c o  m*/

    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;
}

From source file:com.facebook.buck.android.UberRDotJava.java

public Optional<DexWithClasses> getRDotJavaDexWithClasses() {
    Preconditions.checkState(rDotJavaNeedsDexing,
            "Error trying to get R.java dex file: R.java is not supposed to be dexed.");

    final Optional<Integer> linearAllocSizeEstimate = buildOutputInitializer
            .getBuildOutput().rDotJavaDexLinearAllocEstimate;
    if (!linearAllocSizeEstimate.isPresent()) {
        return Optional.absent();
    }//from  w  w w.  j a  v a2  s  .c om

    return Optional.<DexWithClasses>of(new DexWithClasses() {
        @Override
        public Path getPathToDexFile() {
            return getPathToRDotJavaDex();
        }

        @Override
        public ImmutableSet<String> getClassNames() {
            throw new RuntimeException("Since R.java is unconditionally packed in the primary dex, no"
                    + "one should call this method.");
        }

        @Override
        public Sha1HashCode getClassesHash() {
            return Sha1HashCode.fromHashCode(Hashing.combineOrdered(getClassNamesToHashes().values()));
        }

        @Override
        public int getSizeEstimate() {
            return linearAllocSizeEstimate.get();
        }
    });
}