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

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

Introduction

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

Prototype

@Override
    Hasher putByte(byte b);

Source Link

Usage

From source file:org.apache.druid.query.aggregation.cardinality.CardinalityAggregator.java

static void hashRow(ColumnSelectorPlus<CardinalityAggregatorColumnSelectorStrategy>[] selectorPluses,
        HyperLogLogCollector collector) {
    final Hasher hasher = hashFn.newHasher();
    for (int k = 0; k < selectorPluses.length; ++k) {
        if (k != 0) {
            hasher.putByte((byte) 0);
        }//from www .java  2s  .c o  m

        ColumnSelectorPlus<CardinalityAggregatorColumnSelectorStrategy> selectorPlus = selectorPluses[k];
        selectorPlus.getColumnSelectorStrategy().hashRow(selectorPlus.getSelector(), hasher);
    }
    collector.add(hasher.hash().asBytes());
}

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 va  2s. 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: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);
        }/*from   ww  w.  j a  v a2 s .co  m*/
        hasher.putByte(HASH_RECORD_SEPARATOR);
    }
    return hasher.hash().asInt();
}

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   ww w.  j a v a  2s .  com*/
    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  ww w  .  j a v a 2 s . c  om
    return new Sha1HashCode(hasher.hash().toString());
}

From source file:org.danilopianini.util.Hashes.java

/**
 * @param in/*  w w w .j  a va 2s . c  o m*/
 *            input resource
 * @param onFailure
 *            {@link IOException} handler
 * @return an {@link HashCode} representation of the file (non
 *         cryptographic)
 */
private static HashCode hashResource(final InputStreamIterator it) {
    final Hasher hasher = MURMUR128.newHasher();
    while (it.hasNext()) {
        hasher.putByte(it.next());
    }
    return hasher.hash();
}

From source file:com.torodb.mongodb.language.ObjectIdFactory.java

private static int createMachineId() {
    int machineId;
    try {/*from  www. j  a  va 2  s .  c  o m*/
        Hasher hasher = Hashing.crc32c().newHasher();
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        boolean atLeastOne = false;
        while (nics.hasMoreElements()) {
            NetworkInterface ni = nics.nextElement();
            if (ni != null) {
                byte[] macAddress = ni.getHardwareAddress();
                if (macAddress != null) {
                    for (byte b : macAddress) {
                        atLeastOne = true;
                        hasher.putByte(b);
                    }
                }
            }
        }
        if (!atLeastOne) {
            LOGGER.warn("Failed to calculate the machine id. A random number is used");
            machineId = new SecureRandom().nextInt();
        } else {
            machineId = hasher.hash().asInt();
        }
    } catch (SocketException ex) {
        LOGGER.warn("Failed to calculate the machine id. A random number is used");
        machineId = new SecureRandom().nextInt();
    }

    return machineId & 0xFFFFFF;
}

From source file:com.torodb.torod.mongodb.repl.ObjectIdFactory.java

private static int createMachineId() {
    int machineId;
    try {/*from  w w w  .  ja v  a  2  s.c o  m*/
        Hasher hasher = Hashing.crc32c().newHasher();
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        boolean atLeastOne = false;
        while (nics.hasMoreElements()) {
            NetworkInterface ni = nics.nextElement();
            if (ni != null) {
                byte[] macAddress = ni.getHardwareAddress();
                if (macAddress != null) {
                    for (byte _byte : macAddress) {
                        atLeastOne = true;
                        hasher.putByte(_byte);
                    }
                }
            }
        }
        if (!atLeastOne) {
            LOGGER.warn("Failed to calculate the machine id. A random number is used");
            machineId = new SecureRandom().nextInt();
        } else {
            machineId = hasher.hash().asInt();
        }
    } catch (SocketException ex) {
        LOGGER.warn("Failed to calculate the machine id. A random number is used");
        machineId = new SecureRandom().nextInt();
    }

    return machineId & 0xFFFFFF;
}

From source file:org.danilopianini.lang.HashUtils.java

private static void populateHasher(final Object data, final Hasher h) {
    if (data != null) {
        if (data instanceof Number) {
            final Number d = (Number) data;
            if (data instanceof Integer) {
                h.putInt(d.intValue());/*  w ww  .j  av  a 2  s  . co  m*/
            } else if (data instanceof Double) {
                h.putDouble(d.doubleValue());
            } else if (data instanceof Long) {
                h.putLong(d.longValue());
            } else if (data instanceof Float) {
                h.putFloat(d.floatValue());
            } else if (data instanceof Byte) {
                h.putByte(d.byteValue());
            } else if (data instanceof Short) {
                h.putShort(d.shortValue());
            } else {
                h.putInt(data.hashCode());
            }
        } else if (data instanceof CharSequence) {
            h.putString((CharSequence) data, CHARSET);
        } else if (data.getClass().isArray()) {
            final int size = Array.getLength(data);
            for (int i = 0; i < size; i++) {
                populateHasher(Array.get(data, i), h);
            }
        } else if (data instanceof Iterable) {
            for (final Object o : (Iterable<?>) data) {
                populateHasher(o, h);
            }
        } else {
            h.putInt(data.hashCode());
        }
    }
}

From source file:org.sonatype.nexus.common.hash.MultiHashingOutputStream.java

@Override
public void write(final int b) throws IOException {
    out.write(b);/*from w  w  w.ja va  2s .c  o m*/
    for (Hasher hasher : hashers.values()) {
        hasher.putByte((byte) b);
    }
}