Skip to content

Commit

Permalink
In TreeNodeFixedSize, gather all private methods + more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Jan 16, 2018
1 parent 52ead5b commit 19e44ad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 60 deletions.
Expand Up @@ -343,7 +343,7 @@ public void startupState( boolean clean )
* {@link Consumer} to hand out to others who want to decorate information about this tree
* to exceptions thrown out from its surface.
*/
private final Consumer<Throwable> exceptionDecorator = t -> appendTreeInformation( t );
private final Consumer<Throwable> exceptionDecorator = this::appendTreeInformation;

/**
* Opens an index {@code indexFile} in the {@code pageCache}, creating and initializing it if it doesn't exist.
Expand Down
Expand Up @@ -24,7 +24,6 @@
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.read;
import static org.neo4j.index.internal.gbptree.Layout.FIXED_SIZE_KEY;
import static org.neo4j.index.internal.gbptree.Layout.FIXED_SIZE_VALUE;
import static org.neo4j.index.internal.gbptree.TreeNode.Type.INTERNAL;

class TreeNodeFixedSize<KEY,VALUE> extends TreeNode<KEY,VALUE>
{
Expand Down Expand Up @@ -73,13 +72,6 @@ KEY keyAt( PageCursor cursor, KEY into, int pos, Type type )
return into;
}

private void insertKeyAt( PageCursor cursor, KEY key, int pos, int keyCount )
{
insertKeySlotsAt( cursor, pos, 1, keyCount );
cursor.setOffset( keyOffset( pos ) );
layout.writeKey( cursor, key );
}

@Override
void insertKeyAndRightChildAt( PageCursor cursor, KEY key, long child, int pos, int keyCount, long stableGeneration,
long unstableGeneration )
Expand All @@ -95,29 +87,24 @@ void insertKeyValueAt( PageCursor cursor, KEY key, VALUE value, int pos, int key
insertValueAt( cursor, value, pos, keyCount );
}

private void removeKeyAt( PageCursor cursor, int pos, int keyCount, Type type )
{
removeSlotAt( cursor, pos, keyCount, keyOffset( 0 ), keySize );
}

@Override
void removeKeyValueAt( PageCursor cursor, int pos, int keyCount )
{
removeKeyAt( cursor, pos, keyCount, Type.LEAF );
removeKeyAt( cursor, pos, keyCount );
removeValueAt( cursor, pos, keyCount );
}

@Override
void removeKeyAndLeftChildAt( PageCursor cursor, int keyPos, int keyCount )
{
removeKeyAt( cursor, keyPos, keyCount, INTERNAL );
removeKeyAt( cursor, keyPos, keyCount );
removeChildAt( cursor, keyPos, keyCount );
}

@Override
void removeKeyAndRightChildAt( PageCursor cursor, int keyPos, int keyCount )
{
removeKeyAt( cursor, keyPos, keyCount, INTERNAL );
removeKeyAt( cursor, keyPos, keyCount );
removeChildAt( cursor, keyPos + 1, keyCount );
}

Expand Down Expand Up @@ -150,10 +137,53 @@ long childAt( PageCursor cursor, int pos, long stableGeneration, long unstableGe
return read( cursor, stableGeneration, unstableGeneration, pos );
}

@Override
void setChildAt( PageCursor cursor, long child, int pos, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( childOffset( pos ) );
writeChild( cursor, child, stableGeneration, unstableGeneration );
}

@Override
int internalMaxKeyCount()
{
return internalMaxKeyCount;
}

@Override
int leafMaxKeyCount()
{
return leafMaxKeyCount;
}

@Override
boolean reasonableKeyCount( int keyCount )
{
return keyCount >= 0 && keyCount <= Math.max( internalMaxKeyCount(), leafMaxKeyCount() );
}

@Override
int childOffset( int pos )
{
return BASE_HEADER_LENGTH + internalMaxKeyCount * keySize + pos * SIZE_PAGE_REFERENCE;
}

private void insertKeyAt( PageCursor cursor, KEY key, int pos, int keyCount )
{
insertKeySlotsAt( cursor, pos, 1, keyCount );
cursor.setOffset( keyOffset( pos ) );
layout.writeKey( cursor, key );
}

private void removeKeyAt( PageCursor cursor, int pos, int keyCount )
{
removeSlotAt( cursor, pos, keyCount, keyOffset( 0 ), keySize );
}

private void insertChildAt( PageCursor cursor, long child, int pos, int keyCount,
long stableGeneration, long unstableGeneration )
{
insertChildSlotsAt( cursor, pos, 1, keyCount );
insertChildSlot( cursor, pos, keyCount );
setChildAt( cursor, child, pos, stableGeneration, unstableGeneration );
}

Expand All @@ -162,17 +192,10 @@ private void removeChildAt( PageCursor cursor, int pos, int keyCount )
removeSlotAt( cursor, pos, keyCount + 1, childOffset( 0 ), childSize() );
}

@Override
void setChildAt( PageCursor cursor, long child, int pos, long stableGeneration, long unstableGeneration )
{
cursor.setOffset( childOffset( pos ) );
writeChild( cursor, child, stableGeneration, unstableGeneration );
}

private void insertKeyValueSlotsAt( PageCursor cursor, int pos, int numberOfSlots, int keyCount )
private void insertKeyValueSlots( PageCursor cursor, int numberOfSlots, int keyCount )
{
insertKeySlotsAt( cursor, pos, numberOfSlots, keyCount );
insertValueSlotsAt( cursor, pos, numberOfSlots, keyCount );
insertKeySlotsAt( cursor, 0, numberOfSlots, keyCount );
insertValueSlotsAt( cursor, 0, numberOfSlots, keyCount );
}

// Always insert together with key. Use insertKeyValueAt
Expand All @@ -198,27 +221,9 @@ private void insertValueSlotsAt( PageCursor cursor, int pos, int numberOfSlots,
insertSlotsAt( cursor, pos, numberOfSlots, keyCount, valueOffset( 0 ), valueSize );
}

private void insertChildSlotsAt( PageCursor cursor, int pos, int numberOfSlots, int keyCount )
{
insertSlotsAt( cursor, pos, numberOfSlots, keyCount + 1, childOffset( 0 ), childSize() );
}

@Override
int internalMaxKeyCount()
{
return internalMaxKeyCount;
}

@Override
int leafMaxKeyCount()
private void insertChildSlot( PageCursor cursor, int pos, int keyCount )
{
return leafMaxKeyCount;
}

@Override
boolean reasonableKeyCount( int keyCount )
{
return keyCount >= 0 && keyCount <= Math.max( internalMaxKeyCount(), leafMaxKeyCount() );
insertSlotsAt( cursor, pos, 1, keyCount + 1, childOffset( 0 ), childSize() );
}

private int keyOffset( int pos )
Expand All @@ -231,12 +236,6 @@ private int valueOffset( int pos )
return BASE_HEADER_LENGTH + leafMaxKeyCount * keySize + pos * valueSize;
}

@Override
int childOffset( int pos )
{
return BASE_HEADER_LENGTH + internalMaxKeyCount * keySize + pos * SIZE_PAGE_REFERENCE;
}

private int keySize()
{
return keySize;
Expand All @@ -247,6 +246,8 @@ private int valueSize()
return valueSize;
}

/* SPLIT, MERGE and REBALANCE*/

