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

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public native int hashCode();

Source Link

Document

Returns a hash code value for the object.

Usage

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

/**
 * Creates a new OTMetric// w w w .j  a v a2  s.  co m
 * @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);
    }
}