Example usage for com.google.common.hash Hasher putUnencodedChars

List of usage examples for com.google.common.hash Hasher putUnencodedChars

Introduction

In this page you can find the example usage for com.google.common.hash Hasher putUnencodedChars.

Prototype

@Override
Hasher putUnencodedChars(CharSequence charSequence);

Source Link

Document

Equivalent to processing each char value in the CharSequence , in order.

Usage

From source file:com.facebook.buck.util.hashing.StringHashing.java

/**
 * Encodes the length of the string in UTF-16 code units, then the UTF-16 code units of the
 * string./*from ww w.  ja  v a2 s . c om*/
 *
 * <p>Useful to ensure hash codes are different when multiple strings are hashed in order ("foo"
 * then "bar" should hash differently from "foobar").
 */
public static void hashStringAndLength(Hasher hasher, String string) {
    // We used to hash the UTF-8 bytes of the string, but it takes
    // a lot of unnecessary CPU and memory to do so.
    hasher.putInt(string.length());
    hasher.putUnencodedChars(string);
}

From source file:org.gradle.api.internal.file.FileTreeElementHasher.java

public static final int calculateHashForFilePaths(Collection<FileTreeElement> allFileTreeElements) {
    SortedSet<FileTreeElement> sortedFileTreeElement = asSortedSet(allFileTreeElements);

    Hasher hasher = Hashing.adler32().newHasher();
    for (FileTreeElement fileTreeElement : sortedFileTreeElement) {
        for (String pathPart : fileTreeElement.getRelativePath().getSegments()) {
            hasher.putUnencodedChars(pathPart);
            hasher.putByte(HASH_PATH_SEPARATOR);
        }//  w w w .j  av  a 2 s  . c om
        hasher.putByte(HASH_RECORD_SEPARATOR);
    }
    return hasher.hash().asInt();
}

From source file:com.google.devtools.build.lib.rules.config.ConfigFeatureFlagConfiguration.java

/** Converts the given flag values into a string hash for use as an output directory fragment. */
private static String hashFlags(SortedMap<Label, String> flagValues) {
    // This hash function is relatively fast and stable between JVM invocations.
    Hasher hasher = Hashing.murmur3_128().newHasher();

    for (Map.Entry<Label, String> flag : flagValues.entrySet()) {
        hasher.putUnencodedChars(flag.getKey().toString());
        hasher.putUnencodedChars(flag.getValue());
    }//from   w  w w . ja v  a2  s  . c o  m
    return hasher.hash().toString();
}

From source file:nextflow.util.CacheHelper.java

/**
 * Hashes the file by using the metadata information: full path string, size and last update timestamp
 *
 * @param hasher The current {@code Hasher} object
 * @param file file The {@code Path} object to hash
 * @return The updated {@code Hasher} object
 *//*from   w  ww. j a va2  s . co  m*/
static private Hasher hashFileMetadata(Hasher hasher, Path file) {

    hasher = hasher.putUnencodedChars(file.toAbsolutePath().normalize().toString());

    try {
        BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
        hasher = hasher.putLong(attrs.size());
        if (attrs.lastAccessTime() != null) {
            hasher = hasher.putLong(attrs.lastModifiedTime().toMillis());
        }
        return hasher;
    } catch (IOException e) {
        log.debug("Unable to hash file: {} -- Cause: {}", file, e.toString());
        return hasher;
    }
}

From source file:edu.umd.marbl.mhap.sketch.HashUtils.java

public static long[] computeHashes(String item, int numWords, int seed) {
    long[] hashes = new long[numWords];

    for (int word = 0; word < numWords; word += 2) {
        HashFunction hashFunc = Hashing.murmur3_128(seed + word);
        Hasher hasher = hashFunc.newHasher();
        hasher.putUnencodedChars(item);

        // get the two longs out
        HashCode hc = hasher.hash();/*from   www  . j ava  2  s.  co  m*/
        ByteBuffer bb = ByteBuffer.wrap(hc.asBytes());
        hashes[word] = bb.getLong(0);
        if (word + 1 < numWords)
            hashes[word + 1] = bb.getLong(8);
    }

    return hashes;
}

