Search in sources :

Example 56 with HashCode

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;
}
Also used : ZookeeperRegistryCenter(com.paascloud.core.registry.zookeeper.ZookeeperRegistryCenter) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) CoordinatorRegistryCenter(com.paascloud.core.registry.base.CoordinatorRegistryCenter)

Example 57 with HashCode

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();
}
Also used : HashCode(com.google.common.hash.HashCode) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer)

Example 58 with HashCode

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;
}
Also used : HashCode(com.google.common.hash.HashCode)

Example 59 with HashCode

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);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) FileInputStream(java.io.FileInputStream) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) ZipInputStream(java.util.zip.ZipInputStream) HashCode(com.google.common.hash.HashCode) Collection(java.util.Collection) File(java.io.File)

Example 60 with HashCode

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;
}
Also used : HashCode(com.google.common.hash.HashCode) ArrayList(java.util.ArrayList)

Aggregations

HashCode (com.google.common.hash.HashCode)115 Path (java.nio.file.Path)41 Test (org.junit.Test)39 BuildTarget (com.facebook.buck.model.BuildTarget)19 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)13 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 PathSourcePath (com.facebook.buck.rules.PathSourcePath)11 FileHashCache (com.facebook.buck.util.cache.FileHashCache)11 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)10 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)10 Hasher (com.google.common.hash.Hasher)10 Map (java.util.Map)8 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)7 SourcePath (com.facebook.buck.rules.SourcePath)7 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)5