Skip to content

Commit

Permalink
TreeNodeDynamicSize impl part 8 - split internal and merge leaf
Browse files Browse the repository at this point in the history
Also, RightmostInChain throw instead of setting cursor exception
because it does not rely on any reads.
  • Loading branch information
burqen committed Jan 16, 2018
1 parent 9ebdf5b commit 0deacb9
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 67 deletions.
Expand Up @@ -459,21 +459,10 @@ private void splitInternal( PageCursor cursor, StructurePropagation<KEY> structu
// Find position to insert new key // Find position to insert new key
int pos = positionOf( search( cursor, INTERNAL, newKey, readKey, keyCount ) ); int pos = positionOf( search( cursor, INTERNAL, newKey, readKey, keyCount ) );


int keyCountAfterInsert = keyCount + 1;
int middlePos = middle( keyCountAfterInsert );

// Update structurePropagation // Update structurePropagation
structurePropagation.hasRightKeyInsert = true; structurePropagation.hasRightKeyInsert = true;
structurePropagation.midChild = current; structurePropagation.midChild = current;
structurePropagation.rightChild = newRight; structurePropagation.rightChild = newRight;
if ( middlePos == pos )
{
layout.copyKey( newKey, structurePropagation.rightKey );
}
else
{
bTreeNode.keyAt( cursor, structurePropagation.rightKey, pos < middlePos ? middlePos - 1 : middlePos, INTERNAL );
}


try ( PageCursor rightCursor = cursor.openLinkedCursor( newRight ) ) try ( PageCursor rightCursor = cursor.openLinkedCursor( newRight ) )
{ {
Expand All @@ -482,11 +471,10 @@ private void splitInternal( PageCursor cursor, StructurePropagation<KEY> structu
bTreeNode.initializeInternal( rightCursor, stableGeneration, unstableGeneration ); bTreeNode.initializeInternal( rightCursor, stableGeneration, unstableGeneration );
TreeNode.setRightSibling( rightCursor, oldRight, stableGeneration, unstableGeneration ); TreeNode.setRightSibling( rightCursor, oldRight, stableGeneration, unstableGeneration );
TreeNode.setLeftSibling( rightCursor, current, stableGeneration, unstableGeneration ); TreeNode.setLeftSibling( rightCursor, current, stableGeneration, unstableGeneration );
int rightKeyCount = keyCountAfterInsert - middlePos - 1; // -1 because don't keep prim key in internal


// Do split // Do split
bTreeNode.doSplitInternal( cursor, keyCount, rightCursor, rightKeyCount, pos, newKey, newRightChild, middlePos, stableGeneration, bTreeNode.doSplitInternal( cursor, keyCount, rightCursor, pos, newKey, newRightChild, stableGeneration, unstableGeneration,
unstableGeneration ); structurePropagation );
} }


// Update old right with new left sibling (newRight) // Update old right with new left sibling (newRight)
Expand Down Expand Up @@ -1068,7 +1056,7 @@ else if ( TreeNode.isNode( rightSibling ) )
rightSiblingCursor.next(); rightSiblingCursor.next();
int rightSiblingKeyCount = TreeNode.keyCount( rightSiblingCursor ); int rightSiblingKeyCount = TreeNode.keyCount( rightSiblingCursor );


if ( bTreeNode.canMergeLeaves( keyCount, rightSiblingKeyCount ) ) if ( bTreeNode.canMergeLeaves( cursor, keyCount, rightSiblingCursor, rightSiblingKeyCount ) )
{ {
createSuccessorIfNeeded( rightSiblingCursor, structurePropagation, UPDATE_RIGHT_CHILD, createSuccessorIfNeeded( rightSiblingCursor, structurePropagation, UPDATE_RIGHT_CHILD,
stableGeneration, unstableGeneration ); stableGeneration, unstableGeneration );
Expand Down
Expand Up @@ -68,8 +68,9 @@ void assertNext( PageCursor cursor, long newRightmostNodeGeneration,
String errorMessage = errorMessageBuilder.toString(); String errorMessage = errorMessageBuilder.toString();
if ( !errorMessage.equals( "" ) ) if ( !errorMessage.equals( "" ) )
{ {
setPatternException( cursor, newRightmostNodeGeneration, newRightmostLeftSiblingPointer, errorMessage = addPatternToExceptionMessage( newRightmostNodeGeneration, newRightmostLeftSiblingPointer,
newRightmostLeftSiblingPointerGeneration, newRightmostNode, errorMessage ); newRightmostLeftSiblingPointerGeneration, newRightmostNode, errorMessage );
throw new IllegalStateException( errorMessage );
} }


// Update currentRightmostNode = newRightmostNode; // Update currentRightmostNode = newRightmostNode;
Expand All @@ -79,16 +80,16 @@ void assertNext( PageCursor cursor, long newRightmostNodeGeneration,
currentRightmostRightSiblingPointerGeneration = newRightmostRightSiblingPointerGeneration; currentRightmostRightSiblingPointerGeneration = newRightmostRightSiblingPointerGeneration;
} }


private void setPatternException( PageCursor cursor, long newRightmostGeneration, long leftSibling, private String addPatternToExceptionMessage( long newRightmostGeneration, long leftSibling,
long leftSiblingGeneration, long newRightmost, String errorMessage ) long leftSiblingGeneration, long newRightmost, String errorMessage )
{ {
cursor.setCursorException( format( "%s" + return format( "%s" +
" Left siblings view: %s%n" + " Left siblings view: %s%n" +
" Right siblings view: %s%n", errorMessage, " Right siblings view: %s%n", errorMessage,
leftPattern( currentRightmostNode, currentRightmostNodeGeneration, leftPattern( currentRightmostNode, currentRightmostNodeGeneration,
currentRightmostRightSiblingPointerGeneration, currentRightmostRightSiblingPointerGeneration,
currentRightmostRightSiblingPointer ), currentRightmostRightSiblingPointer ),
rightPattern( newRightmost, newRightmostGeneration, leftSiblingGeneration, leftSibling ) ) ); rightPattern( newRightmost, newRightmostGeneration, leftSiblingGeneration, leftSibling ) );
} }


private String leftPattern( long actualLeftSibling, long actualLeftSiblingGeneration, private String leftPattern( long actualLeftSibling, long actualLeftSiblingGeneration,
Expand Down
Expand Up @@ -342,7 +342,7 @@ static void goTo( PageCursor cursor, String messageOnError, long nodeId )
*/ */
abstract int canRebalanceLeaves( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int rightKeyCount ); abstract int canRebalanceLeaves( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int rightKeyCount );


abstract boolean canMergeLeaves( int leftKeyCount, int rightKeyCount ); abstract boolean canMergeLeaves( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int rightKeyCount );


/** /**
* Calculate where split should be done and move entries between leaves participating in split. * Calculate where split should be done and move entries between leaves participating in split.
Expand All @@ -361,9 +361,9 @@ abstract void doSplitLeaf( PageCursor leftCursor, int leftKeyCount, PageCursor r
* *
* Key count is updated. * Key count is updated.
*/ */
abstract void doSplitInternal( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int rightKeyCount, int insertPos, abstract void doSplitInternal( PageCursor leftCursor, int leftKeyCount, PageCursor rightCursor, int insertPos,
KEY newKey, KEY newKey,
long newRightChild, int middlePos, long stableGeneration, long unstableGeneration ); long newRightChild, long stableGeneration, long unstableGeneration, StructurePropagation<KEY> structurePropagation );


/** /**
* Move all rightmost keys and values in left leaf from given position to right node. * Move all rightmost keys and values in left leaf from given position to right node.
Expand Down

0 comments on commit 0deacb9

Please sign in to comment.