List of usage examples for com.google.common.hash Hashing consistentHash
public static int consistentHash(long input, int buckets)
From source file:org.apache.niolex.common.guava.GuavaHashing.java
/** * @param args/*from w w w.j a v a 2 s.c om*/ */ public static void main(String[] args) { // Common Hash HashFunction hf = Hashing.goodFastHash(32); HashCode code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE); System.out.println("Code1 => " + code.asInt()); code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1985), PersonFunnel.INSTANCE); System.out.println("Code2 => " + code.asInt()); // Consistent Hashing HashFunction hf2 = Hashing.goodFastHash(64); code = hf2.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE); System.out.println("Code3 => " + code.asLong()); long hash = code.asLong(); int bucket = Hashing.consistentHash(code, 100); System.out.println("Bucket1 => " + bucket); bucket = Hashing.consistentHash(hash, 101); System.out.println("Bucket2 => " + bucket); for (int i = 0; i < 10; ++i) { System.out.println("HashTo5 => " + Hashing.consistentHash(i, 5)); System.out.println("HashTo6 => " + Hashing.consistentHash(i, 6)); } // BloomFilter BloomFilter<Person> friends = BloomFilter.create(PersonFunnel.INSTANCE, 500, 0.02); for (int i = 0; i < 500; ++i) { friends.put(new Person(i, "Jiyun", "Xie", 1984 + i)); } int k = 0; for (int i = 0; i < 500; ++i) { if (!friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) { System.out.println("Error1 => " + i); ++k; } } System.out.println("fnp => (should be 0)" + ((double) k / 500)); k = 0; for (int i = 0; i < 1000; i += 2) { if (friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) { //System.out.println("Error2 => " + i); ++k; } } System.out.println("fpp => " + ((double) k / 500)); }
From source file:google.registry.model.index.EppResourceIndexBucket.java
/** * Deterministic function that returns a bucket id based on the resource's roid. * NB: At the moment, nothing depends on this being deterministic, so we have the ability to * change the number of buckets and utilize a random distribution once we do. */// w w w .j a v a2 s.co m private static long getBucketIdFromEppResource(Key<? extends EppResource> resourceKey) { int numBuckets = getEppResourceIndexBucketCount(); // IDs can't be 0, so add 1 to the hash. return Hashing.consistentHash(resourceKey.getName().hashCode(), numBuckets) + 1; }
From source file:io.atomix.primitive.partition.Murmur3Partitioner.java
@Override public PartitionId partition(String key, List<PartitionId> partitions) { int hash = Math.abs(Hashing.murmur3_32().hashUnencodedChars(key).asInt()); return partitions.get(Hashing.consistentHash(hash, partitions.size())); }
From source file:info.yangguo.dragon.storage.mysql.algorithm.SingleKeyModuloTableShardingAlgorithm.java
@Override public String doEqualSharding(final Collection<String> availableTargetNames, final ShardingValue<String> shardingValue) { int index = Hashing.consistentHash(Hashing.murmur3_32().hashBytes(shardingValue.getValue().getBytes()), tbSum);// w w w . j a v a2 s . c om for (String each : availableTargetNames) { if (each.endsWith(index + "")) { return each; } } throw new UnsupportedOperationException(); }
From source file:info.yangguo.dragon.storage.mysql.algorithm.SingleKeyModuloDatabaseShardingAlgorithm.java
@Override public String doEqualSharding(final Collection<String> availableTargetNames, final ShardingValue<String> shardingValue) { int index = Hashing.consistentHash(Hashing.murmur3_32().hashBytes(shardingValue.getValue().getBytes()), dbSum);// w w w . j av a 2 s .c o m for (String each : availableTargetNames) { if (each.endsWith(index + "")) { return each; } } throw new UnsupportedOperationException(); }
From source file:com.necla.simba.util.ConsistentHash.java
public int hash(String token, int size) { return Hashing.consistentHash(Hashing.murmur3_128(SEED).hashString(token), size); }
From source file:com.necla.simba.util.ConsistentHash.java
public int hash(int token, int size) { return Hashing.consistentHash(Hashing.murmur3_128(SEED).hashInt(token), size); }
From source file:com.necla.simba.util.ConsistentHash.java
public String getNode(String id) { int bucket = Hashing.consistentHash(Hashing.murmur3_128(SEED).hashString(id), servers.size()); return servers.get(bucket); }
From source file:io.pravega.common.hash.HashHelper.java
public int hashToBucket(String str, int numBuckets) { return Hashing.consistentHash(hash.hashUnencodedChars(str), numBuckets); }
From source file:org.apache.nifi.controller.queue.clustered.partition.CorrelationAttributePartitioner.java
@Override public QueuePartition getPartition(final FlowFileRecord flowFile, final QueuePartition[] partitions, final QueuePartition localPartition) { final int hash = hash(flowFile); // The consistentHash method appears to always return a bucket of '1' if there are 2 possible buckets, // so in this case we will just use modulo division to avoid this. I suspect this is a bug with the Guava // implementation, but it's not clear at this point. final int index; if (partitions.length < 3) { index = hash % partitions.length; } else {/*from ww w. j av a 2s . co m*/ index = Hashing.consistentHash(hash, partitions.length); } return partitions[index]; }