Skip to content

Commit

Permalink
TreeNodeDynamicSize impl part 5 - leafUnderflow
Browse files Browse the repository at this point in the history
Use total available space to determine underflow in leaf
  • Loading branch information
burqen committed Jan 16, 2018
1 parent 87e77b2 commit 859b1b6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
Expand Up @@ -1019,7 +1019,7 @@ private boolean removeFromLeaf( PageCursor cursor, StructurePropagation<KEY> str
stableGeneration, unstableGeneration );
keyCount = simplyRemoveFromLeaf( cursor, into, keyCount, pos );

if ( bTreeNode.leafUnderflow( keyCount ) )
if ( bTreeNode.leafUnderflow( cursor, keyCount ) )
{
// Underflow
underflowInLeaf( cursor, structurePropagation, keyCount, stableGeneration, unstableGeneration );
Expand Down
Expand Up @@ -334,7 +334,7 @@ static void goTo( PageCursor cursor, String messageOnError, long nodeId )
*/
abstract void defragmentLeaf( PageCursor cursor );

abstract boolean leafUnderflow( int keyCount );
abstract boolean leafUnderflow( PageCursor cursor, int keyCount );

abstract boolean canRebalanceLeaves( int leftKeyCount, int rightKeyCount );

Expand Down
Expand Up @@ -282,10 +282,8 @@ boolean internalOverflow( int currentKeyCount )
Overflow leafOverflow( PageCursor cursor, int currentKeyCount, KEY newKey, VALUE newValue )
{
// How much space do we have?
int allocOffset = getAllocOffset( cursor );
int deadSpace = getDeadSpace( cursor );
int endOfOffsetArray = keyPosOffsetLeaf( currentKeyCount );
int allocSpace = allocOffset - endOfOffsetArray;
int allocSpace = getAllocSpace( cursor, currentKeyCount );

// How much space do we need?
int keySize = layout.keySize( newKey );
Expand Down Expand Up @@ -402,9 +400,14 @@ void defragmentLeaf( PageCursor cursor )
}

@Override
boolean leafUnderflow( int keyCount )
boolean leafUnderflow( PageCursor cursor, int keyCount )
{
throw new UnsupportedOperationException( "Implement me" );
int halfSpace = halfSpace();
int allocSpace = getAllocSpace( cursor, keyCount );
int deadSpace = getDeadSpace( cursor );
int availableSpace = allocSpace + deadSpace;

return availableSpace > halfSpace;
}

@Override
Expand Down Expand Up @@ -468,6 +471,13 @@ void doSplitLeaf( PageCursor leftCursor, int leftKeyCount, PageCursor rightCurso
TreeNode.setKeyCount( rightCursor, rightKeyCount );
}

private int getAllocSpace( PageCursor cursor, int keyCount )
{
int allocOffset = getAllocOffset( cursor );
int endOfOffsetArray = keyPosOffsetLeaf( keyCount );
return allocOffset - endOfOffsetArray;
}

private void recordDeadAndAlive( PageCursor cursor, PrimitiveIntStack deadKeysOffset, PrimitiveIntStack aliveKeysOffset )
{
int currentOffset = getAllocOffset( cursor );
Expand Down Expand Up @@ -529,7 +539,7 @@ private int transferRawKeyValue( PageCursor fromCursor, int fromPos, PageCursor

private int middle( PageCursor leftCursor, int insertPos, KEY newKey, VALUE newValue )
{
int halfSpace = (pageSize - HEADER_LENGTH_DYNAMIC) / 2;
int halfSpace = halfSpace();
int middle = 0;
int currentPos = 0;
int middleSpace = 0;
Expand Down Expand Up @@ -562,6 +572,11 @@ private int middle( PageCursor leftCursor, int insertPos, KEY newKey, VALUE newV
return middle;
}

private int halfSpace()
{
return (pageSize - HEADER_LENGTH_DYNAMIC) / 2;
}

private int totalSpaceOfKeyValue( KEY key, VALUE value )
{
return bytesKeyOffset() + bytesKeySize() + bytesValueSize() + layout.keySize( key ) + layout.valueSize( value );
Expand Down
Expand Up @@ -273,7 +273,7 @@ void defragmentLeaf( PageCursor cursor )
}

@Override
boolean leafUnderflow( int keyCount )
boolean leafUnderflow( PageCursor cursor, int keyCount )
{
return keyCount < (leafMaxKeyCount() + 1) / 2;
}
Expand Down

0 comments on commit 859b1b6

Please sign in to comment.