本文整理汇总了Java中com.google.common.hash.HashFunction类的典型用法代码示例。如果您正苦于以下问题:Java HashFunction类的具体用法?Java HashFunction怎么用?Java HashFunction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HashFunction类属于com.google.common.hash包,在下文中一共展示了HashFunction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isFullDuplicate
import com.google.common.hash.HashFunction; //导入依赖的package包/类
public boolean isFullDuplicate(IHttpRequestResponse messageInfo) {
PrintWriter stdout = new PrintWriter(callbacks.getStdout(), true);
IResponseInfo respInfo = helpers.analyzeResponse(messageInfo.getResponse());
if (dubBloomFilter == null) return false;
HashFunction m_hash = Hashing.murmur3_32();
if (helpers.bytesToString(messageInfo.getResponse()).length() > respInfo.getBodyOffset()) {
String body = helpers.bytesToString(messageInfo.getResponse()).substring(respInfo.getBodyOffset());
/* full-dub detection */
String dedupHashValue = m_hash.hashBytes(helpers.stringToBytes(body)).toString();
if (dubBloomFilter.mightContain(dedupHashValue)) {
return true;
}
dubBloomFilter.put(dedupHashValue);
}
return false;
}
示例2: getJarFilePrefix
import com.google.common.hash.HashFunction; //导入依赖的package包/类
/**
* Files exported from jars are exported into a certain folder so that we can rebuild them
* when the related jar file changes.
*/
@NonNull
private static String getJarFilePrefix(@NonNull File inputFile) {
// get the filename
String name = inputFile.getName();
// remove the extension
int pos = name.lastIndexOf('.');
if (pos != -1) {
name = name.substring(0, pos);
}
// add a hash of the original file path.
String input = inputFile.getAbsolutePath();
HashFunction hashFunction = Hashing.sha1();
HashCode hashCode = hashFunction.hashString(input, Charsets.UTF_16LE);
return name + "-" + hashCode.toString();
}
示例3: fingerprintMac
import com.google.common.hash.HashFunction; //导入依赖的package包/类
/**
* Build a stringified MAC address using the ClusterMetadata hash for uniqueness.
* Form of MAC is "02:eb" followed by four bytes of clusterMetadata hash.
*/
static String fingerprintMac(ClusterMetadata cm) {
if (cm == null) {
return DEFAULT_MAC;
}
HashFunction hf = Hashing.murmur3_32();
HashCode hc = hf.newHasher().putObject(cm, ClusterMetadata.HASH_FUNNEL).hash();
int unqf = hc.asInt();
StringBuilder sb = new StringBuilder();
sb.append("02:eb");
for (int i = 0; i < 4; i++) {
byte b = (byte) (unqf >> i * 8);
sb.append(String.format(":%02X", b));
}
return sb.toString();
}
示例4: configureHash
import com.google.common.hash.HashFunction; //导入依赖的package包/类
private static HashFunction configureHash(Algorithm alg, long seedNSalt, long addlSipSeed) {
switch (alg) {
case xxHash64:
return new xxHashFunction(seedNSalt);
case Murmur3_128:
return Hashing.murmur3_128((int) seedNSalt);
case Murmur3_32:
return Hashing.murmur3_32((int) seedNSalt);
case sha256:
return Hashing.sha1();
case sipHash24:
return Hashing.sipHash24(seedNSalt, addlSipSeed);
default:
throw new IllegalArgumentException("Invalid Enum Hashing Algorithm???");
}
}
示例5: getDirectoryNameForJar
import com.google.common.hash.HashFunction; //导入依赖的package包/类
/**
* Chooses a directory name, based on a JAR file name, considering exploded-aar and classes.jar.
*/
public static String getDirectoryNameForJar(File inputFile) {
// add a hash of the original file path.
HashFunction hashFunction = Hashing.sha1();
HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charsets.UTF_16LE);
String name = Files.getNameWithoutExtension(inputFile.getName());
if (name.equals("classes") && inputFile.getAbsolutePath().contains("exploded-aar")) {
// This naming scheme is coming from DependencyManager#computeArtifactPath.
File versionDir = inputFile.getParentFile().getParentFile();
File artifactDir = versionDir.getParentFile();
File groupDir = artifactDir.getParentFile();
name = Joiner.on('-').join(
groupDir.getName(), artifactDir.getName(), versionDir.getName());
}
name = name + "_" + hashCode.toString();
return name;
}
示例6: getJackFileName
import com.google.common.hash.HashFunction; //导入依赖的package包/类
/**
* Returns a unique File for the converted library, even if there are 2 libraries with the same
* file names (but different paths)
*
* @param outFolder the output folder.
* @param inputFile the library
*/
@NonNull
public static File getJackFileName(@NonNull File outFolder, @NonNull File inputFile) {
// get the filename
String name = inputFile.getName();
// remove the extension
int pos = name.lastIndexOf('.');
if (pos != -1) {
name = name.substring(0, pos);
}
// add a hash of the original file path.
String input = inputFile.getAbsolutePath();
HashFunction hashFunction = Hashing.sha1();
HashCode hashCode = hashFunction.hashString(input, Charsets.UTF_16LE);
return new File(outFolder, name + "-" + hashCode.toString() + SdkConstants.DOT_JAR);
}
示例7: HashElement_GivenTestString_ReturnHash
import com.google.common.hash.HashFunction; //导入依赖的package包/类
@Test
public void HashElement_GivenTestString_ReturnHash() throws Exception {
// Arrange
byte[] elementToHash = "test".getBytes();
HashFunction hashFunction = mock(HashFunction.class, RETURNS_DEEP_STUBS);
when(hashFunction.hashBytes(eq(elementToHash))).thenReturn(HashCode.fromLong(10L));
Guava64BitHasher hasher = new Guava64BitHasher(hashFunction);
// Act
Hash64Bits hash = hasher.hash(elementToHash);
// Assert
Hash64Bits expectedHash = new Hash64Bits(10L);
assertEquals(expectedHash, hash);
}
示例8: Hash_GivenTestString_ReturnHash
import com.google.common.hash.HashFunction; //导入依赖的package包/类
@Test
public void Hash_GivenTestString_ReturnHash() throws Exception {
// Arrange
byte[] elementToHash = "test".getBytes();
HashFunction hashFunction = mock(HashFunction.class, RETURNS_DEEP_STUBS);
when(hashFunction.hashBytes(eq(elementToHash))).thenReturn(HashCode.fromInt(10));
Guava32BitHasher hasher = new Guava32BitHasher(hashFunction);
// Act
Hash32Bits hash = hasher.hash(elementToHash);
// Assert
Hash32Bits expectedHash = new Hash32Bits(10);
assertEquals(expectedHash, hash);
}
示例9: getHash
import com.google.common.hash.HashFunction; //导入依赖的package包/类
static double getHash(String newString){
double hashofString = 0;
try{
// The murmur hash is an effective way to uniformly hash different string
HashFunction hf = Hashing.murmur3_128();
HashCode hc = hf.newHasher().putString(newString).hash();
byte[] byteArray = hc.asBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
hashofString = buffer.getShort();
}
catch(Exception e){
e.printStackTrace();
}
return Math.abs(hashofString)%.99;
}
示例10: getKeyHash
import com.google.common.hash.HashFunction; //导入依赖的package包/类
static double getKeyHash(String newString){
double hashofString = 0;
try{
// The murmur hash is an effective way to uniformly hash different string
HashFunction hf = Hashing.murmur3_128();
HashCode hc = hf.newHasher().putString(newString).hash();
byte[] byteArray = hc.asBytes();
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
hashofString = buffer.getShort();
}
catch(Exception e){
e.printStackTrace();
}
return Math.abs(hashofString)%.99;
}
示例11: HashEngine
import com.google.common.hash.HashFunction; //导入依赖的package包/类
/**
* Construct a new hash engine, using the default configuration values
* present in the the hrfs site configuration file.
*/
public HashEngine(HashFunction hfn)
{
this.conf = new HrfsConfiguration();
this.hashfn = hfn;
this.nworkers = conf.getInt(HrfsKeys.HRFS_HENGINE_WORKERS, 5);
this.biqueue = new ConcurrentLinkedQueue<Block>();
this.boqueue = new ConcurrentLinkedQueue<DataBlock>();
this.ahcnt = new AtomicLong(0);
/* Build up our worker pool using the configuration tunable. */
this.executor = new ThreadPoolExecutor(nworkers, nworkers,
1000L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
示例12: calculatePartition
import com.google.common.hash.HashFunction; //导入依赖的package包/类
/**
* Distribute triggers on nodes using a consistent hashing strategy.
* This strategy allows to scale and minimize changes and re-distribution when cluster changes.
*
* @param entries a list of entries to distribute
* @param buckets a table of nodes
* @return a map of entries distributed across nodes
*/
public Map<PartitionEntry, Integer> calculatePartition(List<PartitionEntry> entries,
Map<Integer, Integer> buckets) {
if (entries == null) {
throw new IllegalArgumentException("entries must be not null");
}
if (isEmpty(buckets)) {
throw new IllegalArgumentException("entries must be not null");
}
HashFunction md5 = Hashing.md5();
int numBuckets = buckets.size();
Map<PartitionEntry, Integer> newPartition = new HashMap<>();
for (PartitionEntry entry : entries) {
newPartition.put(entry, buckets.get(Hashing.consistentHash(md5.hashInt(entry.hashCode()), numBuckets)));
}
return newPartition;
}
示例13: getHasher
import com.google.common.hash.HashFunction; //导入依赖的package包/类
public static HashFunction getHasher(HashType hashType) {
switch(hashType) {
case MURMUR3_128:
return Hashing.murmur3_128();
case MURMUR3_32:
return Hashing.murmur3_32();
case SIPHASH24:
return Hashing.sipHash24();
case MD5:
return Hashing.md5();
case SHA1:
return Hashing.sha1();
case SHA256:
return Hashing.sha256();
case SHA512:
return Hashing.sha512();
case ADLER32:
return Hashing.adler32();
case CRC32:
return Hashing.crc32();
case CRC32C:
return Hashing.crc32c();
default:
throw new IllegalArgumentException(Utils.format("Unsupported Hashing Algorithm: {}", hashType.name()));
}
}
示例14: generateHash
import com.google.common.hash.HashFunction; //导入依赖的package包/类
private String generateHash(
Record record,
HashType hashType,
Collection<String> fieldsToHash,
boolean includeRecordHeader,
boolean useSeparator
) throws StageException {
try {
HashFunction hasher = HashingUtil.getHasher(hashType.getHashType());
HashingUtil.RecordFunnel recordFunnel = HashingUtil.getRecordFunnel(
fieldsToHash,
includeRecordHeader,
useSeparator
);
return hasher.hashObject(record, recordFunnel).toString();
} catch (IllegalArgumentException e) {
throw new OnRecordErrorException(Errors.HASH_00, hashType.getDigest(), e.toString(), e);
}
}
示例15: hashForRecordsWithFieldsAndHeaderAttr
import com.google.common.hash.HashFunction; //导入依赖的package包/类
private String hashForRecordsWithFieldsAndHeaderAttr(
Record record,
Collection<String> fieldsToHash,
HashType hashType,
boolean includeRecordHeaderForHashing,
boolean useSeparator
) {
HashFunction hasher = HashingUtil.getHasher(hashType.getHashType());
Set<String> validFieldsToHash = new HashSet<>();
for (String fieldPath : fieldsToHash) {
Field field = record.get(fieldPath);
Field.Type type = field.getType();
if (!(FieldHasherProcessor.UNSUPPORTED_FIELD_TYPES.contains(type) || field.getValue() == null)) {
validFieldsToHash.add(fieldPath);
}
}
HashingUtil.RecordFunnel recordFunnel =
HashingUtil.getRecordFunnel(
validFieldsToHash,
includeRecordHeaderForHashing,
useSeparator
);
return hasher.hashObject(record, recordFunnel).toString();
}