use of com.google.common.hash.HashCode in project paascloud-master by paascloud.
the class RegistryCenterFactory method createCoordinatorRegistryCenter.
/**
* 创建注册中心.
*
* @param zookeeperProperties the zookeeper properties
*
* @return 注册中心对象 coordinator registry center
*/
public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(ZookeeperProperties zookeeperProperties) {
Hasher hasher = Hashing.md5().newHasher().putString(zookeeperProperties.getZkAddressList(), Charsets.UTF_8);
HashCode hashCode = hasher.hash();
CoordinatorRegistryCenter result = REG_CENTER_REGISTRY.get(hashCode);
if (null != result) {
return result;
}
result = new ZookeeperRegistryCenter(zookeeperProperties);
result.init();
REG_CENTER_REGISTRY.put(hashCode, result);
return result;
}
use of com.google.common.hash.HashCode in project janusgraph by JanusGraph.
the class HashingUtil method hashPrefixKey.
public static StaticBuffer hashPrefixKey(final HashLength hashPrefixLen, final StaticBuffer key) {
final int prefixLen = hashPrefixLen.length();
final StaticBuffer.Factory<HashCode> hashFactory;
switch(hashPrefixLen) {
case SHORT:
hashFactory = SHORT_HASH_FACTORY;
break;
case LONG:
hashFactory = LONG_HASH_FACTORY;
break;
default:
throw new IllegalArgumentException("Unknown hash prefix: " + hashPrefixLen);
}
HashCode hashcode = key.as(hashFactory);
WriteByteBuffer newKey = new WriteByteBuffer(prefixLen + key.length());
assert prefixLen == 4 || prefixLen == 8;
if (prefixLen == 4)
newKey.putInt(hashcode.asInt());
else
newKey.putLong(hashcode.asLong());
newKey.putBytes(key);
return newKey.getStaticBuffer();
}
use of com.google.common.hash.HashCode in project gradle by gradle.
the class CachingClasspathEntryHasher method hash.
@Override
public HashCode hash(FileDetails fileDetails) {
HashCode contentMd5 = fileDetails.getContent().getContentMd5();
HashCode signature = persistentCache.get(contentMd5);
if (signature != null) {
if (signature.equals(NO_SIGNATURE)) {
return null;
}
return signature;
}
signature = delegate.hash(fileDetails);
if (signature != null) {
persistentCache.put(contentMd5, signature);
} else {
persistentCache.put(contentMd5, NO_SIGNATURE);
}
return signature;
}
use of com.google.common.hash.HashCode in project gradle by gradle.
the class DefaultClasspathEntryHasher method hashJar.
private HashCode hashJar(FileDetails fileDetails, Hasher hasher, ClasspathContentHasher classpathContentHasher) {
File jarFilePath = new File(fileDetails.getPath());
ZipInputStream zipInput = null;
try {
zipInput = new ZipInputStream(new FileInputStream(jarFilePath));
ZipEntry zipEntry;
Multimap<String, HashCode> entriesByName = MultimapBuilder.hashKeys().arrayListValues().build();
while ((zipEntry = zipInput.getNextEntry()) != null) {
if (zipEntry.isDirectory()) {
continue;
}
HashCode hash = hashZipEntry(zipInput, zipEntry, classpathContentHasher);
if (hash != null) {
entriesByName.put(zipEntry.getName(), hash);
}
}
Map<String, Collection<HashCode>> map = entriesByName.asMap();
// Ensure we hash the zip entries in a deterministic order
String[] keys = map.keySet().toArray(new String[0]);
Arrays.sort(keys);
for (String key : keys) {
for (HashCode hash : map.get(key)) {
hasher.putBytes(hash.asBytes());
}
}
return hasher.hash();
} catch (ZipException e) {
// ZipExceptions point to a problem with the Zip, we try to be lenient for now.
return hashMalformedZip(fileDetails, hasher, classpathContentHasher);
} catch (IOException e) {
// IOExceptions other than ZipException are failures.
throw new UncheckedIOException("Error snapshotting jar [" + fileDetails.getName() + "]", e);
} catch (Exception e) {
// Other Exceptions can be thrown by invalid zips, too. See https://github.com/gradle/gradle/issues/1581.
return hashMalformedZip(fileDetails, hasher, classpathContentHasher);
} finally {
IOUtils.closeQuietly(zipInput);
}
}
use of com.google.common.hash.HashCode in project gradle by gradle.
the class DefaultClasspathSnapshotter method normaliseTreeElements.
@Override
protected List<FileDetails> normaliseTreeElements(List<FileDetails> fileDetails) {
// TODO: We could rework this to produce a FileDetails for the directory that
// has a hash for the contents of this directory vs returning a list of the contents
// of the directory with their hashes
// Collect the signatures of each class file
List<FileDetails> sorted = new ArrayList<FileDetails>(fileDetails.size());
for (FileDetails details : fileDetails) {
if (details.getType() == FileType.RegularFile) {
HashCode signatureForClass = classpathEntryHasher.hash(details);
if (signatureForClass == null) {
// Should be excluded
continue;
}
sorted.add(details.withContentHash(signatureForClass));
}
}
// Sort as their order is not important
Collections.sort(sorted, FILE_DETAILS_COMPARATOR);
return sorted;
}
Aggregations