Skip to content

Commit

Permalink
Various small cleanups around GBPTree
Browse files Browse the repository at this point in the history
  • Loading branch information
tinwelint committed Dec 6, 2016
1 parent e57d2e8 commit 2959b11
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 51 deletions.
Expand Up @@ -100,7 +100,7 @@ private long assertSiblings( PageCursor cursor, int level )
return rightmost.assertNext( cursor ); return rightmost.assertNext( cursor );
} }


private void assertKeyOrderAndSubtrees( PageCursor cursor, final long pageId, KeyRange<KEY> range, int level ) private void assertKeyOrderAndSubtrees( PageCursor cursor, long pageId, KeyRange<KEY> range, int level )
throws IOException throws IOException
{ {
int keyCount = node.keyCount( cursor ); int keyCount = node.keyCount( cursor );
Expand All @@ -111,7 +111,8 @@ private void assertKeyOrderAndSubtrees( PageCursor cursor, final long pageId, Ke
while ( pos < keyCount ) while ( pos < keyCount )
{ {
node.keyAt( cursor, readKey, pos ); node.keyAt( cursor, readKey, pos );
assert range.inRange( readKey ); assert range.inRange( readKey ) :
readKey + " (pos " + pos + "/" + (keyCount-1) + ")" + " isn't in range " + range;
if ( pos > 0 ) if ( pos > 0 )
{ {
assert comparator.compare( prev, readKey ) < 0; // Assume unique keys assert comparator.compare( prev, readKey ) < 0; // Assume unique keys
Expand Down
Expand Up @@ -257,7 +257,7 @@ private void writeState( PagedFile pagedFile ) throws IOException
} }
} }


private PageCursor openMetaPageCursor( PagedFile pagedFile ) throws IOException private static PageCursor openMetaPageCursor( PagedFile pagedFile ) throws IOException
{ {
PageCursor metaCursor = pagedFile.io( IdSpace.META_PAGE_ID, PagedFile.PF_SHARED_WRITE_LOCK ); PageCursor metaCursor = pagedFile.io( IdSpace.META_PAGE_ID, PagedFile.PF_SHARED_WRITE_LOCK );
if ( !metaCursor.next() ) if ( !metaCursor.next() )
Expand Down Expand Up @@ -314,7 +314,8 @@ private void writeMeta( Layout<KEY,VALUE> layout, PagedFile pagedFile ) throws I
} }
} }


private PagedFile mapWithCorrectPageSize( PageCache pageCache, File indexFile, PagedFile pagedFile ) throws IOException private PagedFile mapWithCorrectPageSize( PageCache pageCache, File indexFile, PagedFile pagedFile )
throws IOException
{ {
// This index was created with another page size, re-open with that actual page size // This index was created with another page size, re-open with that actual page size
if ( pageSize != pageCache.pageSize() ) if ( pageSize != pageCache.pageSize() )
Expand Down Expand Up @@ -425,7 +426,7 @@ private void goToRoot( PageCursor cursor ) throws IOException
bTreeNode.goTo( cursor, rootId, stableGeneration, unstableGeneration ); bTreeNode.goTo( cursor, rootId, stableGeneration, unstableGeneration );
} }


private void checkOutOfBounds( PageCursor cursor ) private static void checkOutOfBounds( PageCursor cursor )
{ {
if ( cursor.checkAndClearBoundsFlag() ) if ( cursor.checkAndClearBoundsFlag() )
{ {
Expand Down
Expand Up @@ -19,8 +19,6 @@
*/ */
package org.neo4j.index.gbptree; package org.neo4j.index.gbptree;


import java.io.IOException;

import org.neo4j.io.pagecache.PageCursor; import org.neo4j.io.pagecache.PageCursor;


class TreeState class TreeState
Expand Down Expand Up @@ -73,12 +71,11 @@ boolean isValid()
} }


static void write( PageCursor cursor, long stableGeneration, long unstableGeneration, long rootId, static void write( PageCursor cursor, long stableGeneration, long unstableGeneration, long rootId,
long lastId ) throws IOException long lastId )
{ {
GenSafePointer.assertGeneration( stableGeneration ); GenSafePointer.assertGeneration( stableGeneration );
GenSafePointer.assertGeneration( unstableGeneration ); GenSafePointer.assertGeneration( unstableGeneration );



writeStateOnce( cursor, stableGeneration, unstableGeneration, rootId, lastId ); // Write state writeStateOnce( cursor, stableGeneration, unstableGeneration, rootId, lastId ); // Write state
writeStateOnce( cursor, stableGeneration, unstableGeneration, rootId, lastId ); // Write checksum writeStateOnce( cursor, stableGeneration, unstableGeneration, rootId, lastId ); // Write checksum
} }
Expand Down
Expand Up @@ -43,7 +43,7 @@ public class SeekCursorTest
private final Layout<MutableLong,MutableLong> layout = new SimpleLongLayout(); private final Layout<MutableLong,MutableLong> layout = new SimpleLongLayout();
private final TreeNode<MutableLong,MutableLong> treeNode = new TreeNode<>( PAGE_SIZE, layout ); private final TreeNode<MutableLong,MutableLong> treeNode = new TreeNode<>( PAGE_SIZE, layout );
private final PageAwareByteArrayCursor delegate = new PageAwareByteArrayCursor( PAGE_SIZE ); private final PageAwareByteArrayCursor delegate = new PageAwareByteArrayCursor( PAGE_SIZE );
private final TestPageCursor pageCursor = new TestPageCursor( delegate ); private final TestPageCursor cursor = new TestPageCursor( delegate );
private final int maxKeyCount = treeNode.leafMaxKeyCount(); private final int maxKeyCount = treeNode.leafMaxKeyCount();
private final byte[] tmp = new byte[PAGE_SIZE]; private final byte[] tmp = new byte[PAGE_SIZE];


Expand All @@ -55,8 +55,8 @@ public class SeekCursorTest
@Before @Before
public void setUp() throws IOException public void setUp() throws IOException
{ {
pageCursor.next( 0L ); cursor.next( 0L );
treeNode.initializeLeaf( pageCursor, STABLE_GENERATION, UNSTABLE_GENERATION ); treeNode.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION );
} }


/* NO CONCURRENT INSERT */ /* NO CONCURRENT INSERT */
Expand Down Expand Up @@ -132,13 +132,13 @@ public void mustFindEntriesSpanningTwoLeaves() throws Exception
append( i ); append( i );
i++; i++;
} }
long left = createRightSibling( pageCursor ); long left = createRightSibling( cursor );
while ( i < maxKeyCount * 2 ) while ( i < maxKeyCount * 2 )
{ {
append( i ); append( i );
i++; i++;
} }
pageCursor.next( left ); cursor.next( left );


