in mavibot/src/main/java/org/apache/directory/mavibot/btree/PersistedLeaf.java [331:398]
private DeleteResult<K, V> mergeWithSibling( Tuple<K, V> removedElement, long revision,
PersistedLeaf<K, V> sibling,
boolean isLeft, int pos )
throws EndOfFileExceededException, IOException
{
boolean isNotSubTree = ( btree.getType() != PERSISTED_SUB );
// Create the new page. It will contain N - 1 elements (the maximum number)
// as we merge two pages that contain N/2 elements minus the one we remove
PersistedLeaf<K, V> newLeaf = new PersistedLeaf<K, V>( btree, revision, btree.getPageSize() - 1 );
if ( isLeft )
{
// The sibling is on the left
// Copy all the elements from the sibling first
System.arraycopy( sibling.keys, 0, newLeaf.keys, 0, sibling.nbElems );
if ( isNotSubTree )
{
System.arraycopy( sibling.values, 0, newLeaf.values, 0, sibling.nbElems );
}
// Copy all the elements from the page up to the deletion position
System.arraycopy( keys, 0, newLeaf.keys, sibling.nbElems, pos );
if ( isNotSubTree )
{
System.arraycopy( values, 0, newLeaf.values, sibling.nbElems, pos );
}
// And copy the remaining elements after the deletion point
System.arraycopy( keys, pos + 1, newLeaf.keys, sibling.nbElems + pos, nbElems - pos - 1 );
if ( isNotSubTree )
{
System.arraycopy( values, pos + 1, newLeaf.values, sibling.nbElems + pos, nbElems - pos - 1 );
}
}
else
{
// The sibling is on the right
// Copy all the elements from the page up to the deletion position
System.arraycopy( keys, 0, newLeaf.keys, 0, pos );
if ( isNotSubTree )
{
System.arraycopy( values, 0, newLeaf.values, 0, pos );
}
// Then copy the remaining elements after the deletion point
System.arraycopy( keys, pos + 1, newLeaf.keys, pos, nbElems - pos - 1 );
if ( isNotSubTree )
{
System.arraycopy( values, pos + 1, newLeaf.values, pos, nbElems - pos - 1 );
}
// And copy all the elements from the sibling
System.arraycopy( sibling.keys, 0, newLeaf.keys, nbElems - 1, sibling.nbElems );
if ( isNotSubTree )
{
System.arraycopy( sibling.values, 0, newLeaf.values, nbElems - 1, sibling.nbElems );
}
}
// And create the result
DeleteResult<K, V> result = new MergedWithSiblingResult<K, V>( newLeaf, removedElement );
result.addCopiedPage( this );
result.addCopiedPage( sibling );
return result;
}