Example usage for org.apache.hadoop.io RawComparator compare

List of usage examples for org.apache.hadoop.io RawComparator compare

Introduction

In this page you can find the example usage for org.apache.hadoop.io RawComparator compare.

Prototype

public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2);

Source Link

Document

Compare two objects in binary.

Usage

From source file:cn.iie.haiep.hbase.value.Bytes.java

License:Apache License

/**
 * Binary search for keys in indexes.// w  ww  .j  a  v a 2s .c  om
 *
 * @param arr array of byte arrays to search for
 * @param key the key you want to find
 * @param offset the offset in the key you want to find
 * @param length the length of the key
 * @param comparator a comparator to compare.
 * @return zero-based index of the key, if the key is present in the array.
 *         Otherwise, a value -(i + 1) such that the key is between arr[i -
 *         1] and arr[i] non-inclusively, where i is in [0, i], if we define
 *         arr[-1] = -Inf and arr[N] = Inf for an N-element array. The above
 *         means that this function can return 2N + 1 different values
 *         ranging from -(N + 1) to N - 1.
 */
public static int binarySearch(byte[][] arr, byte[] key, int offset, int length,
        RawComparator<byte[]> comparator) {
    int low = 0;
    int high = arr.length - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        // we have to compare in this order, because the comparator order
        // has special logic when the 'left side' is a special key.
        int cmp = comparator.compare(key, offset, length, arr[mid], 0, arr[mid].length);
        // key lives above the midpoint
        if (cmp > 0)
            low = mid + 1;
        // key lives below the midpoint
        else if (cmp < 0)
            high = mid - 1;
        // BAM. how often does this really happen?
        else
            return mid;
    }
    return -(low + 1);
}

From source file:com.chinamobile.bcbsp.util.Bytes.java

License:Apache License

/**
 * Binary search for keys in indexes./*from w  w w  . jav  a  2s  .co  m*/
 *
 * @param arr
 *        array of byte arrays to search for
 * @param key
 *        the key you want to find
 * @param offset
 *        the offset in the key you want to find
 * @param length
 *        the length of the key
 * @param comparator
 *        a comparator to compare.
 * @return index of key
 */
public static int binarySearch(byte[][] arr, byte[] key, int offset, int length,
        RawComparator<byte[]> comparator) {
    int low = 0;
    int high = arr.length - 1;
    while (low <= high) {
        int mid = (low + high) >>> 1;
        // we have to compare in this order, because the comparator order
        // has special logic when the 'left side' is a special key.
        int cmp = comparator.compare(key, offset, length, arr[mid], 0, arr[mid].length);
        // key lives above the midpoint
        if (cmp > 0) {
            low = mid + 1;
        } else if (cmp < 0) {
            // key lives below the midpoint
            high = mid - 1;
        } else {
            // BAM. how often does this really happen?
            return mid;
        }
    }
    return -(low + 1);
}

From source file:com.datasalt.pangool.tuplemr.mapred.SortComparator.java

License:Apache License

