Block leftBlock = _store.readBlock(leftBlockId);
try {
byte []leftBuffer = leftBlock.getBuffer();
Lock leftLock = leftBlock.getWriteLock();
leftLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
int leftLength = getLength(leftBuffer);
Lock blockLock = block.getWriteLock();
blockLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
if (_minN < leftLength) {
validateEqualLeaf(buffer, leftBuffer, block, leftBlock);
parent.setFlushDirtyOnCommit(false);
leftBlock.setFlushDirtyOnCommit(false);
validate(parentBlockId, parentBuffer);
validate(leftBlockId, leftBuffer);
validate(blockId, buffer);
// System.out.println("MOVE_FROM_LEFT: " + debugId(blockId) + " from " + debugId(leftBlockId));
moveFromLeft(parentBuffer, leftBuffer, buffer, blockId);
validate(parentBlockId, parentBuffer);
validate(leftBlockId, leftBuffer);
validate(blockId, buffer);
parent.setDirty(0, BlockStore.BLOCK_SIZE);
leftBlock.setDirty(0, BlockStore.BLOCK_SIZE);
return false;
}
} finally {
blockLock.unlock();
}
} finally {
leftLock.unlock();
}
} finally {
leftBlock.free();
}
}
// If the right block has extra data, shift the first right item
// to the block
if (rightBlockId > 0) {
Block rightBlock = _store.readBlock(rightBlockId);
try {
byte []rightBuffer = rightBlock.getBuffer();
Lock blockLock = block.getWriteLock();
blockLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
Lock rightLock = rightBlock.getWriteLock();
rightLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
int rightLength = getLength(rightBuffer);
if (_minN < rightLength) {
validateEqualLeaf(buffer, rightBuffer, block, rightBlock);
parent.setFlushDirtyOnCommit(false);
rightBlock.setFlushDirtyOnCommit(false);
// System.out.println("MOVE_FROM_RIGHT: " + debugId(blockId) + " from " + debugId(rightBlockId));
moveFromRight(parentBuffer, buffer, rightBuffer, blockId);
validate(parentBlockId, parentBuffer);
validate(blockId, buffer);
validate(rightBlockId, rightBuffer);
parent.setDirty(0, BlockStore.BLOCK_SIZE);
rightBlock.setDirty(0, BlockStore.BLOCK_SIZE);
return false;
}
} finally {
rightLock.unlock();
}
} finally {
blockLock.unlock();
}
} finally {
rightBlock.free();
}
}
if (parentLength < 2)
return false;
// If the left block has space, merge with it
if (leftBlockId > 0) {
Block leftBlock = _store.readBlock(leftBlockId);
try {
byte []leftBuffer = leftBlock.getBuffer();
Lock leftLock = leftBlock.getWriteLock();
leftLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
int leftLength = getLength(leftBuffer);
Lock blockLock = block.getWriteLock();
blockLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
int length = getLength(buffer);
if (length + leftLength <= _n) {
validateEqualLeaf(leftBuffer, buffer, leftBlock, block);
parent.setFlushDirtyOnCommit(false);
leftBlock.setFlushDirtyOnCommit(false);
// System.out.println("MERGE_LEFT: " + debugId(blockId) + " from " + debugId(leftBlockId));
mergeLeft(parentBuffer,
leftBuffer, leftBlockId,
buffer, blockId);
validate(parentBlockId, parentBuffer);
validate(leftBlockId, leftBuffer);
parent.setDirty(0, BlockStore.BLOCK_SIZE);
leftBlock.setDirty(0, BlockStore.BLOCK_SIZE);
// System.out.println("FREE-ML: " + block);
return true;
}
} finally {
blockLock.unlock();
}
} finally {
leftLock.unlock();
}
} finally {
leftBlock.free();
}
}
// If the right block has space, merge with it
if (rightBlockId > 0) {
Block rightBlock = _store.readBlock(rightBlockId);
try {
byte []rightBuffer = rightBlock.getBuffer();
Lock blockLock = block.getWriteLock();
blockLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
Lock rightLock = rightBlock.getWriteLock();
rightLock.tryLock(_timeout, TimeUnit.MILLISECONDS);
try {
int length = getLength(buffer);
int rightLength = getLength(rightBuffer);
if (length + rightLength <= _n) {
validateEqualLeaf(rightBuffer, buffer, rightBlock, block);
rightBlock.setFlushDirtyOnCommit(false);
parent.setFlushDirtyOnCommit(false);
// System.out.println("MERGE_RIGHT: " + debugId(blockId) + " from " + debugId(rightBlockId));
validate(blockId, buffer);
validate(parentBlockId, parentBuffer);
validate(rightBlockId, rightBuffer);
mergeRight(parentBuffer, buffer, rightBuffer, blockId);
validate(parentBlockId, parentBuffer);
validate(rightBlockId, rightBuffer);
rightBlock.setDirty(0, BlockStore.BLOCK_SIZE);
parent.setDirty(0, BlockStore.BLOCK_SIZE);
// System.out.println("FREE-MR: " + block);
return true;
}
} finally {
rightLock.unlock();
}
} finally {
blockLock.unlock();
}
} finally {