@Override public void initData(int keySize) { // try to reuse existing memory from a lower-level split if possible this.keySize = keySize; if (data != null) { if (data.length < keySize + 8) { data = new byte[keySize + 8]; // add 8 for left and right page buf = ByteBuffer.wrap(data); } } else { data = new byte[keySize + 8]; // add 8 for left and right page buf = ByteBuffer.wrap(data); } tuple.resetByTupleOffset(buf.array(), 0); }
@Override public ITupleReference getMinTuple() { minTuple.resetByTupleOffset(min.getByteArray(), 0); return minTuple; }
@Override public ITupleReference getMaxTuple() { maxTuple.resetByTupleOffset(max.getByteArray(), 0); return maxTuple; }
@Override public void initData(int keySize) { // try to reuse existing memory from a lower-level split if possible this.keySize = keySize; if (leftPageData != null) { if (leftPageData.length < keySize + 4) { leftPageData = new byte[keySize + 4]; // add 4 for the page leftPageBuf = ByteBuffer.wrap(leftPageData); } } else { leftPageData = new byte[keySize + 4]; // add 4 for the page leftPageBuf = ByteBuffer.wrap(leftPageData); } if (rightPageData != null) { if (rightPageData.length < keySize + 4) { rightPageData = new byte[keySize + 4]; // add 4 for the page rightPageBuf = ByteBuffer.wrap(rightPageData); } } else { rightPageData = new byte[keySize + 4]; // add 4 for the page rightPageBuf = ByteBuffer.wrap(rightPageData); } leftTuple.resetByTupleOffset(leftPageBuf.array(), 0); rightTuple.resetByTupleOffset(rightPageBuf.array(), 0); }
@Override public void deleteGreatest() { int slotOff = slotManager.getSlotEndOff(); int tupleOff = slotManager.getTupleOff(slotOff); frameTuple.resetByTupleOffset(buf.array(), tupleOff); int keySize = tupleWriter.bytesRequired(frameTuple); System.arraycopy(buf.array(), tupleOff + keySize, buf.array(), RIGHT_LEAF_OFFSET, CHILD_PTR_SIZE); // Maintain space information. buf.putInt(Constants.TUPLE_COUNT_OFFSET, buf.getInt(Constants.TUPLE_COUNT_OFFSET) - 1); buf.putInt(TOTAL_FREE_SPACE_OFFSET, buf.getInt(TOTAL_FREE_SPACE_OFFSET) + keySize + CHILD_PTR_SIZE + slotManager.getSlotSize()); int freeSpace = buf.getInt(Constants.FREE_SPACE_OFFSET); if (freeSpace == tupleOff + keySize + CHILD_PTR_SIZE) { buf.putInt(freeSpace, freeSpace - (keySize + CHILD_PTR_SIZE)); } }
@Override public int getLeftmostChildPageId() { int tupleOff = slotManager.getTupleOff(slotManager.getSlotStartOff()); frameTuple.resetByTupleOffset(buf.array(), tupleOff); int childPageOff = getLeftChildPageOff(frameTuple); return buf.getInt(childPageOff); }
public ISplitKey duplicate(ITreeIndexTupleReference copyLeftTuple, ITreeIndexTupleReference copyRightTuple) { RTreeSplitKey copy = new RTreeSplitKey(copyLeftTuple, copyRightTuple); copy.leftPageData = leftPageData.clone(); copy.leftPageBuf = ByteBuffer.wrap(copy.leftPageData); copy.leftTuple.setFieldCount(leftTuple.getFieldCount()); copy.leftTuple.resetByTupleOffset(copy.leftPageBuf.array(), 0); copy.rightPageData = rightPageData.clone(); copy.rightPageBuf = ByteBuffer.wrap(copy.rightPageData); copy.rightTuple.setFieldCount(rightTuple.getFieldCount()); copy.rightTuple.resetByTupleOffset(copy.rightPageBuf.array(), 0); return copy; }
@Override public void delete(ITupleReference tuple, int tupleIndex) { int slotOff = slotManager.getSlotOff(tupleIndex); int tupleOff = slotManager.getTupleOff(slotOff); frameTuple.resetByTupleOffset(buf.array(), tupleOff); int tupleSize = tupleWriter.bytesRequired(frameTuple); // perform deletion (we just do a memcpy to overwrite the slot) int slotStartOff = slotManager.getSlotEndOff(); int length = slotOff - slotStartOff; System.arraycopy(buf.array(), slotStartOff, buf.array(), slotStartOff + slotManager.getSlotSize(), length); // maintain space information buf.putInt(Constants.TUPLE_COUNT_OFFSET, buf.getInt(Constants.TUPLE_COUNT_OFFSET) - 1); buf.putInt(TOTAL_FREE_SPACE_OFFSET, buf.getInt(TOTAL_FREE_SPACE_OFFSET) + tupleSize + slotManager.getSlotSize()); }
@Override public void delete(int tupleIndex, MultiComparator cmp) { int slotOff = slotManager.getSlotOff(tupleIndex); int tupleOff = slotManager.getTupleOff(slotOff); frameTuple.resetByTupleOffset(buf.array(), tupleOff); int tupleSize = tupleWriter.bytesRequired(frameTuple); // perform deletion (we just do a memcpy to overwrite the slot) int slotStartOff = slotManager.getSlotEndOff(); int length = slotOff - slotStartOff; System.arraycopy(buf.array(), slotStartOff, buf.array(), slotStartOff + slotManager.getSlotSize(), length); // maintain space information buf.putInt(Constants.TUPLE_COUNT_OFFSET, buf.getInt(Constants.TUPLE_COUNT_OFFSET) - 1); buf.putInt(TOTAL_FREE_SPACE_OFFSET, buf.getInt(TOTAL_FREE_SPACE_OFFSET) + tupleSize + slotManager.getSlotSize()); }
@Override public BTreeSplitKey duplicate(ITreeIndexTupleReference copyTuple) { BTreeSplitKey copy = new BTreeSplitKey(copyTuple); copy.data = data.clone(); copy.buf = ByteBuffer.wrap(copy.data); copy.tuple.setFieldCount(tuple.getFieldCount()); copy.tuple.resetByTupleOffset(copy.buf.array(), 0); return copy; } }
@Override public void delete(int tupleIndex, MultiComparator cmp) { frameTuple.setFieldCount(cmp.getKeyFieldCount()); int slotOff = slotManager.getSlotOff(tupleIndex); int tupleOff = slotManager.getTupleOff(slotOff); frameTuple.resetByTupleOffset(buf.array(), tupleOff); int tupleSize = tupleWriter.bytesRequired(frameTuple); // perform deletion (we just do a memcpy to overwrite the slot) int slotStartOff = slotManager.getSlotEndOff(); int length = slotOff - slotStartOff; System.arraycopy(buf.array(), slotStartOff, buf.array(), slotStartOff + slotManager.getSlotSize(), length); // maintain space information buf.putInt(Constants.TUPLE_COUNT_OFFSET, buf.getInt(Constants.TUPLE_COUNT_OFFSET) - 1); buf.putInt(TOTAL_FREE_SPACE_OFFSET, buf.getInt(TOTAL_FREE_SPACE_OFFSET) + tupleSize + childPtrSize + slotManager.getSlotSize()); }
@Override public void delete(ITupleReference tuple, int tupleIndex) { int slotOff = slotManager.getSlotOff(tupleIndex); int tupleOff; int keySize; if (tupleIndex == slotManager.getGreatestKeyIndicator()) { tupleOff = slotManager.getTupleOff(slotManager.getSlotEndOff()); frameTuple.resetByTupleOffset(buf.array(), tupleOff); keySize = frameTuple.getTupleSize(); // Copy new rightmost pointer. System.arraycopy(buf.array(), tupleOff + keySize, buf.array(), RIGHT_LEAF_OFFSET, CHILD_PTR_SIZE); } else { tupleOff = slotManager.getTupleOff(slotOff); frameTuple.resetByTupleOffset(buf.array(), tupleOff); keySize = frameTuple.getTupleSize(); // Perform deletion (we just do a memcpy to overwrite the slot). int slotStartOff = slotManager.getSlotEndOff(); int length = slotOff - slotStartOff; System.arraycopy(buf.array(), slotStartOff, buf.array(), slotStartOff + slotManager.getSlotSize(), length); } // Maintain space information. buf.putInt(Constants.TUPLE_COUNT_OFFSET, buf.getInt(Constants.TUPLE_COUNT_OFFSET) - 1); buf.putInt(TOTAL_FREE_SPACE_OFFSET, buf.getInt(TOTAL_FREE_SPACE_OFFSET) + keySize + CHILD_PTR_SIZE + slotManager.getSlotSize()); }
@Override public boolean compact() { resetSpaceParams(); int tupleCount = buf.getInt(Constants.TUPLE_COUNT_OFFSET); int freeSpace = buf.getInt(Constants.FREE_SPACE_OFFSET); ArrayList<SlotOffTupleOff> sortedTupleOffs = new ArrayList<>(); sortedTupleOffs.ensureCapacity(tupleCount); for (int i = 0; i < tupleCount; i++) { int slotOff = slotManager.getSlotOff(i); int tupleOff = slotManager.getTupleOff(slotOff); sortedTupleOffs.add(new SlotOffTupleOff(i, slotOff, tupleOff)); } Collections.sort(sortedTupleOffs); for (int i = 0; i < sortedTupleOffs.size(); i++) { int tupleOff = sortedTupleOffs.get(i).tupleOff; frameTuple.resetByTupleOffset(buf.array(), tupleOff); int tupleEndOff = frameTuple.getFieldStart(frameTuple.getFieldCount() - 1) + frameTuple.getFieldLength(frameTuple.getFieldCount() - 1); int tupleLength = tupleEndOff - tupleOff + childPtrSize; System.arraycopy(buf.array(), tupleOff, buf.array(), freeSpace, tupleLength); slotManager.setSlot(sortedTupleOffs.get(i).slotOff, freeSpace); freeSpace += tupleLength; } buf.putInt(Constants.FREE_SPACE_OFFSET, freeSpace); buf.putInt(TOTAL_FREE_SPACE_OFFSET, buf.capacity() - freeSpace - tupleCount * slotManager.getSlotSize()); return false; }
frameTuple.resetByTupleOffset(buf.array(), tupleOff); int tupleEndOff = frameTuple.getFieldStart(frameTuple.getFieldCount() - 1) + frameTuple.getFieldLength(frameTuple.getFieldCount() - 1);
public ArrayList<Integer> getChildren(MultiComparator cmp) { ArrayList<Integer> ret = new ArrayList<>(); frameTuple.setFieldCount(cmp.getKeyFieldCount()); int tupleCount = buf.getInt(Constants.TUPLE_COUNT_OFFSET); for (int i = 0; i < tupleCount; i++) { int tupleOff = slotManager.getTupleOff(slotManager.getSlotOff(i)); frameTuple.resetByTupleOffset(buf.array(), tupleOff); int intVal = IntegerPointable.getInteger(buf.array(), frameTuple.getFieldStart(frameTuple.getFieldCount() - 1) + frameTuple.getFieldLength(frameTuple.getFieldCount() - 1)); ret.add(intVal); } return ret; }
public ArrayList<Integer> getChildren(MultiComparator cmp) { ArrayList<Integer> ret = new ArrayList<>(); frameTuple.setFieldCount(cmp.getKeyFieldCount()); int tupleCount = buf.getInt(Constants.TUPLE_COUNT_OFFSET); for (int i = 0; i < tupleCount; i++) { int tupleOff = slotManager.getTupleOff(slotManager.getSlotOff(i)); frameTuple.resetByTupleOffset(buf.array(), tupleOff); int intVal = IntegerPointable.getInteger(buf.array(), frameTuple.getFieldStart(frameTuple.getFieldCount() - 1) + frameTuple.getFieldLength(frameTuple.getFieldCount() - 1)); ret.add(intVal); } if (!isLeaf()) { int rightLeaf = buf.getInt(RIGHT_LEAF_OFFSET); if (rightLeaf > 0) { ret.add(buf.getInt(RIGHT_LEAF_OFFSET)); } } return ret; }
@Override public void insert(ITupleReference tuple, int tupleIndex) { int slotOff = slotManager.insertSlot(tupleIndex, buf.getInt(Constants.FREE_SPACE_OFFSET)); int freeSpace = buf.getInt(Constants.FREE_SPACE_OFFSET); int bytesWritten = tupleWriter.writeTupleFields(tuple, 0, tuple.getFieldCount(), buf.array(), freeSpace); System.arraycopy(tuple.getFieldData(tuple.getFieldCount() - 1), getLeftChildPageOff(tuple), buf.array(), freeSpace + bytesWritten, CHILD_PTR_SIZE); int tupleSize = bytesWritten + CHILD_PTR_SIZE; buf.putInt(Constants.TUPLE_COUNT_OFFSET, buf.getInt(Constants.TUPLE_COUNT_OFFSET) + 1); buf.putInt(Constants.FREE_SPACE_OFFSET, buf.getInt(Constants.FREE_SPACE_OFFSET) + tupleSize); buf.putInt(TOTAL_FREE_SPACE_OFFSET, buf.getInt(TOTAL_FREE_SPACE_OFFSET) - tupleSize - slotManager.getSlotSize()); // Did we insert into the rightmost slot? if (slotOff == slotManager.getSlotEndOff()) { System.arraycopy(tuple.getFieldData(tuple.getFieldCount() - 1), getLeftChildPageOff(tuple) + CHILD_PTR_SIZE, buf.array(), RIGHT_LEAF_OFFSET, CHILD_PTR_SIZE); } else { // If slotOff has a right (slot-)neighbor then update its child pointer. // The only time when this is NOT the case, is when this is the very first tuple // (or when the splitkey goes into the rightmost slot but that case is handled in the if above). if (buf.getInt(Constants.TUPLE_COUNT_OFFSET) > 1) { int rightNeighborOff = slotOff - slotManager.getSlotSize(); frameTuple.resetByTupleOffset(buf.array(), slotManager.getTupleOff(rightNeighborOff)); System.arraycopy(tuple.getFieldData(0), getLeftChildPageOff(tuple) + CHILD_PTR_SIZE, buf.array(), getLeftChildPageOff(frameTuple), CHILD_PTR_SIZE); } } }
@Override public boolean doHasNext() throws HyracksDataException { if (numTuples <= currentTupleIndex) { return false; } // We don't latch pages since this code is only used by flush () before // bulk-loading the r-tree to disk and flush is not concurrent. // ICachedPage node1 = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, tPointers[currentTupleIndex * 2]), false); try { leafFrame1.setPage(node1); frameTuple1.resetByTupleOffset(leafFrame1.getBuffer().array(), tPointers[currentTupleIndex * 2 + 1]); } finally { bufferCache.unpin(node1); } return true; }
private int compare(int[] tPointers, int tp1, int tp2i, int tp2j) throws HyracksDataException { int i1 = tPointers[tp1 * 2]; int j1 = tPointers[tp1 * 2 + 1]; int i2 = tp2i; int j2 = tp2j; ICachedPage node1 = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, i1), false); try { leafFrame1.setPage(node1); ICachedPage node2 = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, i2), false); try { leafFrame2.setPage(node2); frameTuple1.resetByTupleOffset(leafFrame1.getBuffer().array(), j1); frameTuple2.resetByTupleOffset(leafFrame2.getBuffer().array(), j2); return cmp.selectiveFieldCompare(frameTuple1, frameTuple2, comparatorFields); } finally { bufferCache.unpin(node2); } } finally { bufferCache.unpin(node1); } }
tupleWriter.writeTupleFields(frontier.lastTuple, 0, cmp.getKeyFieldCount(), splitKey.getBuffer().array(), 0); splitKey.getTuple().resetByTupleOffset(splitKey.getBuffer().array(), 0);