/** * Reads a zero-compressed encoded integer from a byte array and returns it. * @param bytes byte array with the encoded integer * @param start start index * @throws java.io.IOException * @return deserialized integer */ public static int readVInt(byte[] bytes, int start) throws IOException { return WritableComparator.readVInt(bytes, start); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * @param bytes byte array with the encoded integer * @param start start index * @throws java.io.IOException * @return deserialized integer */ public static int readVInt(byte[] bytes, int start) throws IOException { return WritableComparator.readVInt(bytes, start); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * @param bytes byte array with the encoded integer * @param start start index * @throws java.io.IOException * @return deserialized integer */ public static int readVInt(byte[] bytes, int start) throws IOException { return WritableComparator.readVInt(bytes, start); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * @param bytes byte array with the encoded integer * @param start start index * @throws java.io.IOException * @return deserialized integer */ public static int readVInt(byte[] bytes, int start) throws IOException { return WritableComparator.readVInt(bytes, start); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * @param bytes byte array with the encoded integer * @param start start index * @throws java.io.IOException * @return deserialized integer */ public static int readVInt(byte[] bytes, int start) throws IOException { return WritableComparator.readVInt(bytes, start); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * @param bytes byte array with the encoded integer * @param start start index * @throws java.io.IOException * @return deserialized integer */ public static int readVInt(byte[] bytes, int start) throws IOException { return WritableComparator.readVInt(bytes, start); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * @param bytes byte array with the encoded integer * @param start start index * @throws java.io.IOException * @return deserialized integer */ public static int readVInt(byte[] bytes, int start) throws IOException { return WritableComparator.readVInt(bytes, start); }
@Override public int getPartition(BytesWritable key, IntWritable value, int numPartitions) { try { int partitionId = WritableComparator.readVInt(key.getBytes(), 0); return Math.abs(partitionId % numPartitions); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public int compareInBytes(byte[] b1, int o1, byte[] b2, int o2) throws IOException { int p1 = WritableComparator.readVInt(b1, o1); int p2 = WritableComparator.readVInt(b2, o2); if (p1 < p2) { return -1; } else if (p1 > p2) { return +1; } WritableRawComparable object = objects[p1]; int meta = WritableUtils.decodeVIntSize(b1[o1]); return object.compareInBytes(b1, o1 + meta, b2, o2 + meta); }
@Override public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { // compare the identifiers int id1; int id2; try { id1 = WritableComparator.readVInt(b1, s1 + LENGTH_BYTES); id2 = WritableComparator.readVInt(b2, s2 + LENGTH_BYTES); } catch (IOException e) { throw new RuntimeException(e); } if (id1 != id2) { return(new Integer(id1).compareTo(id2)); } // compare length if (l1 != l2) { return(new Integer(l1).compareTo(l2)); } // else compare content return WritableComparator.compareBytes(b1, s1 + LENGTH_BYTES, l1 - LENGTH_BYTES, b2, s2 + LENGTH_BYTES, l2 - LENGTH_BYTES ); }
/** * Computes and returns size in bytes. * @param buf bytes array * @param offset bytes offset * @return size in bytes * @throws IOException if failed to compute size */ @Override public final int getSizeInBytes(byte[] buf, int offset) throws IOException { int cursor = 0; for (int i = 0; i < formatters.length; i++) { int metaSize = WritableUtils.decodeVIntSize(buf[offset + cursor]); int bodySize = WritableComparator.readVInt(buf, offset + cursor); cursor += metaSize + bodySize; } return cursor; }
@Override public int getSizeInBytes(byte[] buf, int offset) throws IOException { int pos = WritableComparator.readVInt(buf, offset); WritableRawComparable object = objects[pos]; int meta = WritableUtils.decodeVIntSize(buf[offset]); return meta + object.getSizeInBytes(buf, offset + meta); }
@Override public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { try { int varIntSize; int offset1 = 0; int offset2 = 0; varIntSize = WritableUtils.decodeVIntSize(b1[s1 + offset1]); int slot1 = WritableComparator.readVInt(b1, s1 + offset1); offset1 += varIntSize; varIntSize = WritableUtils.decodeVIntSize(b2[s2 + offset2]); int slot2 = WritableComparator.readVInt(b2, s2 + offset2); offset2 += varIntSize; if (slot1 != slot2) { return slot1 - slot2; } varIntSize = WritableUtils.decodeVIntSize(b1[s1 + offset1]); int length1 = WritableComparator.readVInt(b1, s1 + offset1); offset1 += varIntSize; varIntSize = WritableUtils.decodeVIntSize(b2[s2 + offset2]); int length2 = WritableComparator.readVInt(b2, s2 + offset2); offset2 += varIntSize; return compareBytes( b1, s1 + offset1, length1, b2, s2 + offset2, length2); } catch (IOException e) { // bad manner throw new RuntimeException(e); } }
/** * Returns the actual number of bytes from the serialized byte array. * @param bytes the target byte array * @param offset the beginning index in the byte array (inclusive) * @param length the limit length of the byte array * @return the comparison result */ public static int getBytesLength(byte[] bytes, int offset, int length) { try { int cursor = offset; int head = bytes[cursor++] & 0xff; if ((head & MASK_PRESENT) != 0) { cursor += WritableUtils.decodeVIntSize(bytes[cursor]); int bytesLength = WritableComparator.readVInt(bytes, cursor); cursor += WritableUtils.decodeVIntSize(bytes[cursor]); cursor += bytesLength; } return cursor - offset; } catch (IOException e) { throw new IllegalStateException(e); } }
@SuppressWarnings("deprecation") @Override public int restore(byte[] bytes, int offset, int limit) throws IOException { int cursor = offset; int head = bytes[cursor++] & 0xff; if ((head & MASK_PRESENT) == 0) { setNull(); } else { boolean plus = (head & MASK_PLUS) != 0; int scale = WritableComparator.readVInt(bytes, cursor); cursor += WritableUtils.decodeVIntSize(bytes[cursor]); int length = WritableComparator.readVInt(bytes, cursor); cursor += WritableUtils.decodeVIntSize(bytes[cursor]); DecimalBuffer buffer = BUFFER_MAIN.get(); buffer.set(plus, scale, bytes, cursor, length); cursor += length; modify(buffer.toBigDecimal()); } return cursor - offset; }
int scale1 = WritableComparator.readVInt(b1, cursor1); int scale2 = WritableComparator.readVInt(b2, cursor2); cursor1 += WritableUtils.decodeVIntSize(b1[cursor1]); cursor2 += WritableUtils.decodeVIntSize(b2[cursor2]); int bytesCount1 = WritableComparator.readVInt(b1, cursor1); int bytesCount2 = WritableComparator.readVInt(b2, cursor2); cursor1 += WritableUtils.decodeVIntSize(b1[cursor1]); cursor2 += WritableUtils.decodeVIntSize(b2[cursor2]);
protected int compareMultipleSources(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) throws IOException { Schema commonSchema = serInfo.getCommonSchema(); Criteria commonOrder = tupleMRConf.getCommonCriteria(); int comparison = compare(b1, s1, b2, s2, commonSchema, commonOrder, offsets, nulls); if (comparison != 0) { return comparison; } int schemaId1 = readVInt(b1, offsets.offset1); int schemaId2 = readVInt(b2, offsets.offset2); if (schemaId1 != schemaId2) { int r = schemaId1 - schemaId2; return (tupleMRConf.getSchemasOrder() == Order.ASC) ? r : -r; } int vintSize = WritableUtils.decodeVIntSize(b1[offsets.offset1]); offsets.offset1 += vintSize; offsets.offset2 += vintSize; // sources are the same Criteria criteria = tupleMRConf.getSpecificOrderBys().get(schemaId1); if (criteria == null) { return 0; } Schema specificSchema = serInfo.getSpecificSchema(schemaId1); return compare(b1, offsets.offset1, b2, offsets.offset2, specificSchema, criteria, offsets, nulls); }
protected int compareMultipleSources(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) throws IOException { Schema commonSchema = serInfo.getCommonSchema(); Criteria commonOrder = tupleMRConf.getCommonCriteria(); int comparison = compare(b1, s1, b2, s2, commonSchema, commonOrder, offsets, nulls); if (comparison != 0) { return comparison; } int schemaId1 = readVInt(b1, offsets.offset1); int schemaId2 = readVInt(b2, offsets.offset2); if (schemaId1 != schemaId2) { int r = schemaId1 - schemaId2; return (tupleMRConf.getSchemasOrder() == Order.ASC) ? r : -r; } int vintSize = WritableUtils.decodeVIntSize(b1[offsets.offset1]); offsets.offset1 += vintSize; offsets.offset2 += vintSize; // sources are the same Criteria criteria = tupleMRConf.getSpecificOrderBys().get(schemaId1); if (criteria == null) { return 0; } Schema specificSchema = serInfo.getSpecificSchema(schemaId1); return compare(b1, offsets.offset1, b2, offsets.offset2, specificSchema, criteria, offsets, nulls); }
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]); 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; 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 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]); 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; 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]);