Example usage for org.apache.hadoop.io WritableUtils decodeVIntSize

List of usage examples for org.apache.hadoop.io WritableUtils decodeVIntSize

Introduction

In this page you can find the example usage for org.apache.hadoop.io WritableUtils decodeVIntSize.

Prototype

public static int decodeVIntSize(byte value) 

Source Link

Document

Parse the first byte of a vint/vlong to determine the number of bytes

Usage

From source file:it.crs4.seal.demux.TwoOneThreeSortComparator.java

License:Open Source License

@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
    if (ByteSize != 1)
        throw new RuntimeException("Byte size != 1 (it's " + ByteSize + "). Make sure this code still works!");

    int sizeVint1 = WritableUtils.decodeVIntSize(b1[s1]);
    int sizeVint2 = WritableUtils.decodeVIntSize(b2[s2]);

    // compare by the location field (i.e. everything except the last byte.
    int locationCmp = WritableComparator.compareBytes(b1, s1 + sizeVint1, l1 - sizeVint1 - ByteSize, b2,
            s2 + sizeVint2, l2 - sizeVint2 - ByteSize);

    if (locationCmp == 0) // same location
    {//from  ww w. j  a va2s  . c  o m
        // SequenceId writes the read number to the last byte of the record.
        byte r1 = b1[s1 + l1 - 1];
        byte r2 = b2[s2 + l2 - 1];
        int retval;
        if (r1 == r2)
            retval = 0;
        else if (r1 == 2)
            retval = -1;
        else if (r2 == 2)
            retval = 1;
        else
            retval = (r1 < r2) ? -1 : 1;
        return retval;
    } else
        return locationCmp;
}

From source file:org.apache.gora.util.ByteUtils.java

License:Apache License

/**
 * @param buffer/*from   www. j a  v  a 2s. com*/
 * @return vint bytes as an integer.
 */
public static long bytesToVlong(final byte[] buffer) {
    int offset = 0;
    byte firstByte = buffer[offset++];
    int len = WritableUtils.decodeVIntSize(firstByte);
    if (len == 1) {
        return firstByte;
    }
    long i = 0;
    for (int idx = 0; idx < len - 1; idx++) {
        byte b = buffer[offset++];
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}

From source file:org.apache.gora.util.ByteUtils.java

License:Apache License

/**
 * @param buffer//from   ww w  . java 2 s. c om
 * @return vint bytes as an integer.
 */
public static int bytesToVint(final byte[] buffer) {
    int offset = 0;
    byte firstByte = buffer[offset++];
    int len = WritableUtils.decodeVIntSize(firstByte);
    if (len == 1) {
        return firstByte;
    }
    long i = 0;
    for (int idx = 0; idx < len - 1; idx++) {
        byte b = buffer[offset++];
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (int) (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}

From source file:org.apache.gora.util.ByteUtils.java

License:Apache License

/**
 * Reads a zero-compressed encoded long from input stream and returns it.
 * @param buffer Binary array//from w  ww  .ja v  a 2 s. c om
 * @param offset Offset into array at which vint begins.
 * @throws java.io.IOException
 * @return deserialized long from stream.
 */
public static long readVLong(final byte[] buffer, final int offset) throws IOException {
    byte firstByte = buffer[offset];
    int len = WritableUtils.decodeVIntSize(firstByte);
    if (len == 1) {
        return firstByte;
    }
    long i = 0;
    for (int idx = 0; idx < len - 1; idx++) {
        byte b = buffer[offset + 1 + idx];
        i = i << 8;
        i = i | (b & 0xFF);
    }
    return (WritableUtils.isNegativeVInt(firstByte) ? (i ^ -1L) : i);
}

From source file:org.apache.ignite.hadoop.io.TextPartiallyRawComparator.java

License:Apache License

/** {@inheritDoc} */
@Override//w w  w.  ja v a2  s  .com
public int compare(Text val1, long val2Ptr, int val2Len) {
    int len2 = WritableUtils.decodeVIntSize(GridUnsafe.getByte(val2Ptr));

    return HadoopUtils.compareBytes(val1.getBytes(), val1.getLength(), val2Ptr + len2, val2Len - len2);
}

From source file:org.apache.mrql.Bag.java

License:Apache License

/** compare this Bag with a given Bag by comparing their associated elements */
final public static int compare(byte[] x, int xs, int xl, byte[] y, int ys, int yl, int[] size) {
    try {/*ww w .  jav  a  2 s  .c o m*/
        int xn = WritableComparator.readVInt(x, xs);
        int xx = WritableUtils.decodeVIntSize(x[xs]);
        int yn = WritableComparator.readVInt(y, ys);
        int yy = WritableUtils.decodeVIntSize(y[ys]);
        for (int i = 0; i < xn && i < yn; i++) {
            int k = MRContainer.compare(x, xs + xx, xl - xx, y, ys + yy, yl - yy, size);
            if (k != 0)
                return k;
            xx += size[0];
            yy += size[0];
        }
        ;
        size[0] = xx + 1;
        if (xn > yn)
            return 1;
        if (xn < yn)
            return -1;
        return 0;
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:org.apache.mrql.MR_int.java

License:Apache License

final public static int compare(byte[] x, int xs, int xl, byte[] y, int ys, int yl, int[] size) {
    try {//from  ww  w. j  ava2  s  .c  o m
        size[0] = 1 + WritableUtils.decodeVIntSize(x[xs]);
        int v = WritableComparator.readVInt(x, xs) - WritableComparator.readVInt(y, ys);
        return (v == 0) ? 0 : ((v > 0) ? 1 : -1);
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:org.apache.mrql.MR_long.java

License:Apache License

final public static int compare(byte[] x, int xs, int xl, byte[] y, int ys, int yl, int[] size) {
    try {/*from   ww  w .  ja v  a  2s  . c  om*/
        size[0] = 1 + WritableUtils.decodeVIntSize(x[xs]);
        long v = WritableComparator.readVLong(x, xs) - WritableComparator.readVLong(y, ys);
        return (v == 0) ? 0 : ((v > 0) ? 1 : -1);
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:org.apache.mrql.MR_string.java

License:Apache License

final public static int compare(byte[] x, int xs, int xl, byte[] y, int ys, int yl, int[] size) {
    try {//from  ww  w .  j ava2  s.  co  m
        size[0] = 1 + WritableComparator.readVInt(x, xs) + WritableUtils.decodeVIntSize(x[xs]);
        return comparator.compare(x, xs, xl, y, ys, yl);
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:org.apache.mrql.Tuple.java

License:Apache License

final public static int compare(byte[] x, int xs, int xl, byte[] y, int ys, int yl, int[] size) {
    try {/*from   ww  w. j  a va2s  .c  om*/
        int n = WritableComparator.readVInt(x, xs);
        int s = WritableUtils.decodeVIntSize(x[xs]);
        for (short i = 0; i < n; i++) {
            int k = MRContainer.compare(x, xs + s, xl - s, y, ys + s, yl - s, size);
            if (k != 0)
                return k;
            s += size[0];
        }
        ;
        size[0] = s + 1;
        return 0;
    } catch (IOException e) {
        throw new Error(e);
    }
}