{
long parentId = parentBlock.getBlockId();
log.finest("btree splitting root " + (parentId / BLOCK_SIZE));
Block leftBlock = null;
Block rightBlock = null;
try {
byte []parentBuffer = parentBlock.getBuffer();
int length = getLength(parentBuffer);
if (length == 1)
return;
parentBlock.setFlushDirtyOnCommit(false);
int parentFlags = getInt(parentBuffer, FLAGS_OFFSET);
leftBlock = _store.allocateIndexBlock();
// System.out.println("TREE-alloc2:" + Long.toHexString(leftBlock.getBlockId()));
leftBlock.setFlushDirtyOnCommit(false);
long leftBlockId = leftBlock.getBlockId();
rightBlock = _store.allocateIndexBlock();
// System.out.println("TREE-alloc3:" + Long.toHexString(rightBlock.getBlockId()));
rightBlock.setFlushDirtyOnCommit(false);
long rightBlockId = rightBlock.getBlockId();
int pivot = (length - 1) / 2;
//System.out.println("INDEX SPLIT ROOT: " + (parentId / BLOCK_SIZE)
// + " PIVOT=" + pivot);
if (length <= 2 || _n < length || pivot < 1 || length <= pivot)
throw new IllegalStateException(Long.toHexString(parentBlock.getBlockId()) + ": " + length + " is an illegal length, or pivot " + pivot + " is bad, with n=" + _n);
int pivotOffset = HEADER_SIZE + pivot * _tupleSize;
long pivotValue = getPointer(parentBuffer, pivotOffset);
byte []leftBuffer = leftBlock.getBuffer();
System.arraycopy(parentBuffer, HEADER_SIZE,
leftBuffer, HEADER_SIZE,
pivotOffset + _tupleSize - HEADER_SIZE);
setInt(leftBuffer, FLAGS_OFFSET, parentFlags);
setLength(leftBuffer, pivot + 1);
setPointer(leftBuffer, PARENT_OFFSET, parentId);
setPointer(leftBuffer, NEXT_OFFSET, 0); // rightBlockId);
byte []rightBuffer = rightBlock.getBuffer();
if (length - pivot - 1 < 0)
throw new IllegalStateException("illegal length " + pivot + " " + length);
System.arraycopy(parentBuffer, pivotOffset + _tupleSize,
rightBuffer, HEADER_SIZE,
(length - pivot - 1) * _tupleSize);
setInt(rightBuffer, FLAGS_OFFSET, parentFlags);
setLength(rightBuffer, length - pivot - 1);
setPointer(rightBuffer, PARENT_OFFSET, parentId);
setPointer(rightBuffer, NEXT_OFFSET,
getPointer(parentBuffer, NEXT_OFFSET));
System.arraycopy(parentBuffer, pivotOffset,
parentBuffer, HEADER_SIZE,
_tupleSize);
setPointer(parentBuffer, HEADER_SIZE, leftBlockId);
setLeaf(parentBuffer, false);
setLength(parentBuffer, 1);
setPointer(parentBuffer, NEXT_OFFSET, rightBlockId);
parentBlock.setDirty(0, BlockStore.BLOCK_SIZE);
leftBlock.setDirty(0, BlockStore.BLOCK_SIZE);
rightBlock.setDirty(0, BlockStore.BLOCK_SIZE);
validate(parentBlock);
validate(leftBlock);
validate(rightBlock);
} finally {
if (leftBlock != null)
leftBlock.free();
if (rightBlock != null)
rightBlock.free();
}
}