@Override
public void insertSorted(ITupleReference tuple) throws HyracksDataException {
int freeSpace = buf.getInt(ITreeIndexFrame.Constants.FREE_SPACE_OFFSET);
int fieldsToTruncate = 0;
if (buf.getInt(PREFIX_TUPLE_COUNT_OFFSET) > 0) {
framePrefixTuple.resetByTupleIndex(this, buf.getInt(PREFIX_TUPLE_COUNT_OFFSET) - 1);
if (cmp.fieldRangeCompare(tuple, framePrefixTuple, 0, framePrefixTuple.getFieldCount()) == 0) {
fieldsToTruncate = framePrefixTuple.getFieldCount();
}
}
int bytesWritten = tupleWriter.writeTupleFields(tuple, fieldsToTruncate,
tuple.getFieldCount() - fieldsToTruncate, buf.array(), freeSpace);
int prefixSlotNum = FieldPrefixSlotManager.TUPLE_UNCOMPRESSED;
if (fieldsToTruncate > 0) {
prefixSlotNum = buf.getInt(PREFIX_TUPLE_COUNT_OFFSET) - 1;
} else {
buf.putInt(UNCOMPRESSED_TUPLE_COUNT_OFFSET, buf.getInt(UNCOMPRESSED_TUPLE_COUNT_OFFSET) + 1);
}
int insSlot = slotManager.encodeSlotFields(prefixSlotNum, FieldPrefixSlotManager.GREATEST_KEY_INDICATOR);
slotManager.insertSlot(insSlot, freeSpace);
buf.putInt(ITreeIndexFrame.Constants.TUPLE_COUNT_OFFSET,
buf.getInt(ITreeIndexFrame.Constants.TUPLE_COUNT_OFFSET) + 1);
buf.putInt(ITreeIndexFrame.Constants.FREE_SPACE_OFFSET,
buf.getInt(ITreeIndexFrame.Constants.FREE_SPACE_OFFSET) + bytesWritten);
buf.putInt(TOTAL_FREE_SPACE_OFFSET,
buf.getInt(TOTAL_FREE_SPACE_OFFSET) - bytesWritten - slotManager.getSlotSize());
}