From source file:com.facebook.buck.android.DexProducedFromJavaLibrary.java

@VisibleForTesting
static Sha1HashCode computeAbiKey(ImmutableSortedMap<String, HashCode> classNames) {
    Hasher hasher = Hashing.sha1().newHasher();
    for (Map.Entry<String, HashCode> entry : classNames.entrySet()) {
        hasher.putUnencodedChars(entry.getKey());
        hasher.putByte((byte) 0);
        hasher.putUnencodedChars(entry.getValue().toString());
        hasher.putByte((byte) 0);
    }//from   w  w w .j a v a 2 s . c o m
    return new Sha1HashCode(hasher.hash().toString());
}

From source file:com.facebook.buck.java.AccumulateClassNames.java

@VisibleForTesting
static Sha1HashCode computeAbiKey(Supplier<ImmutableSortedMap<String, HashCode>> classNames) {
    Hasher hasher = Hashing.sha1().newHasher();
    for (Map.Entry<String, HashCode> entry : classNames.get().entrySet()) {
        hasher.putUnencodedChars(entry.getKey());
        hasher.putByte((byte) 0);
        hasher.putUnencodedChars(entry.getValue().toString());
        hasher.putByte((byte) 0);
    }//from  w w  w  .j a va2  s.  co m
    return new Sha1HashCode(hasher.hash().toString());
}

From source file:com.googlesource.gerrit.plugins.xdocs.XDocServlet.java

private static String computeETag(Project.NameKey project, ObjectId revId, String file, ObjectId revIdB,
        DiffMode diffMode) {/* www .  j  av a 2s.  co m*/
    Hasher hasher = Hashing.md5().newHasher();
    hasher.putUnencodedChars(project.get());
    if (revId != null) {
        hasher.putUnencodedChars(revId.getName());
    }
    hasher.putUnencodedChars(file);
    if (diffMode != DiffMode.NO_DIFF) {
        hasher.putUnencodedChars(revIdB.getName()).putUnencodedChars(diffMode.name());
    }
    return hasher.hash().toString();
}

From source file:io.druid.query.aggregation.cardinality.CardinalityAggregator.java

protected static void hashRow(List<DimensionSelector> selectorList, HyperLogLogCollector collector) {
    final Hasher hasher = hashFn.newHasher();
    for (int k = 0; k < selectorList.size(); ++k) {
        if (k != 0) {
            hasher.putByte((byte) 0);
        }/*from w w w.j  a va2s  . c om*/
        final DimensionSelector selector = selectorList.get(k);
        final IndexedInts row = selector.getRow();
        final int size = row.size();
        // nothing to add to hasher if size == 0, only handle size == 1 and size != 0 cases.
        if (size == 1) {
            final String value = selector.lookupName(row.get(0));
            hasher.putUnencodedChars(value != null ? value : NULL_STRING);
        } else if (size != 0) {
            final String[] values = new String[size];
            for (int i = 0; i < size; ++i) {
                final String value = selector.lookupName(row.get(i));
                values[i] = value != null ? value : NULL_STRING;
            }
            // Values need to be sorted to ensure consistent multi-value ordering across different segments
            Arrays.sort(values);
            for (int i = 0; i < size; ++i) {
                if (i != 0) {
                    hasher.putChar(SEPARATOR);
                }
                hasher.putUnencodedChars(values[i]);
            }
        }
    }
    collector.add(hasher.hash().asBytes());
}

From source file:com.yahoo.yqlplus.api.types.YQLNamedValueType.java

public void hashTo(Hasher digest) {
    super.hashTo(digest);
    digest.putUnencodedChars(getName());
}