public boolean append(BlockCursor cursor) { // the extra BYTE here is for the null flag int writableBytes = sliceOutput.writableBytes() - SIZE_OF_BYTE; boolean isNull = cursor.isNull(); if (type == Type.FIXED_INT_64) { if (writableBytes < SIZE_OF_LONG) { return false; } positionOffsets.add(sliceOutput.size()); sliceOutput.writeByte(isNull ? 1 : 0); sliceOutput.appendLong(isNull ? 0 : cursor.getLong()); } else if (type == Type.DOUBLE) { if (writableBytes < SIZE_OF_DOUBLE) { return false; } positionOffsets.add(sliceOutput.size()); sliceOutput.writeByte(isNull ? 1 : 0); sliceOutput.appendDouble(isNull ? 0 : cursor.getDouble()); } else if (type == Type.BOOLEAN) { if (writableBytes < SIZE_OF_BYTE) { return false; } positionOffsets.add(sliceOutput.size()); sliceOutput.writeByte(isNull ? 1 : 0); sliceOutput.writeByte(!isNull && cursor.getBoolean() ? 1 : 0); } else if (type == Type.VARIABLE_BINARY) { int sliceLength = isNull ? 0 : getVariableBinaryLength(cursor.getRawSlice(), cursor.getRawOffset()); if (writableBytes < SIZE_OF_INT + sliceLength) { return false; } int startingOffset = sliceOutput.size(); positionOffsets.add(startingOffset); sliceOutput.writeByte(isNull ? 1 : 0); sliceOutput.appendInt(sliceLength + SIZE_OF_BYTE + SIZE_OF_INT); if (!isNull) { sliceOutput.writeBytes( cursor.getRawSlice(), cursor.getRawOffset() + SIZE_OF_BYTE + SIZE_OF_INT, sliceLength); } } else { throw new IllegalArgumentException("Unsupported type " + type); } return true; }
@Override public BlockBuilder writeBytes(Slice source, int sourceIndex, int length) { sliceOutput.writeBytes(source, sourceIndex, length); currentEntrySize += length; return this; }
@Override public void appendTo(Slice slice, int offset, SliceOutput sliceOutput) { // copy full value including length int length = getLength(slice, offset); sliceOutput.writeBytes(slice, offset, length); }
@Override public int writeSlice(SliceOutput sliceOutput, Slice value, int offset, int length) { sliceOutput.writeInt(length); sliceOutput.writeBytes(value, offset, length); return length + SIZE_OF_INT; }