@Override
boolean internalOverflow( int keyCount )
{
Expand Down Expand Up @@ -400,7 +401,7 @@ void moveKeyValuesFromLeftToRight( PageCursor leftCursor, int leftKeyCount, Page
int numberOfKeysToMove = leftKeyCount - fromPosInLeftNode;

// Push keys and values in right sibling to the right
insertKeyValueSlotsAt( rightCursor, 0, numberOfKeysToMove, rightKeyCount );
insertKeyValueSlots( rightCursor, numberOfKeysToMove, rightKeyCount );

// Move keys and values from left sibling to right sibling
copyKeysAndValues( leftCursor, fromPosInLeftNode, rightCursor, 0, numberOfKeysToMove );
Expand Down
Expand Up @@ -45,15 +45,15 @@

public abstract class TreeNodeTestBase<KEY,VALUE>
{
static final int STABLE_GENERATION = 1;
static final int UNSTABLE_GENERATION = 3;
private static final int STABLE_GENERATION = 1;
private static final int UNSTABLE_GENERATION = 3;
private static final int HIGH_GENERATION = 4;

private static final int PAGE_SIZE = 512;
final PageCursor cursor = new PageAwareByteArrayCursor( PAGE_SIZE );

Layout<KEY,VALUE> layout;
TreeNode<KEY,VALUE> node;
private Layout<KEY,VALUE> layout;
private TreeNode<KEY,VALUE> node;

@Rule
public final RandomRule random = new RandomRule();
Expand Down Expand Up @@ -250,7 +250,7 @@ private void assertKeysAndChildren( long stable, long unstable, long... keysAndC
}

@Test
public void shouldInsertAndRemoveKeyAndChildInInternal() throws Exception
public void keyChildOperationsInInternal() throws Exception
{
// GIVEN
node.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
Expand Down

0 comments on commit 19e44ad

Please sign in to comment.