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

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

Introduction

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

Prototype

@Override
    Hasher putChar(char c);

Source Link

Usage

From source file:com.google.api.control.aggregator.Signing.java

/**
 * Updates {@code h} with the contents of {@code labels}.
 *
 * {@code labels} can be any Map<String, String>, but intended to be used for the labels of
 * one of the model protobufs.//from   ww  w . j a  v a 2  s.  com
 *
 * @param h a {@link Hasher}
 * @param labels some labels
 * @return the {@code Hasher}, to allow fluent-style usage
 */
public static Hasher putLabels(Hasher h, Map<String, String> labels) {
    for (Map.Entry<String, String> labelsEntry : labels.entrySet()) {
        h.putChar('\0');
        h.putString(labelsEntry.getKey(), StandardCharsets.UTF_8);
        h.putChar('\0');
        h.putString(labelsEntry.getValue(), StandardCharsets.UTF_8);
    }
    return h;
}

From source file:com.google.api.control.aggregator.ReportRequestAggregator.java

/**
 * Obtains the {@hashCode} for the contents of {@code value}.
 *
 * @param value a {@code Operation} to be signed
 * @return the {@code HashCode} corresponding to {@code value}
 *//*from   w  ww.j  a va  2  s .  co m*/
private static HashCode sign(Operation value) {
    Hasher h = Hashing.md5().newHasher();
    h.putString(value.getConsumerId(), StandardCharsets.UTF_8);
    h.putChar('\0');
    h.putString(value.getOperationName(), StandardCharsets.UTF_8);
    h.putChar('\0');
    return Signing.putLabels(h, value.getLabels()).hash();
}

From source file:org.ambraproject.wombat.util.CacheKey.java

/**
 * Create a hash from the sequence of identifiers and return it, with a prefix appended, for use as a cache key.
 * <p>/* w ww  .  j  a  v  a 2 s .  c om*/
 * This is useful because the strings may contain characters that are invalid for a cache key. Also, when they are
 * concatenated, they may be too long (by environmental default, >250 chars) to be used as a key.
 *
 * @param identifiers a sequence of identifiers that uniquely identify a cache value
 * @return a digest of bounded length
 */
public static String createKeyHash(List<String> identifiers) {
    Hasher hasher = HASH_ALGORITHM.newHasher();
    for (Iterator<String> iterator = identifiers.iterator(); iterator.hasNext();) {
        hasher.putString(iterator.next(), HASH_CHARSET);
        if (iterator.hasNext()) {
            hasher.putChar(HASH_SEPARATOR);
        }
    }
    return HASH_BASE.encode(hasher.hash().asBytes());
}

From source file:com.google.api.control.aggregator.CheckRequestAggregator.java

/**
 * Obtains the {@code HashCode} for the contents of {@code value}.
 *
 * @param value a {@code CheckRequest} to be signed
 * @return the {@code HashCode} corresponding to {@code value}
 *///from  ww  w .  ja  v  a2 s . c o  m
public static HashCode sign(CheckRequest value) {
    Hasher h = Hashing.md5().newHasher();
    Operation o = value.getOperation();
    if (o == null || Strings.isNullOrEmpty(o.getConsumerId()) || Strings.isNullOrEmpty(o.getOperationName())) {
        throw new IllegalArgumentException("CheckRequest should have a valid operation");
    }
    h.putString(o.getConsumerId(), StandardCharsets.UTF_8);
    h.putChar('\0');
    h.putString(o.getOperationName(), StandardCharsets.UTF_8);
    h.putChar('\0');
    Signing.putLabels(h, o.getLabels());
    for (MetricValueSet mvSet : o.getMetricValueSetsList()) {
        h.putString(mvSet.getMetricName(), StandardCharsets.UTF_8);
        h.putChar('\0');
        for (MetricValue metricValue : mvSet.getMetricValuesList()) {
            MetricValues.putMetricValue(h, metricValue);
        }
    }
    return h.hash();
}

From source file:com.facebook.buck.util.hash.AppendingHasher.java

@Override
public Hasher putChar(char c) {
    for (Hasher hasher : hashers) {
        hasher.putChar(c);
    }//from   w ww  . j a v  a  2s . c  om
    return this;
}

From source file:nextflow.util.CacheHelper.java

