Skip to content

Commit

Permalink
Make InternalTreeLogic split ratio tests randomized
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Feb 7, 2019
1 parent 29e01e8 commit 846c8c6
Showing 1 changed file with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import static org.neo4j.index.internal.gbptree.ConsistencyChecker.assertNoCrashOrBrokenPointerInGSPP;
import static org.neo4j.index.internal.gbptree.GenerationSafePointerPair.pointer;
import static org.neo4j.index.internal.gbptree.TreeNode.Overflow.NO;
import static org.neo4j.index.internal.gbptree.TreeNode.Overflow.YES;
import static org.neo4j.index.internal.gbptree.TreeNode.Type.INTERNAL;
import static org.neo4j.index.internal.gbptree.TreeNode.Type.LEAF;
import static org.neo4j.index.internal.gbptree.ValueMergers.overwrite;
Expand Down Expand Up @@ -359,42 +360,41 @@ public void modifierMustUpdatePointersInSiblingsToSplit() throws Exception
assertSiblingOrderAndPointers( child0, child1, child2 );
}

// todo randomize me and verify "can't fit" any more in right / left instead of key counts
@Test
public void splitWithSplitRatio0() throws IOException
{
// given
ratioToKeepInLeftOnSplit = 0;
initialize();
int keyCount = 0;
int someHighSeed = 1000;
KEY key = key( someHighSeed - keyCount );
VALUE value = value( someHighSeed - keyCount );
KEY key = key( random.nextLong() );
VALUE value = value( random.nextLong() );
while ( node.leafOverflow( cursor, keyCount, key, value ) == NO )
{
insert( key, value );
assertFalse( structurePropagation.hasRightKeyInsert );

keyCount++;
key = key( someHighSeed - keyCount );
value = value( someHighSeed - keyCount );
key = key( random.nextLong() );
value = value( random.nextLong() );
}

// when
insert( key, value );
keyCount++;

// then
goTo( readCursor, rootId );
long child0 = childAt( readCursor, 0, stableGeneration, unstableGeneration );
long child1 = childAt( readCursor, 1, stableGeneration, unstableGeneration );
assertEquals( 1, numberOfRootSplits );

// Left node after split only singel key and right node the rest.
int leftKeyCount = keyCount( child0 );
int rightKeyCount = keyCount( child1 );
assertEquals( 1, leftKeyCount );
assertEquals( keyCount - leftKeyCount, rightKeyCount );
assertEquals( 1, numberOfRootSplits );

// Left node should hold as few keys as possible, such that nothing more can be moved to right.
KEY rightmostKeyInLeftChild = keyAt( child0,leftKeyCount - 1, LEAF );
VALUE rightmostValueInLeftChild = valueAt( child0, leftKeyCount - 1 );
goTo( readCursor, child1 );
assertEquals( YES, node.leafOverflow( readCursor, rightKeyCount, rightmostKeyInLeftChild, rightmostValueInLeftChild ) );
}

@Test
Expand All @@ -404,34 +404,33 @@ public void splitWithSplitRatio1() throws IOException
ratioToKeepInLeftOnSplit = 1;
initialize();
int keyCount = 0;
int someLowSeed = 1000;
KEY key = key( someLowSeed + keyCount );
VALUE value = value( someLowSeed + keyCount );
KEY key = key( random.nextLong() );
VALUE value = value( random.nextLong() );
while ( node.leafOverflow( cursor, keyCount, key, value ) == NO )
{
insert( key, value );
assertFalse( structurePropagation.hasRightKeyInsert );

keyCount++;
key = key( someLowSeed + keyCount );
value = value( someLowSeed + keyCount );
key = key( random.nextLong() );
value = value( random.nextLong() );
}

// when
insert( key, value );
keyCount++;

// then
goTo( readCursor, rootId );
long child0 = childAt( readCursor, 0, stableGeneration, unstableGeneration );
long child1 = childAt( readCursor, 1, stableGeneration, unstableGeneration );
int leftKeyCount = keyCount( child0 );
assertEquals( 1, numberOfRootSplits );

// Left node after split only singel key and right node the rest.
int leftKeyCount = keyCount( child0 );
int rightKeyCount = keyCount( child1 );
assertEquals( 1, rightKeyCount );
assertEquals( keyCount - rightKeyCount, leftKeyCount );
// Right node should hold as few keys as possible, such that nothing more can be moved to left.
KEY leftmostKeyInRightChild = keyAt( child1,0, LEAF );
VALUE leftmostValueInRightChild = valueAt( child1, 0 );
goTo( readCursor, child0 );
assertEquals( YES, node.leafOverflow( readCursor, leftKeyCount, leftmostKeyInRightChild, leftmostValueInRightChild ) );
}

/* REMOVE */
Expand Down Expand Up @@ -1810,6 +1809,21 @@ private KEY keyAt( int pos, TreeNode.Type type )
return node.keyAt( readCursor, layout.newKey(), pos, type );
}

private VALUE valueAt( long nodeId, int pos )
{
VALUE readValue = layout.newValue();
long prevId = readCursor.getCurrentPageId();
try
{
readCursor.next( nodeId );
return node.valueAt( readCursor, readValue, pos );
}
finally
{
readCursor.next( prevId );
}
}

private VALUE valueAt( int pos )
{
return node.valueAt( readCursor, layout.newValue(), pos );
Expand Down

0 comments on commit 846c8c6

Please sign in to comment.