public void writeTo(ByteBuffer bb) { if (bb.remaining() < getLength()) { throw new IndexOutOfBoundsException("remaining length: " + bb.remaining() + ", tuple length: " + getLength()); } if (getLength() > 0) { if (bb.isDirect()) { PlatformDependent.copyMemory(address(), PlatformDependent.directBufferAddress(bb) + bb.position(), getLength()); bb.position(bb.position() + getLength()); } else { PlatformDependent.copyMemory(address(), bb.array(), bb.arrayOffset() + bb.position(), getLength()); bb.position(bb.position() + getLength()); } } }
public UnSafeTuple buildToZeroCopyTuple() { UnSafeTuple zcTuple = new UnSafeTuple(); zcTuple.set(memoryBlock, memoryBlock.readerPosition(), dataTypes()); return zcTuple; }
public long getFieldAddr(int fieldId) { int fieldOffset = getFieldOffset(fieldId); if (fieldOffset < 0 || fieldOffset > getLength()) { throw new RuntimeException("Invalid Access. Field : " + fieldId + ", Offset:" + fieldOffset + ", Record length:" + getLength()); } return address() + fieldOffset; }
@Override public Datum asDatum(int fieldId) { if (isBlankOrNull(fieldId)) { return NullDatum.get(); return DatumFactory.createBool(getBool(fieldId)); case BIT: return DatumFactory.createBit(getByte(fieldId)); case INT1: case INT2: return DatumFactory.createInt2(getInt2(fieldId)); case INT4: return DatumFactory.createInt4(getInt4(fieldId)); case INT8: return DatumFactory.createInt8(getInt8(fieldId)); case FLOAT4: return DatumFactory.createFloat4(getFloat4(fieldId)); case FLOAT8: return DatumFactory.createFloat8(getFloat8(fieldId)); case CHAR: return DatumFactory.createChar(getBytes(fieldId)); case TEXT: return DatumFactory.createText(getBytes(fieldId)); case BLOB: return DatumFactory.createBlob(getBytes(fieldId)); case TIMESTAMP: return DatumFactory.createTimestamp(getInt8(fieldId)); case DATE: return DatumFactory.createDate(getInt4(fieldId)); case TIME:
public static final int compareColumn(UnSafeTuple tuple1, UnSafeTuple tuple2, int index, TajoDataTypes.Type type, boolean ascending, boolean nullFirst) { final boolean n1 = tuple1.isBlankOrNull(index); final boolean n2 = tuple2.isBlankOrNull(index); if (n1 && n2) { return 0; switch (type) { case BOOLEAN: compare = Booleans.compare(tuple1.getBool(index), tuple2.getBool(index)); break; case BIT: compare = tuple1.getByte(index) - tuple2.getByte(index); break; case INT1: case INT2: compare = Shorts.compare(tuple1.getInt2(index), tuple2.getInt2(index)); break; case DATE: case INT4: compare = Ints.compare(tuple1.getInt4(index), tuple2.getInt4(index)); break; case TIME: case TIMESTAMP: case INT8: compare = Longs.compare(tuple1.getInt8(index), tuple2.getInt8(index)); break; case FLOAT4: compare = Floats.compare(tuple1.getFloat4(index), tuple2.getFloat4(index)); break;
/** * Get a 16-bit radix key from a column values of the given tuple. * The return key is an unsigned short value. * * @param tuple * @param sortKeyId * @param pass * @return */ static int ascNullLast16RadixKey(UnSafeTuple tuple, int sortKeyId, int pass) { int key = _16BIT_NULL_LAST_IDX; // for null if (!tuple.isBlankOrNull(sortKeyId)) { key = 1 + (PlatformDependent.getShort(getFieldAddr(tuple.address(), sortKeyId) + (pass)) & SHORT_UNSIGNED_MASK); } return key; }
@Override public short getInt2(int fieldId) { long addr = getFieldAddr(fieldId); return PlatformDependent.getShort(addr); }
@Override public int getLength() { return PlatformDependent.getInt(address()); }
@Override public boolean isBlankOrNull(int fieldid) { return getFieldOffset(fieldid) == MemoryRowBlock.NULL_FIELD_OFFSET; }
@Override public String getText(int fieldId) { return new String(getBytes(fieldId), TextDatum.DEFAULT_CHARSET); }
@Override public TimeMeta getTimeDate(int fieldId) { return asDatum(fieldId).asTimeMeta(); }
public static List<Tuple> sort(MemoryRowBlock rowBlock, Comparator<Tuple> comparator) { List<Tuple> tupleList = Lists.newArrayList(); ZeroCopyTuple zcTuple; if(rowBlock.getMemory().hasAddress()) { zcTuple = new UnSafeTuple(); } else { zcTuple = new HeapTuple(); } RowBlockReader reader = rowBlock.getReader(); while(reader.next(zcTuple)) { tupleList.add(zcTuple); if(rowBlock.getMemory().hasAddress()) { zcTuple = new UnSafeTuple(); } else { zcTuple = new HeapTuple(); } } Collections.sort(tupleList, comparator); return tupleList; }
@Override public Datum asDatum(int fieldId) { if (isBlankOrNull(fieldId)) { return NullDatum.get(); return DatumFactory.createBool(getBool(fieldId)); case BIT: return DatumFactory.createBit(getByte(fieldId)); case INT1: case INT2: return DatumFactory.createInt2(getInt2(fieldId)); case INT4: return DatumFactory.createInt4(getInt4(fieldId)); case INT8: return DatumFactory.createInt8(getInt8(fieldId)); case FLOAT4: return DatumFactory.createFloat4(getFloat4(fieldId)); case FLOAT8: return DatumFactory.createFloat8(getFloat8(fieldId)); case CHAR: return DatumFactory.createChar(getBytes(fieldId)); case TEXT: return DatumFactory.createText(getBytes(fieldId)); case BLOB: return DatumFactory.createBlob(getBytes(fieldId)); case TIMESTAMP: return DatumFactory.createTimestamp(getInt8(fieldId)); case DATE: return DatumFactory.createDate(getInt4(fieldId)); case TIME:
public static final int compareColumn(UnSafeTuple tuple1, UnSafeTuple tuple2, int index, TajoDataTypes.Type type, boolean ascending, boolean nullFirst) { final boolean n1 = tuple1.isBlankOrNull(index); final boolean n2 = tuple2.isBlankOrNull(index); if (n1 && n2) { return 0; switch (type) { case BOOLEAN: compare = Booleans.compare(tuple1.getBool(index), tuple2.getBool(index)); break; case BIT: compare = tuple1.getByte(index) - tuple2.getByte(index); break; case INT1: case INT2: compare = Shorts.compare(tuple1.getInt2(index), tuple2.getInt2(index)); break; case DATE: case INT4: compare = Ints.compare(tuple1.getInt4(index), tuple2.getInt4(index)); break; case INET4: compare = UnsignedInts.compare(tuple1.getInt4(index), tuple2.getInt4(index)); break; case TIME: case TIMESTAMP: case INT8: compare = Longs.compare(tuple1.getInt8(index), tuple2.getInt8(index)); break;
/** * Get a 1-bit radix key from a column values of the given tuple. * The keys of 0 and 3 are reserved for null values. * * @param tuple * @param sortKeyId * @param pass * @return */ static int ascNullFirst1bRadixKey(UnSafeTuple tuple, int sortKeyId, int pass) { int key = 0; // for null if (!tuple.isBlankOrNull(sortKeyId)) { key = 2 - ((PlatformDependent.getShort(getFieldAddr(tuple.address(), sortKeyId) + (pass)) & 0xFFFF) >> 15); } return key; }
public long getFieldAddr(int fieldId) { int fieldOffset = getFieldOffset(fieldId); if (fieldOffset < 0 || fieldOffset > getLength()) { throw new RuntimeException("Invalid Access. Field : " + fieldId + ", Offset:" + fieldOffset + ", Record length:" + getLength()); } return address() + fieldOffset; }
@Override public short getInt2(int fieldId) { long addr = getFieldAddr(fieldId); return PlatformDependent.getShort(addr); }
@Override public int getLength() { return PlatformDependent.getInt(address()); }
@Override public boolean contains(int fieldid) { return getFieldOffset(fieldid) > MemoryRowBlock.NULL_FIELD_OFFSET; }