public static Hasher hasher(Hasher hasher, Object value, HashMode mode) {

    if (value == null)
        return hasher;

    if (value instanceof Boolean)
        return hasher.putBoolean((Boolean) value);

    if (value instanceof Short)
        return hasher.putShort((Short) value);

    if (value instanceof Integer)
        return hasher.putInt((Integer) value);

    if (value instanceof Long)
        return hasher.putLong((Long) value);

    if (value instanceof Float)
        return hasher.putFloat((Float) value);

    if (value instanceof Double)
        return hasher.putDouble((Double) value);

    if (value instanceof Number)
        // reduce all other number types (BigInteger, BigDecimal, AtomicXxx, etc) to string equivalent
        return hasher.putUnencodedChars(value.toString());

    if (value instanceof Character)
        return hasher.putChar((Character) value);

    if (value instanceof CharSequence)
        return hasher.putUnencodedChars((CharSequence) value);

    if (value instanceof Byte)
        return hasher.putByte((Byte) value);

    if (value instanceof byte[])
        return hasher.putBytes((byte[]) value);

    if (value instanceof Object[]) {
        for (Object item : ((Object[]) value))
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }//www .j  a va2s.c  o m

    if (value instanceof Map) {
        // note: should map be order invariant as Set ?
        for (Object item : ((Map) value).values())
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }

    if (value instanceof Bag || value instanceof Set)
        return hashUnorderedCollection(hasher, (Collection) value, mode);

    if (value instanceof Collection) {
        for (Object item : ((Collection) value))
            hasher = CacheHelper.hasher(hasher, item, mode);
        return hasher;
    }

    if (value instanceof FileHolder)
        return CacheHelper.hasher(hasher, ((FileHolder) value).getSourceObj(), mode);

    if (value instanceof Path)
        return hashFile(hasher, (Path) value, mode);

    if (value instanceof java.io.File)
        return hashFile(hasher, (java.io.File) value, mode);

    if (value instanceof UUID) {
        UUID uuid = (UUID) value;
        return hasher.putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits());
    }

    log.debug("[WARN] Unknown hashing type: {} -- {}", value.getClass(), value);
    return hasher.putInt(value.hashCode());
}

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  ww  w.  j  a  v  a2s.c o m*/
        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:org.fim.model.FileState.java

@Override
public void hashObject(Hasher hasher) {
    hasher.putString("FileState", Charsets.UTF_8).putChar(HASH_FIELD_SEPARATOR)
            .putString(fileName, Charsets.UTF_8).putChar(HASH_FIELD_SEPARATOR).putLong(fileLength);

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    fileTime.hashObject(hasher);//from w  w  w  . ja  v  a2s  .com

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    fileHash.hashObject(hasher);

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    if (fileAttributes != null) {
        for (String key : fileAttributes.keySet()) {
            hasher.putString(key, Charsets.UTF_8).putChar(':').putChar(':').putString(fileAttributes.get(key),
                    Charsets.UTF_8);
            hasher.putChar(HASH_OBJECT_SEPARATOR);
        }
    }
}

From source file:org.fim.model.State.java

@Override
public void hashObject(Hasher hasher) {
    hasher.putString("State", Charsets.UTF_8).putChar(HASH_FIELD_SEPARATOR)
            .putString(modelVersion, Charsets.UTF_8).putChar(HASH_FIELD_SEPARATOR).putLong(timestamp)
            .putChar(HASH_FIELD_SEPARATOR).putString(comment, Charsets.UTF_8).putChar(HASH_FIELD_SEPARATOR)
            .putInt(fileCount).putChar(HASH_FIELD_SEPARATOR).putLong(filesContentLength)
            .putChar(HASH_FIELD_SEPARATOR).putString(hashMode.name(), Charsets.UTF_8);

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    for (String ignoredFile : ignoredFiles) {
        hasher.putString(ignoredFile, Charsets.UTF_8).putChar(HASH_OBJECT_SEPARATOR);
    }/*w  ww .  ja v  a 2 s. c o  m*/

    hasher.putChar(HASH_OBJECT_SEPARATOR);
    for (FileState fileState : fileStates) {
        fileState.hashObject(hasher);
        hasher.putChar(HASH_OBJECT_SEPARATOR);
    }
}

From source file:io.druid.query.aggregation.cardinality.types.StringCardinalityAggregatorColumnSelectorStrategy.java

@Override
public void hashRow(DimensionSelector dimSelector, Hasher hasher) {
    final IndexedInts row = dimSelector.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 = dimSelector.lookupName(row.get(0));
        hasher.putUnencodedChars(nullToSpecial(value));
    } else if (size != 0) {
        final String[] values = new String[size];
        for (int i = 0; i < size; ++i) {
            final String value = dimSelector.lookupName(row.get(i));
            values[i] = nullToSpecial(value);
        }//  www  .  j  ava2s  .c  o  m
        // 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(CARDINALITY_AGG_SEPARATOR);
            }
            hasher.putUnencodedChars(values[i]);
        }
    }
}