protected int compare(byte[] b1, int s1, byte[] b2, int s2, Schema schema, Criteria criteria, Offsets o,
        Nulls n) throws IOException {
    o.offset1 = s1;/*from w w w  . j  av  a  2s .c  om*/
    o.offset2 = s2;

    // Reading nulls bit field, if present
    if (schema.containsNullableFields()) {
        o.offset1 += n.nulls1.deser(b1, s1);
        o.offset2 += n.nulls2.deser(b2, s2);
    }

    for (int depth = 0; depth < criteria.getElements().size(); depth++) {
        Field field = schema.getField(depth);
        Field.Type type = field.getType();
        SortElement sortElement = criteria.getElements().get(depth);
        Order sort = sortElement.getOrder();
        RawComparator comparator = sortElement.getCustomComparator();

        // Control for nulls, if field is nullable.
        if (field.isNullable()) {
            Criteria.NullOrder nullOrder = sortElement.getNullOrder();
            if (n.nulls1.isSet(schema.getNullablePositionFromIndex(depth))) {
                if (n.nulls2.isSet(schema.getNullablePositionFromIndex(depth))) {
                    // Both are null, so both are equal. No space is used. Continue.
                    continue;
                } else {
                    // First is null
                    return (nullOrder == Criteria.NullOrder.NULL_SMALLEST && sort == Order.ASC) ? -1 : 1;
                }
            } else if (n.nulls2.isSet(schema.getNullablePositionFromIndex(depth))) {
                // Second is null
                return (nullOrder == Criteria.NullOrder.NULL_SMALLEST && sort == Order.ASC) ? 1 : -1;
            }
        }

        if (comparator != null) {
            //custom comparator for OBJECT
            int length1 = WritableComparator.readVInt(b1, o.offset1);
            int length2 = WritableComparator.readVInt(b2, o.offset2);
            o.offset1 += WritableUtils.decodeVIntSize(b1[o.offset1]);
            o.offset2 += WritableUtils.decodeVIntSize(b2[o.offset2]);
            int comparison = comparator.compare(b1, o.offset1, length1, b2, o.offset2, length2);
            o.offset1 += length1;
            o.offset2 += length2;
            if (comparison != 0) {
                return (sort == Order.ASC) ? comparison : -comparison;
            }
        } else {
            //not custom comparator
            switch (type) {
            case INT:
            case ENUM: {
                int value1 = readVInt(b1, o.offset1);
                int value2 = readVInt(b2, o.offset2);
                if (value1 > value2) {
                    return (sort == Order.ASC) ? 1 : -1;
                } else if (value1 < value2) {
                    return (sort == Order.ASC) ? -1 : 1;
                }
                int vintSize = WritableUtils.decodeVIntSize(b1[o.offset1]);
                o.offset1 += vintSize;
                o.offset2 += vintSize;
            }
                break;
            case LONG: {
                long value1 = readVLong(b1, o.offset1);
                long value2 = readVLong(b2, o.offset2);
                if (value1 > value2) {
                    return (sort == Order.ASC) ? 1 : -1;
                } else if (value1 < value2) {
                    return (sort == Order.ASC) ? -1 : 1;
                }
                int vIntSize = WritableUtils.decodeVIntSize(b1[o.offset1]);
                o.offset1 += vIntSize;
                o.offset2 += vIntSize;
            }
                break;
            case FLOAT: {
                float value1 = readFloat(b1, o.offset1);
                float value2 = readFloat(b2, o.offset2);
                int comp = Float.compare(value1, value2);
                if (comp != 0) {
                    return (sort == Order.ASC) ? comp : -comp;
                }
                o.offset1 += Float.SIZE / 8;
                o.offset2 += Float.SIZE / 8;
            }
                break;
            case DOUBLE: {
                double value1 = readDouble(b1, o.offset1);
                double value2 = readDouble(b2, o.offset2);
                int comp = Double.compare(value1, value2);
                if (comp != 0) {
                    return (sort == Order.ASC) ? comp : -comp;
                }
                o.offset1 += Double.SIZE / 8;
                o.offset2 += Double.SIZE / 8;
            }
                break;
            case BOOLEAN: {
                byte value1 = b1[o.offset1++];
                byte value2 = b2[o.offset2++];
                if (value1 > value2) {
                    return (sort == Order.ASC) ? 1 : -1;
                } else if (value1 < value2) {
                    return (sort == Order.ASC) ? -1 : 1;
                }
            }
                break;
            case STRING:
            case OBJECT:
            case BYTES: {
                int length1 = readVInt(b1, o.offset1);
                int length2 = readVInt(b2, o.offset2);
                o.offset1 += WritableUtils.decodeVIntSize(b1[o.offset1]);
                o.offset2 += WritableUtils.decodeVIntSize(b2[o.offset2]);
                int comparison = compareBytes(b1, o.offset1, length1, b2, o.offset2, length2);
                o.offset1 += length1;
                o.offset2 += length2;
                if (comparison != 0) {
                    return (sort == Order.ASC) ? comparison : (-comparison);
                }
            }
                break;
            default:
                throw new IOException("Not supported comparison for type:" + type);
            }
        }
    }
    return 0; // equals
}