int fromInclusive = 0; int fromInclusive = 0;
int toExclusive = maxKeyCount * 2; int toExclusive = maxKeyCount * 2;
Expand All @@ -157,17 +157,17 @@ public void mustFindEntriesOnSecondLeafWhenStartingFromFirstLeaf() throws Except
{ {
// GIVEN // GIVEN
int i = 0; int i = 0;
long left = pageCursor.getCurrentPageId(); long left = cursor.getCurrentPageId();
while ( i < maxKeyCount * 2 ) while ( i < maxKeyCount * 2 )
{ {
if ( i == maxKeyCount ) if ( i == maxKeyCount )
{ {
createRightSibling( pageCursor ); createRightSibling( cursor );
} }
append( i ); append( i );
i++; i++;
} }
pageCursor.next( left ); cursor.next( left );


int fromInclusive = maxKeyCount; int fromInclusive = maxKeyCount;
int toExclusive = maxKeyCount * 2; int toExclusive = maxKeyCount * 2;
Expand All @@ -184,7 +184,7 @@ public void mustFindEntriesOnSecondLeafWhenStartingFromFirstLeaf() throws Except
public void mustNotContinueToSecondLeafAfterFindingEndOfRangeInFirst() throws Exception public void mustNotContinueToSecondLeafAfterFindingEndOfRangeInFirst() throws Exception
{ {
AtomicBoolean nextCalled = new AtomicBoolean(); AtomicBoolean nextCalled = new AtomicBoolean();
PageCursor pageCursorSpy = new DelegatingPageCursor( pageCursor ) PageCursor pageCursorSpy = new DelegatingPageCursor( cursor )
{ {
@Override @Override
public boolean next( long pageId ) throws IOException public boolean next( long pageId ) throws IOException
Expand All @@ -196,7 +196,7 @@ public boolean next( long pageId ) throws IOException


// GIVEN // GIVEN
int i = 0; int i = 0;
long left = pageCursor.getCurrentPageId(); long left = cursor.getCurrentPageId();
while ( i < maxKeyCount * 2 ) while ( i < maxKeyCount * 2 )
{ {
if ( i == maxKeyCount ) if ( i == maxKeyCount )
Expand Down Expand Up @@ -252,7 +252,7 @@ public void mustFindNewKeyInsertedRightOfSeekPoint() throws Exception


// Seeker pauses and writer insert new key at the end of leaf // Seeker pauses and writer insert new key at the end of leaf
append( middle ); append( middle );
pageCursor.changed(); this.cursor.changed();


// Seeker continue // Seeker continue
while ( cursor.next() ) while ( cursor.next() )
Expand Down Expand Up @@ -296,7 +296,7 @@ public void mustFindKeyInsertedOnSeekPosition() throws Exception
long midInsert = (stopPoint * 2) - 1; long midInsert = (stopPoint * 2) - 1;
insertIn( stopPoint, midInsert ); insertIn( stopPoint, midInsert );
expected.add( readKeys, midInsert ); expected.add( readKeys, midInsert );
pageCursor.changed(); this.cursor.changed();


while ( cursor.next() ) while ( cursor.next() )
{ {
Expand Down Expand Up @@ -339,7 +339,7 @@ public void mustNotFindKeyInsertedLeftOfSeekPoint() throws Exception
// Seeker pauses and writer insert new key to the left of seekers next position // Seeker pauses and writer insert new key to the left of seekers next position
long midInsert = ((stopPoint - 1) * 2) - 1; long midInsert = ((stopPoint - 1) * 2) - 1;
insertIn( stopPoint - 1, midInsert ); insertIn( stopPoint - 1, midInsert );
pageCursor.changed(); this.cursor.changed();


while ( cursor.next() ) while ( cursor.next() )
{ {
Expand Down Expand Up @@ -385,16 +385,16 @@ public void mustContinueToNextLeafWhenRangeIsSplitIntoRightLeafAndPosToLeft() th
expected.add( (long) maxKeyCount ); expected.add( (long) maxKeyCount );


// Add rightmost keys to right sibling // Add rightmost keys to right sibling
long left = createRightSibling( pageCursor ); long left = createRightSibling( this.cursor );
for ( int i = middle; i <= maxKeyCount; i++ ) for ( int i = middle; i <= maxKeyCount; i++ )
{ {
Long key = expected.get( i ); Long key = expected.get( i );
append( key ); append( key );
} }
// Update keycount in left sibling // Update keycount in left sibling
pageCursor.next( left ); this.cursor.next( left );
treeNode.setKeyCount( pageCursor, middle ); treeNode.setKeyCount( this.cursor, middle );
pageCursor.changed(); this.cursor.changed();


while ( cursor.next() ) while ( cursor.next() )
{ {
Expand Down Expand Up @@ -438,16 +438,16 @@ public void mustContinueToNextLeafWhenRangeIsSplitIntoRightLeafAndPosToRight() t
expected.add( (long) maxKeyCount ); expected.add( (long) maxKeyCount );


// Add rightmost keys to right sibling // Add rightmost keys to right sibling
long left = createRightSibling( pageCursor ); long left = createRightSibling( this.cursor );
for ( int i = middle; i <= maxKeyCount; i++ ) for ( int i = middle; i <= maxKeyCount; i++ )
{ {
Long key = expected.get( i ); Long key = expected.get( i );
append( key ); append( key );
} }
// Update keycount in left sibling // Update keycount in left sibling
pageCursor.next( left ); this.cursor.next( left );
treeNode.setKeyCount( pageCursor, middle ); treeNode.setKeyCount( this.cursor, middle );
pageCursor.changed(); this.cursor.changed();


while ( cursor.next() ) while ( cursor.next() )
{ {
Expand Down Expand Up @@ -490,7 +490,7 @@ public void mustNotFindKeyRemovedRightOfSeekPoint() throws Exception
// Seeker pauses and writer remove rightmost key // Seeker pauses and writer remove rightmost key
// [0 1 ... maxKeyCount-2] // [0 1 ... maxKeyCount-2]
remove( maxKeyCount - 1 ); remove( maxKeyCount - 1 );
pageCursor.changed(); this.cursor.changed();


while ( cursor.next() ) while ( cursor.next() )
{ {
Expand Down Expand Up @@ -531,7 +531,7 @@ public void mustFindKeyMovedToLeftOfSeekPointBecauseOfRemove() throws Exception
// Seeker pauses and writer remove rightmost key // Seeker pauses and writer remove rightmost key
// [1 ... maxKeyCount-1] // [1 ... maxKeyCount-1]
remove( 0 ); remove( 0 );
pageCursor.changed(); this.cursor.changed();


while ( cursor.next() ) while ( cursor.next() )
{ {
Expand Down Expand Up @@ -570,7 +570,7 @@ public void mustFindKeyMovedToLeftOfSeekPointBecauseOfRemoveOfPreviouslyReturned


// Seeker pauses and writer remove rightmost key // Seeker pauses and writer remove rightmost key
remove( middle - 1 ); remove( middle - 1 );
pageCursor.changed(); this.cursor.changed();


while ( cursor.next() ) while ( cursor.next() )
{ {
Expand Down Expand Up @@ -607,7 +607,7 @@ public void mustRereadHeadersOnRetry() throws Exception
to.setValue( keyCount + 1 ); // +1 because we're adding one more down below to.setValue( keyCount + 1 ); // +1 because we're adding one more down below


// WHEN // WHEN
try ( SeekCursor<MutableLong,MutableLong> cursor = new SeekCursor<>( pageCursor, key, value, try ( SeekCursor<MutableLong,MutableLong> cursor = new SeekCursor<>( this.cursor, key, value,
treeNode, from, to, layout, STABLE_GENERATION, UNSTABLE_GENERATION, 2, keyCount ) ) treeNode, from, to, layout, STABLE_GENERATION, UNSTABLE_GENERATION, 2, keyCount ) )
{ {
// reading a couple of keys // reading a couple of keys
Expand All @@ -618,7 +618,7 @@ public void mustRereadHeadersOnRetry() throws Exception


// and WHEN a change happens // and WHEN a change happens
append( keyCount ); append( keyCount );
pageCursor.changed(); this.cursor.changed();


// THEN at least keyCount should be re-read on next() // THEN at least keyCount should be re-read on next()
assertTrue( cursor.next() ); assertTrue( cursor.next() );
Expand All @@ -638,7 +638,7 @@ public void mustRereadHeadersOnRetry() throws Exception
private SeekCursor<MutableLong,MutableLong> seekCursor( long fromInclusive, long toExclusive, int pos, private SeekCursor<MutableLong,MutableLong> seekCursor( long fromInclusive, long toExclusive, int pos,
int keyCount ) int keyCount )
{ {
return seekCursor( fromInclusive, toExclusive, pos, pageCursor, keyCount ); return seekCursor( fromInclusive, toExclusive, pos, cursor, keyCount );
} }


private SeekCursor<MutableLong,MutableLong> seekCursor( long fromInclusive, long toExclusive, int pos, private SeekCursor<MutableLong,MutableLong> seekCursor( long fromInclusive, long toExclusive, int pos,
Expand All @@ -651,7 +651,7 @@ private SeekCursor<MutableLong,MutableLong> seekCursor( long fromInclusive, long
} }


/** /**
* Create a right sibling to node pointed to by pageCursor. Leave cursor on new right sibling when done, * Create a right sibling to node pointed to by cursor. Leave cursor on new right sibling when done,
* and return id of left sibling. * and return id of left sibling.
*/ */
private long createRightSibling( PageCursor pageCursor ) throws IOException private long createRightSibling( PageCursor pageCursor ) throws IOException
Expand Down Expand Up @@ -709,33 +709,33 @@ private void insertKeysAndValues( int keyCount )


private void append( long k ) private void append( long k )
{ {
int keyCount = treeNode.keyCount( pageCursor ); int keyCount = treeNode.keyCount( cursor );
key.setValue( k ); key.setValue( k );
value.setValue( valueForKey( k ) ); value.setValue( valueForKey( k ) );
treeNode.insertKeyAt( pageCursor, key, keyCount, keyCount, tmp ); treeNode.insertKeyAt( cursor, key, keyCount, keyCount, tmp );
treeNode.insertValueAt( pageCursor, value, keyCount, keyCount, tmp ); treeNode.insertValueAt( cursor, value, keyCount, keyCount, tmp );
treeNode.setKeyCount( pageCursor, keyCount + 1 ); treeNode.setKeyCount( cursor, keyCount + 1 );
} }


private void insertIn( int pos, long k ) private void insertIn( int pos, long k )
{ {
int keyCount = treeNode.keyCount( pageCursor ); int keyCount = treeNode.keyCount( cursor );
if ( keyCount + 1 > maxKeyCount ) if ( keyCount + 1 > maxKeyCount )
{ {
throw new IllegalStateException( "Can not insert another key in current node" ); throw new IllegalStateException( "Can not insert another key in current node" );
} }
key.setValue( k ); key.setValue( k );
value.setValue( valueForKey( k ) ); value.setValue( valueForKey( k ) );
treeNode.insertKeyAt( pageCursor, key, pos, keyCount, tmp ); treeNode.insertKeyAt( cursor, key, pos, keyCount, tmp );
treeNode.insertValueAt( pageCursor, value, pos, keyCount, tmp ); treeNode.insertValueAt( cursor, value, pos, keyCount, tmp );
treeNode.setKeyCount( pageCursor, keyCount + 1 ); treeNode.setKeyCount( cursor, keyCount + 1 );
} }


private void remove( int pos ) private void remove( int pos )
{ {
int keyCount = treeNode.keyCount( pageCursor ); int keyCount = treeNode.keyCount( cursor );
treeNode.removeKeyAt( pageCursor, pos, keyCount, tmp ); treeNode.removeKeyAt( cursor, pos, keyCount, tmp );
treeNode.removeValueAt( pageCursor, pos, keyCount, tmp ); treeNode.removeValueAt( cursor, pos, keyCount, tmp );
treeNode.setKeyCount( pageCursor, keyCount - 1 ); treeNode.setKeyCount( cursor, keyCount - 1 );
} }
} }
Expand Up @@ -190,4 +190,4 @@ void verify( Pair<TreeState,TreeState> states, SelectionUseCase selection )


abstract void verify( Pair<TreeState,TreeState> states, SelectionUseCase selection ); abstract void verify( Pair<TreeState,TreeState> states, SelectionUseCase selection );
} }
} }
Expand Up @@ -127,4 +127,4 @@ private void breakChecksum( PageCursor cursor )
long existing = cursor.getLong( cursor.getOffset() ); long existing = cursor.getLong( cursor.getOffset() );
cursor.putLong( cursor.getOffset(), ~existing ); cursor.putLong( cursor.getOffset(), ~existing );
} }
} }

0 comments on commit 2959b11

Please sign in to comment.