Example usage for org.apache.spark.unsafe.array ByteArrayMethods arrayEquals

List of usage examples for org.apache.spark.unsafe.array ByteArrayMethods arrayEquals

Introduction

In this page you can find the example usage for org.apache.spark.unsafe.array ByteArrayMethods arrayEquals.

Prototype

public static boolean arrayEquals(Object leftBase, long leftOffset, Object rightBase, long rightOffset,
        final long length) 

Source Link

Document

Optimized byte array equality check for byte arrays.

Usage

From source file:edu.ucla.cs.wis.bigdatalog.spark.storage.map.BytesToBytesMap.java

License:Apache License

/**
 * Looks up a key, and saves the result in provided `loc`.
 *
 * This is a thread-safe version of `lookup`, could be used by multiple threads.
 *//*from www  .  ja  v a  2s.  c  o  m*/
public void safeLookup(Object keyBase, long keyOffset, int keyLength,
        edu.ucla.cs.wis.bigdatalog.spark.storage.map.BytesToBytesMap.Location loc) {
    assert (longArray != null);

    if (enablePerfMetrics) {
        numKeyLookups++;
    }
    final int hashcode = HASHER.hashUnsafeWords(keyBase, keyOffset, keyLength);
    int pos = hashcode & mask;
    int step = 1;
    while (true) {
        if (enablePerfMetrics) {
            numProbes++;
        }
        if (longArray.get(pos * 2) == 0) {
            // This is a new key.
            loc.with(pos, hashcode, false);
            return;
        } else {
            long stored = longArray.get(pos * 2 + 1);
            if ((int) (stored) == hashcode) {
                // Full hash code matches.  Let's compare the keys for equality.
                loc.with(pos, hashcode, true);
                if (loc.getKeyLength() == keyLength) {
                    final MemoryLocation keyAddress = loc.getKeyAddress();
                    final Object storedkeyBase = keyAddress.getBaseObject();
                    final long storedkeyOffset = keyAddress.getBaseOffset();
                    final boolean areEqual = ByteArrayMethods.arrayEquals(keyBase, keyOffset, storedkeyBase,
                            storedkeyOffset, keyLength);
                    if (areEqual) {
                        return;
                    } else {
                        if (enablePerfMetrics) {
                            numHashCollisions++;
                        }
                    }
                }
            }
        }
        pos = (pos + step) & mask;
        step++;
    }
}

From source file:edu.ucla.cs.wis.bigdatalog.spark.storage.set.hashset.BytesSet.java

License:Apache License

/**
 * Looks up a key, and saves the result in provided `loc`.
 *
 * This is a thread-safe version of `lookup`, could be used by multiple threads.
 *///from w w  w. j  a  v a  2s.com
public void safeLookup(Object keyBase, long keyOffset, int keyLength, BytesSet.Location loc) {
    assert (longArray != null);

    if (enablePerfMetrics) {
        numKeyLookups++;
    }
    final int hashcode = HASHER.hashUnsafeWords(keyBase, keyOffset, keyLength);
    int pos = hashcode & mask;
    int step = 1;
    while (true) {
        if (enablePerfMetrics) {
            numProbes++;
        }
        if (longArray.get(pos * 2) == 0) {
            // This is a new key.
            loc.with(pos, hashcode, false);
            return;
        } else {
            long stored = longArray.get(pos * 2 + 1);
            if ((int) (stored) == hashcode) {
                // Full hash code matches.  Let's compare the keys for equality.
                loc.with(pos, hashcode, true);
                if (loc.getKeyLength() == keyLength) {
                    final MemoryLocation keyAddress = loc.getKeyAddress();
                    final Object storedkeyBase = keyAddress.getBaseObject();
                    final long storedkeyOffset = keyAddress.getBaseOffset();
                    final boolean areEqual = ByteArrayMethods.arrayEquals(keyBase, keyOffset, storedkeyBase,
                            storedkeyOffset, keyLength);
                    if (areEqual) {
                        return;
                    } else {
                        if (enablePerfMetrics) {
                            numHashCollisions++;
                        }
                    }
                }
            }
        }
        pos = (pos + step) & mask;
        step++;
    }
}