Skip to content

Commit

Permalink
totalSpace and halfSpace as fields in TreeNodeDynamicSize
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Jan 16, 2018
1 parent ce1d8ec commit 96b6ae3
Showing 1 changed file with 13 additions and 20 deletions.
Expand Up @@ -88,12 +88,15 @@ public class TreeNodeDynamicSize<KEY, VALUE> extends TreeNode<KEY,VALUE>
private final int maxKeyCount = pageSize / bytesKeyOffset() + bytesKeySize() + bytesValueSize();
private final int[] oldOffset = new int[maxKeyCount];
private final int[] newOffset = new int[maxKeyCount];
private int totalSpace;
private int halfSpace;

TreeNodeDynamicSize( int pageSize, Layout<KEY,VALUE> layout )
{
super( pageSize, layout );

keyValueSizeCap = totalSpace( pageSize ) / LEAST_NUMBER_OF_ENTRIES_PER_PAGE - SIZE_TOTAL_OVERHEAD;
totalSpace = pageSize - HEADER_LENGTH_DYNAMIC;
halfSpace = totalSpace / 2;
keyValueSizeCap = totalSpace / LEAST_NUMBER_OF_ENTRIES_PER_PAGE - SIZE_TOTAL_OVERHEAD;

if ( keyValueSizeCap < MINIMUM_ENTRY_SIZE_CAP )
{
Expand Down Expand Up @@ -343,7 +346,7 @@ void setChildAt( PageCursor cursor, long child, int pos, long stableGeneration,
@Override
boolean reasonableKeyCount( int keyCount )
{
return keyCount >= 0 && keyCount <= totalSpace( pageSize ) / SIZE_TOTAL_OVERHEAD;
return keyCount >= 0 && keyCount <= totalSpace / SIZE_TOTAL_OVERHEAD;
}

@Override
Expand Down Expand Up @@ -542,7 +545,7 @@ For each dead space of size X (can be multiple consecutive dead keys)
@Override
boolean leafUnderflow( PageCursor cursor, int keyCount )
{
int halfSpace = halfSpace();
int halfSpace = this.halfSpace;
int allocSpace = getAllocSpace( cursor, keyCount, LEAF );
int deadSpace = getDeadSpace( cursor );
int availableSpace = allocSpace + deadSpace;
Expand All @@ -556,7 +559,7 @@ int canRebalanceLeaves( PageCursor leftCursor, int leftKeyCount, PageCursor righ
int leftActiveSpace = totalActiveSpace( leftCursor, leftKeyCount );
int rightActiveSpace = totalActiveSpace( rightCursor, rightKeyCount );

if ( leftActiveSpace + rightActiveSpace < totalSpace( pageSize ) )
if ( leftActiveSpace + rightActiveSpace < totalSpace )
{
// We can merge
return -1;
Expand Down Expand Up @@ -586,7 +589,7 @@ int canRebalanceLeaves( PageCursor leftCursor, int leftKeyCount, PageCursor righ
leftActiveSpace += lastChunkSize;
rightActiveSpace -= lastChunkSize;

int halfSpace = halfSpace();
int halfSpace = this.halfSpace;
boolean canRebalance = leftActiveSpace > halfSpace && rightActiveSpace > halfSpace;
return canRebalance ? keysToMove : 0;
}
Expand All @@ -596,7 +599,7 @@ boolean canMergeLeaves( PageCursor leftCursor, int leftKeyCount, PageCursor righ
{
int leftActiveSpace = totalActiveSpace( leftCursor, leftKeyCount );
int rightActiveSpace = totalActiveSpace( rightCursor, rightKeyCount );
int totalSpace = totalSpace( pageSize );
int totalSpace = this.totalSpace;
return totalSpace >= leftActiveSpace + rightActiveSpace;
}

Expand Down Expand Up @@ -945,7 +948,7 @@ private int transferRawKey( PageCursor fromCursor, int fromPos, PageCursor toCur

private int middleInternal( PageCursor cursor, int insertPos, KEY newKey )
{
int halfSpace = halfSpace();
int halfSpace = this.halfSpace;
int middle = 0;
int currentPos = 0;
int middleSpace = childSize(); // Leftmost child will always be included in left side
Expand Down Expand Up @@ -980,7 +983,7 @@ private int middleInternal( PageCursor cursor, int insertPos, KEY newKey )

private int middleLeaf( PageCursor cursor, int insertPos, KEY newKey, VALUE newValue )
{
int halfSpace = halfSpace();
int halfSpace = this.halfSpace;
int middle = 0;
int currentPos = 0;
int middleSpace = 0;
Expand Down Expand Up @@ -1017,17 +1020,7 @@ private int totalActiveSpace( PageCursor cursor, int keyCount )
{
int deadSpace = getDeadSpace( cursor );
int allocSpace = getAllocSpace( cursor, keyCount, LEAF );
return totalSpace( pageSize ) - deadSpace - allocSpace;
}

private int totalSpace( int pageSize )
{
return pageSize - HEADER_LENGTH_DYNAMIC;
}

private int halfSpace()
{
return totalSpace( pageSize ) / 2;
return totalSpace - deadSpace - allocSpace;
}

int totalSpaceOfKeyValue( KEY key, VALUE value )
Expand Down

0 comments on commit 96b6ae3

Please sign in to comment.