Java Code Examples for com.google.common.hash.Hashing#goodFastHash()

The following examples show how to use com.google.common.hash.Hashing#goodFastHash() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Hasher.java    From datafu with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the HashFunction named by algorithm
 *
 * See the Hasher class docs for a list of algorithms and guidance on selection.
 *
 * @param algorithm the hash algorithm to use
 * @throws IllegalArgumentException for an invalid seed given the algorithm
 * @throws RuntimeException when the seed cannot be parsed
 */
private void makeHashFunc(String algorithm) throws IllegalArgumentException, RuntimeException
{
  if (hash_func != null) { throw new RuntimeException("The hash function should only be set once per instance"); }

  if (algorithm.startsWith("good-")) {
    int bits = Integer.parseInt(algorithm.substring(5));
    hash_func = Hashing.goodFastHash(bits);
  }
  else if (algorithm.equals("murmur3-32")) { hash_func = Hashing.murmur3_32();  }
  else if (algorithm.equals("murmur3-128")){ hash_func = Hashing.murmur3_128(); }
  else if (algorithm.equals("sip24"))      { hash_func = Hashing.sipHash24();   }
  else if (algorithm.equals("sha1"))       { hash_func = Hashing.sha1();        }
  else if (algorithm.equals("sha256"))     { hash_func = Hashing.sha256();      }
  else if (algorithm.equals("sha512"))     { hash_func = Hashing.sha512();      }
  else if (algorithm.equals("md5"))        { hash_func = Hashing.md5();         }
  else if (algorithm.equals("adler32"))    { hash_func = Hashing.adler32();     }
  else if (algorithm.equals("crc32"))      { hash_func = Hashing.crc32();       }
  else { throw new IllegalArgumentException("No hash function found for algorithm "+algorithm+". Allowed values include "+HASH_NAMES); }
}
 
Example 2
Source File: ResultsMerger.java    From splicer with Apache License 2.0 5 votes vote down vote up
public long signatureOf(TsdbResult result) {
	HashFunction hf = Hashing.goodFastHash(64);
	Hasher hasher = hf.newHasher();

	List<String> aggTags = result.getAggregateTags();
	if (aggTags != null) {
		List<String> sortedAggTags = Lists.newArrayList(aggTags);
		Collections.sort(sortedAggTags);
		for (String aggTag: sortedAggTags) {
			hasher.putString(aggTag, Charset.forName("ISO-8859-1"));
		}
	}

	Map<String, String> tags;
	if (result.getTags() != null && (tags = result.getTags().getTags()) != null) {
		List<String> tagTokens = Lists.newArrayList(tags.keySet());
		Collections.sort(tagTokens);
		for (String s: tagTokens) {
			hasher.putString(s, Charset.forName("ISO-8859-1"));
			hasher.putString(tags.get(s), Charset.forName("ISO-8859-1"));
		}
	}

	List<String> tsuids = result.getTsuids();
	if (tsuids != null) {
		List<String> sortedTsUIDs = Lists.newArrayList(tsuids);
		Collections.sort(sortedTsUIDs);
		for (String tsuid: sortedTsUIDs) {
			hasher.putString(tsuid, Charset.forName("ISO-8859-1"));
		}
	}

	return hasher.hash().asLong();
}
 
Example 3
Source File: SkipScanFilter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 4
Source File: SkipScanFilter.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 5
Source File: SkipScanFilter.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(32);
    Hasher h = hf.newHasher();
    h.putInt(slots.size());
    for (int i=0; i<slots.size(); i++) {
        h.putInt(slots.get(i).size());
        for (int j=0; j<slots.size(); j++) {
            h.putBytes(slots.get(i).get(j).getLowerRange());
            h.putBytes(slots.get(i).get(j).getUpperRange());
        }
    }
    return h.hash().asInt();
}
 
Example 6
Source File: RangeSplit.java    From datawave with Apache License 2.0 4 votes vote down vote up
@Override
public int hashCode() {
    HashFunction hf = Hashing.goodFastHash(64);
    return hf.newHasher().putObject(this, strategy).hash().asInt();
}
 
Example 7
Source File: HashableTest.java    From metanome-algorithms with Apache License 2.0 4 votes vote down vote up
@Test
public void test() {
	HashFunction function = Hashing.goodFastHash(10);
	assertThat(Hasher.of(function).put(new TestHashable()).hash())
		.isEqualTo(function.newHasher().putUnencodedChars(TestHashable.class.getName()).hash());
}
 
Example 8
Source File: KeyHasher.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public static String getHashedKey(String key, String hashingAlgorithm) {
    final long start = System.nanoTime();
    HashFunction hf = null; 
    switch(hashingAlgorithm.toLowerCase()) {
        case "murmur3" :
            hf = Hashing.murmur3_128();
            break;

        case "adler32" :
            hf = Hashing.adler32();
            break;

        case "crc32" :
            hf = Hashing.crc32();
            break;

        case "sha1" :
            hf = Hashing.sha1();
            break;

        case "sha256" :
            hf = Hashing.sha256();
            break;

        case "siphash24" :
            hf = Hashing.sipHash24();
            break;

        case "goodfasthash" :
            hf = Hashing.goodFastHash(128);
            break;

        case "md5" :
        default :
            hf = Hashing.md5();
            break;
    }

    final HashCode hc = hf.newHasher().putString(key, Charsets.UTF_8).hash();
    final byte[] digest = hc.asBytes();
    if(log.isDebugEnabled()) log.debug("Key : " + key +"; digest length : " + digest.length + "; byte Array contents : " + Arrays.toString(digest) );
    final String hKey = encoder.encodeToString(digest);
    if(log.isDebugEnabled()) log.debug("Key : " + key +"; Hashed & encoded key : " + hKey + "; Took " + (System.nanoTime() - start) + " nanos");
    return hKey;
}