From source file:edu.uci.ics.hyracks.dataflow.hadoop.data.KeyBinaryComparatorFactory.java

License:Apache License

@Override
public IBinaryComparator createBinaryComparator() {
    final RawComparator<T> instance = ReflectionUtils.createInstance(cmpClass);
    return new IBinaryComparator() {
        @Override//from w ww.  j  a v a  2s.com
        public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
            return instance.compare(b1, s1, l1, b2, s2, l2);
        }
    };
}

From source file:edu.uci.ics.pregelix.runtime.touchpoint.WritableDescComparingBinaryComparatorFactory.java

License:Apache License

@Override
public IBinaryComparator createBinaryComparator() {
    final RawComparator<T> instance = ReflectionUtils.createInstance(cmpClass);
    return new IBinaryComparator() {
        @Override//from   w w  w . jav a2s.com
        public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
            return -instance.compare(b1, s1, l1, b2, s2, l2);
        }
    };
}

From source file:org.apache.accumulo.pig.Bytes.java

License:Apache License

/**
 * Binary search for keys in indexes.// w  ww .  ja v  a  2  s.  c  o m
 *
 * @param arr array of byte arrays to search for
 * @param key the key you want to find
 * @param offset the offset in the key you want to find
 * @param length the length of the key
 * @param comparator a comparator to compare.
 * @return index of key
 */
public static int binarySearch(byte[][] arr, byte[] key, int offset, int length,
        RawComparator<byte[]> comparator) {
    int low = 0;
    int high = arr.length - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        // we have to compare in this order, because the comparator order
        // has special logic when the 'left side' is a special key.
        int cmp = comparator.compare(key, offset, length, arr[mid], 0, arr[mid].length);
        // key lives above the midpoint
        if (cmp > 0) {
            low = mid + 1;
        } // key lives below the midpoint
        else if (cmp < 0) {
            high = mid - 1;
        } // BAM. how often does this really happen?
        else {
            return mid;
        }
    }
    return -(low + 1);
}

From source file:org.apache.pig.test.TestPigTupleRawComparator.java

License:Apache License

private int compareHelper(NullableTuple t1, NullableTuple t2, RawComparator comparator) throws IOException {
    t1.write(dos1);/*from www.  j a v  a2  s  . co m*/
    t2.write(dos2);
    byte[] b1 = baos1.toByteArray();
    byte[] b2 = baos2.toByteArray();
    baos1.reset();
    baos2.reset();
    return comparator.compare(b1, 0, b1.length, b2, 0, b2.length);
}

From source file:org.apache.tajo.util.Bytes.java

License:Apache License

/**
 * Binary search for keys in indexes.//from w w  w  .  j a  va  2  s .  c o m
 *
 * @param arr array of byte arrays to search for
 * @param key the key you want to find
 * @param offset the offset in the key you want to find
 * @param length the length of the key
 * @param comparator a comparator to compare.
 * @return zero-based index of the key, if the key is present in the array.
 *         Otherwise, a value -(i + 1) such that the key is between arr[i -
 *         1] and arr[i] non-inclusively, where i is in [0, i], if we define
 *         arr[-1] = -Inf and arr[N] = Inf for an N-element array. The above
 *         means that this function can return 2N + 1 different values
 *         ranging from -(N + 1) to N - 1.
 */
public static int binarySearch(byte[][] arr, byte[] key, int offset, int length, RawComparator<?> comparator) {
    int low = 0;
    int high = arr.length - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        // we have to compare in this order, because the comparator order
        // has special logic when the 'left side' is a special key.
        int cmp = comparator.compare(key, offset, length, arr[mid], 0, arr[mid].length);
        // key lives above the midpoint
        if (cmp > 0)
            low = mid + 1;
        // key lives below the midpoint
        else if (cmp < 0)
            high = mid - 1;
        // BAM. how often does this really happen?
        else
            return mid;
    }
    return -(low + 1);
}