Example usage for com.google.common.hash HashCode padToLong

List of usage examples for com.google.common.hash HashCode padToLong

Introduction

In this page you can find the example usage for com.google.common.hash HashCode padToLong.

Prototype

@CheckReturnValue
public abstract long padToLong();

Source Link

Document

If this hashcode has enough bits, returns asLong() , otherwise returns a long value with asBytes() as the least-significant bytes and 0x00 as the remaining most-significant bytes.

Usage

From source file:org.breizhbeans.thrift.tools.thriftmongobridge.example.SecuredWrapper.java

@Override
public long digest64(byte[] data) {
    // initialize the hash with the AES Hash code
    int seed = Arrays.hashCode(skeyspec.getEncoded());
    HashFunction hf = Hashing.murmur3_128(seed);

    com.google.common.hash.HashCode hashCode = hf.hashBytes(data);

    return hashCode.padToLong();
}

From source file:com.facebook.buck.zip.ZipDirectoryWithMaxDeflateStep.java

private void addDirectoryToZipEntryList(File directory, String currentPath,
        ImmutableMap.Builder<File, ZipEntry> zipEntriesBuilder) throws IOException {
    Preconditions.checkNotNull(currentPath);

    for (File inputFile : directory.listFiles()) {
        String childPath = currentPath + (currentPath.isEmpty() ? "" : "/") + inputFile.getName();

        if (inputFile.isDirectory()) {
            addDirectoryToZipEntryList(inputFile, childPath, zipEntriesBuilder);
        } else {//from  w w  w .j  a va2s .  c  o  m
            ZipEntry nextEntry = new ZipEntry(childPath);
            long fileLength = inputFile.length();
            if (fileLength > maxDeflatedBytes
                    || EXTENSIONS_NOT_TO_DEFLATE.contains(Files.getFileExtension(inputFile.getName()))) {
                nextEntry.setMethod(ZipEntry.STORED);
                nextEntry.setCompressedSize(inputFile.length());
                nextEntry.setSize(inputFile.length());
                HashCode crc = ByteStreams.hash(Files.newInputStreamSupplier(inputFile), Hashing.crc32());
                nextEntry.setCrc(crc.padToLong());
            }

            zipEntriesBuilder.put(inputFile, nextEntry);
        }
    }
}

From source file:com.heliosapm.opentsdb.client.opentsdb.OTMetric.java

/**
 * Creates a new OTMetric/* w  ww  . j a  v  a  2 s. c om*/
 * @param flatName The plain flat name from the Metric name
 * @param nprefix The optional prefix which is prefixed to the flat name
 * @param extension The optional extension which is appended to the TSDB metric name
 * @param extraTags The optional extra tags to add
 */
OTMetric(final String flatName, final String nprefix, final String extension,
        final Map<String, String> extraTags) {
    if (flatName == null)
        throw new IllegalArgumentException("The passed flat name was null");
    final SplitFlatName sfn = OTMetric.splitFlatName(flatName);
    sfn.addTags(extraTags);
    final String fprefix = pref(nprefix);
    final String fext = suff(extension);
    final boolean isext = !"".equals(fext);
    final byte[] metricName = (fprefix + sfn.getMetricName() + fext).getBytes(UTF8);
    final Hasher hasherx = OTMETRIC_HASH.newHasher();
    PrimitiveSink hasher = new CapturingPrimitiveSink(hasherx);
    if (!"".equals(fprefix)) {
        hasher.putBytes(fprefix.getBytes(UTF8));
        //         hasher.putBytes(DOT);
    }
    hasher.putBytes(sfn.getMetricName().getBytes(UTF8));
    if (isext) {
        //         hasher.putBytes(DOT);
        hasher.putBytes(fext.getBytes(UTF8));
    }
    //hasher.putBytes(metricName);
    ByteBuffer buff = null;
    try {
        buff = ByteBuffer.allocateDirect((metricName.length + sfn.estimateSize()) * 3); // FIXME: Need a closer estimate
        buff.putLong(0) // the long hash code, Zero for now
                .putInt(0) // the java hash code, Zero for now
                .putLong(0) // the last trace time, Zero
                .put(sfn.hasAppTag() ? ONE_BYTE : ZERO_BYTE) // App Tag
                .put(sfn.hasHostTag() ? ONE_BYTE : ZERO_BYTE) // Host Tag
                .put(isext ? ONE_BYTE : ZERO_BYTE) // Ext flag
                .putLong(0L) // Parent long hash code
                .put(ZERO_BYTE) // CHMetric type mask
                .putInt(0) // The measurement mask
                .putInt(0) // SubMetric type flag
                .putInt(0) // Total Length, Zero for now
                .putInt(metricName.length) // Length of the prefix
                .putInt(sfn.getTags().size()) // Tag count
                .put(metricName); // The metric name bytes

        // IS_EXT_TAG || PARENT_TAG(8), CHMETRIC_TAG(1),  MEASUREMENT_TAG(4),  SUB_METRIC_TAG(4)      

        int totalLength = metricName.length;
        if (!sfn.getTags().isEmpty()) {
            for (Map.Entry<String, String> entry : sfn.getTags().entrySet()) {
                final byte[] key = entry.getKey().getBytes(UTF8);
                final byte[] value = entry.getValue().getBytes(UTF8);
                int tagLength = key.length + value.length + TAG_OVERHEAD;
                hasher.putBytes(key);
                hasher.putBytes(value);
                buff.putInt(tagLength).put(QT).put(key).put(QT).put(COLON).put(QT).put(value).put(QT);
                totalLength += tagLength;
            }
        }

        final int pos = buff.position();
        final HashCode hashCode = hasherx.hash();
        buff.putLong(LONG_HASH_CODE, hashCode.padToLong());
        buff.putInt(HASH_CODE, hashCode.hashCode());
        buff.putInt(TOTAL_SIZE_OFFSET, totalLength);
        buff.limit(pos);
        buff.position(0);
        nameBuffer = ByteBuffer.allocateDirect(buff.limit()).put(buff); //.asReadOnlyBuffer();
    } finally {
        OffHeapFIFOFile.clean(buff);
    }
}