Skip to content

Commit

Permalink
TreeNode.initialize* not longer static and more
Browse files Browse the repository at this point in the history
- initializeLeaf and Internal is not longer static
- Fix type in CrashGenerationCleanerTest
- readValue take valueSize for dynamic size values
- TreeNodeFixedSixeTest -> TreeNodeFixedSizeTest
- Cleanup in TreeNodeTestBase
  • Loading branch information
burqen committed Jan 16, 2018
1 parent f2642fe commit 52ead5b
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 182 deletions.
Expand Up @@ -478,7 +478,7 @@ private void initializeAfterCreation( Layout<KEY,VALUE> layout, Consumer<PageCur
{
long stableGeneration = stableGeneration( generation );
long unstableGeneration = unstableGeneration( generation );
TreeNode.initializeLeaf( cursor, stableGeneration, unstableGeneration );
bTreeNode.initializeLeaf( cursor, stableGeneration, unstableGeneration );
checkOutOfBounds( cursor );
}

Expand Down Expand Up @@ -1190,7 +1190,7 @@ public void merge( KEY key, VALUE value, ValueMerger<KEY,VALUE> valueMerger ) th
long newRootId = freeList.acquireNewId( stableGeneration, unstableGeneration );
PageCursorUtil.goTo( cursor, "new root", newRootId );

TreeNode.initializeInternal( cursor, stableGeneration, unstableGeneration );
bTreeNode.initializeInternal( cursor, stableGeneration, unstableGeneration );
bTreeNode.setChildAt( cursor, structurePropagation.midChild, 0,
stableGeneration, unstableGeneration );
bTreeNode.insertKeyAndRightChildAt( cursor, structurePropagation.rightKey, structurePropagation.rightChild, 0, 0,
Expand Down
Expand Up @@ -477,7 +477,7 @@ private void splitInternal( PageCursor cursor, StructurePropagation<KEY> structu
{
// Initialize new right
TreeNode.goTo( rightCursor, "new right sibling in split", newRight );
TreeNode.initializeInternal( rightCursor, stableGeneration, unstableGeneration );
bTreeNode.initializeInternal( rightCursor, stableGeneration, unstableGeneration );
TreeNode.setRightSibling( rightCursor, oldRight, stableGeneration, unstableGeneration );
TreeNode.setLeftSibling( rightCursor, current, stableGeneration, unstableGeneration );
int rightKeyCount = keyCountAfterInsert - middlePos - 1; // -1 because don't keep prim key in internal
Expand Down Expand Up @@ -650,7 +650,7 @@ private void splitLeaf( PageCursor cursor, StructurePropagation<KEY> structurePr
{
// Initialize new right
TreeNode.goTo( rightCursor, "new right sibling in split", newRight );
TreeNode.initializeLeaf( rightCursor, stableGeneration, unstableGeneration );
bTreeNode.initializeLeaf( rightCursor, stableGeneration, unstableGeneration );
TreeNode.setRightSibling( rightCursor, oldRight, stableGeneration, unstableGeneration );
TreeNode.setLeftSibling( rightCursor, current, stableGeneration, unstableGeneration );
int rightKeyCount = keyCountAfterInsert - middlePos;
Expand Down
Expand Up @@ -40,7 +40,8 @@
*/
public interface Layout<KEY, VALUE> extends Comparator<KEY>
{
int FIXED_SIZE_KEY = 0;
int FIXED_SIZE_KEY = -1;
int FIXED_SIZE_VALUE = -1;

/**
* @return new key instance.
Expand Down Expand Up @@ -91,19 +92,19 @@ public interface Layout<KEY, VALUE> extends Comparator<KEY>

/**
* Reads key contents at {@code cursor} at its current offset into {@code key}.
* @param cursor {@link PageCursor} to read from, at current offset.
* @param cursor {@link PageCursor} to read from, at current offset.
* @param into key instances to read into.
* @param keySize
* @param keySize size of key to read or {@link #FIXED_SIZE_KEY} if key is fixed size.
*/
void readKey( PageCursor cursor, KEY into, int keySize );

/**
* Reads value contents at {@code cursor} at its current offset into {@code value}.
*
* @param cursor {@link PageCursor} to read from, at current offset.
* @param into value instances to read into.
* @param valueSize size of key to read or {@link #FIXED_SIZE_VALUE} if value is fixed size.
*/
void readValue( PageCursor cursor, VALUE into );
void readValue( PageCursor cursor, VALUE into, int valueSize );

/**
* Used as verification when loading an index after creation, to verify that the same layout is used,
Expand Down Expand Up @@ -272,7 +273,7 @@ public void readKey( PageCursor cursor, Object into, int keySize )
}

@Override
public void readValue( PageCursor cursor, Object into )
public void readValue( PageCursor cursor, Object into, int valueSize )
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
Expand Down
Expand Up @@ -104,7 +104,7 @@ static byte nodeType( PageCursor cursor )
return cursor.getByte( BYTE_POS_NODE_TYPE );
}

private static void initialize( PageCursor cursor, byte type, long stableGeneration, long unstableGeneration )
private static void writeBaseHeader( PageCursor cursor, byte type, long stableGeneration, long unstableGeneration )
{
cursor.putByte( BYTE_POS_NODE_TYPE, NODE_TYPE_TREE_NODE );
cursor.putByte( BYTE_POS_TYPE, type );
Expand All @@ -115,16 +115,24 @@ private static void initialize( PageCursor cursor, byte type, long stableGenerat
setSuccessor( cursor, NO_NODE_FLAG, stableGeneration, unstableGeneration );
}

static void initializeLeaf( PageCursor cursor, long stableGeneration, long unstableGeneration )
void initializeLeaf( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
initialize( cursor, LEAF_FLAG, stableGeneration, unstableGeneration );
writeBaseHeader( cursor, LEAF_FLAG, stableGeneration, unstableGeneration );
writeAdditionalHeader( cursor );
}

static void initializeInternal( PageCursor cursor, long stableGeneration, long unstableGeneration )
void initializeInternal( PageCursor cursor, long stableGeneration, long unstableGeneration )
{
initialize( cursor, INTERNAL_FLAG, stableGeneration, unstableGeneration );
writeBaseHeader( cursor, INTERNAL_FLAG, stableGeneration, unstableGeneration );
writeAdditionalHeader( cursor );
}

/**
* Write additional header. When called, cursor should be located directly after base header.
* Meaning at {@link #BASE_HEADER_LENGTH}.
*/
abstract void writeAdditionalHeader( PageCursor cursor );

// HEADER METHODS

static boolean isLeaf( PageCursor cursor )
Expand Down Expand Up @@ -296,15 +304,6 @@ static void goTo( PageCursor cursor, String messageOnError, long nodeId )
PageCursorUtil.goTo( cursor, messageOnError, GenerationSafePointerPair.pointer( nodeId ) );
}

@Override
public String toString()
{
return "TreeNode[pageSize:" + pageSize + ", internalMax:" + internalMaxKeyCount() + ", leafMax:" + leafMaxKeyCount() + ", " +
additionalToString() + "]";
}

abstract String additionalToString();

/* SPLIT, MERGE AND REBALANCE */

abstract boolean internalOverflow( int keyCount );
Expand Down
Expand Up @@ -23,6 +23,7 @@

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 @@ -54,6 +55,11 @@ class TreeNodeFixedSize<KEY,VALUE> extends TreeNode<KEY,VALUE>
}
}

@Override
void writeAdditionalHeader( PageCursor cursor )
{ // no-op
}

private static int childSize()
{
return SIZE_PAGE_REFERENCE;
Expand Down Expand Up @@ -126,7 +132,7 @@ void setKeyAt( PageCursor cursor, KEY key, int pos, Type type )
VALUE valueAt( PageCursor cursor, VALUE value, int pos )
{
cursor.setOffset( valueOffset( pos ) );
layout.readValue( cursor, value );
layout.readValue( cursor, value, FIXED_SIZE_VALUE );
return value;
}

Expand Down Expand Up @@ -231,12 +237,6 @@ int childOffset( int pos )
return BASE_HEADER_LENGTH + internalMaxKeyCount * keySize + pos * SIZE_PAGE_REFERENCE;
}

@Override
String additionalToString()
{
return "keySize:" + keySize() + ", valueSize:" + valueSize;
}

private int keySize()
{
return keySize;
Expand Down Expand Up @@ -411,4 +411,11 @@ private void copyKeysAndValues( PageCursor fromCursor, int fromPos, PageCursor t
fromCursor.copyTo( keyOffset( fromPos ), toCursor, keyOffset( toPos ), count * keySize() );
fromCursor.copyTo( valueOffset( fromPos ), toCursor, valueOffset( toPos ),count * valueSize() );
}

@Override
public String toString()
{
return "TreeNodeFixedSize[pageSize:" + pageSize + ", internalMax:" + internalMaxKeyCount() + ", leafMax:" + leafMaxKeyCount() + ", " +
"keySize:" + keySize() + ", valueSize:" + valueSize + "]";
}
}
Expand Up @@ -48,7 +48,7 @@ public void shouldThrowDescriptiveExceptionOnBrokenGSPP() throws Exception
long pointer = 123;

cursor.next( 0 );
TreeNode.initializeInternal( cursor, stableGeneration, crashGeneration );
new TreeNodeFixedSize<>( pageSize, new SimpleLongLayout() ).initializeInternal( cursor, stableGeneration, crashGeneration );
TreeNode.setSuccessor( cursor, pointer, stableGeneration, crashGeneration );

// WHEN
Expand Down Expand Up @@ -83,7 +83,7 @@ public void shouldDetectUnusedPages() throws Exception
InternalTreeLogic<MutableLong,MutableLong> logic = new InternalTreeLogic<>( idProvider, node, layout );
PageCursor cursor = new PageAwareByteArrayCursor( pageSize );
cursor.next( idProvider.acquireNewId( stableGeneration, unstableGeneration ) );
TreeNode.initializeLeaf( cursor, stableGeneration, unstableGeneration );
node.initializeLeaf( cursor, stableGeneration, unstableGeneration );
logic.initialize( cursor );
StructurePropagation<MutableLong> structure = new StructurePropagation<>( layout.newKey(), layout.newKey(),
layout.newKey() );
Expand All @@ -99,7 +99,7 @@ public void shouldDetectUnusedPages() throws Exception
{
goTo( cursor, "new root",
idProvider.acquireNewId( stableGeneration, unstableGeneration ) );
TreeNode.initializeInternal( cursor, stableGeneration, unstableGeneration );
node.initializeInternal( cursor, stableGeneration, unstableGeneration );
node.setChildAt( cursor, structure.midChild, 0, stableGeneration, unstableGeneration );
node.insertKeyAndRightChildAt( cursor, structure.rightKey, structure.rightChild, 0, 0,
stableGeneration, unstableGeneration );
Expand Down
Expand Up @@ -61,14 +61,14 @@ public class CrashGenerationCleanerTest

private PagedFile pagedFile;
private final Layout<MutableLong,MutableLong> layout = new SimpleLongLayout();
private final CorruptableTreeNode corruptableTreeNode = new CorruptableTreeNode( PAGE_SIZE, layout );
private final CorruptibleTreeNode corruptibleTreeNode = new CorruptibleTreeNode( PAGE_SIZE, layout );
private final int oldStableGeneration = 9;
private final int stableGeneration = 10;
private final int unstableGeneration = 12;
private final int crashGeneration = 11;
private final int firstChildPos = 0;
private final int middleChildPos = corruptableTreeNode.internalMaxKeyCount() / 2;
private final int lastChildPos = corruptableTreeNode.internalMaxKeyCount();
private final int middleChildPos = corruptibleTreeNode.internalMaxKeyCount() / 2;
private final int lastChildPos = corruptibleTreeNode.internalMaxKeyCount();
private final List<PageCorruption> possibleCorruptionsInInternal = Arrays.asList(
crashed( leftSibling() ),
crashed( rightSibling() ),
Expand Down Expand Up @@ -222,7 +222,7 @@ public void shouldCleanLargeFile() throws Exception
private CrashGenerationCleaner crashGenerationCleaner( PagedFile pagedFile, int lowTreeNodeId, int highTreeNodeId,
SimpleCleanupMonitor monitor )
{
return new CrashGenerationCleaner( pagedFile, corruptableTreeNode, lowTreeNodeId, highTreeNodeId,
return new CrashGenerationCleaner( pagedFile, corruptibleTreeNode, lowTreeNodeId, highTreeNodeId,
stableGeneration, unstableGeneration, monitor );
}

Expand All @@ -233,7 +233,7 @@ private void initializeFile( PagedFile pagedFile, Page... pages ) throws IOExcep
for ( Page page : pages )
{
cursor.next();
page.write( cursor, corruptableTreeNode, stableGeneration, unstableGeneration, crashGeneration );
page.write( cursor, corruptibleTreeNode, stableGeneration, unstableGeneration, crashGeneration );
}
}
}
Expand Down Expand Up @@ -317,7 +317,7 @@ private Page( PageType type, PageCorruption... pageCorruptions )
this.pageCorruptions = pageCorruptions;
}

private void write( PageCursor cursor, CorruptableTreeNode node, int stableGeneration, int unstableGeneration,
private void write( PageCursor cursor, CorruptibleTreeNode node, int stableGeneration, int unstableGeneration,
int crashGeneration ) throws IOException
{
type.write( cursor, node, oldStableGeneration, stableGeneration );
Expand All @@ -331,31 +331,31 @@ enum PageType
LEAF
{
@Override
void write( PageCursor cursor, CorruptableTreeNode corruptableTreeNode, int stableGeneration,
void write( PageCursor cursor, CorruptibleTreeNode corruptibleTreeNode, int stableGeneration,
int unstableGeneration )
{
TreeNode.initializeLeaf( cursor, stableGeneration, unstableGeneration );
corruptibleTreeNode.initializeLeaf( cursor, stableGeneration, unstableGeneration );
}
},
INTERNAL
{
@Override
void write( PageCursor cursor, CorruptableTreeNode corruptableTreeNode, int stableGeneration,
void write( PageCursor cursor, CorruptibleTreeNode corruptibleTreeNode, int stableGeneration,
int unstableGeneration )
{
TreeNode.initializeInternal( cursor, stableGeneration, unstableGeneration );
int maxKeyCount = corruptableTreeNode.internalMaxKeyCount();
corruptibleTreeNode.initializeInternal( cursor, stableGeneration, unstableGeneration );
int maxKeyCount = corruptibleTreeNode.internalMaxKeyCount();
long base = IdSpace.MIN_TREE_NODE_ID;
for ( int i = 0; i <= maxKeyCount; i++ )
{
long child = base + i;
corruptableTreeNode.setChildAt( cursor, child, i, stableGeneration, unstableGeneration );
corruptibleTreeNode.setChildAt( cursor, child, i, stableGeneration, unstableGeneration );
}
TreeNode.setKeyCount( cursor, maxKeyCount );
}
};

abstract void write( PageCursor cursor, CorruptableTreeNode corruptableTreeNode,
abstract void write( PageCursor cursor, CorruptibleTreeNode corruptibleTreeNode,
int stableGeneration, int unstableGeneration );
}

Expand Down Expand Up @@ -427,13 +427,13 @@ private PageCorruption crashed( GSPPType gsppType )

private interface PageCorruption
{
void corrupt( PageCursor pageCursor, CorruptableTreeNode node, int stableGeneration,
void corrupt( PageCursor pageCursor, CorruptibleTreeNode node, int stableGeneration,
int unstableGeneration, int crashGeneration );
}

class CorruptableTreeNode extends TreeNodeFixedSize<MutableLong,MutableLong>
class CorruptibleTreeNode extends TreeNodeFixedSize<MutableLong,MutableLong>
{
CorruptableTreeNode( int pageSize, Layout<MutableLong,MutableLong> layout )
CorruptibleTreeNode( int pageSize, Layout<MutableLong,MutableLong> layout )
{
super( pageSize, layout );
}
Expand Down
Expand Up @@ -1473,7 +1473,7 @@ private int keyCount()

private void initialize()
{
TreeNode.initializeLeaf( cursor, stableGeneration, unstableGeneration );
node.initializeLeaf( cursor, stableGeneration, unstableGeneration );
updateRoot();
}

Expand Down Expand Up @@ -1545,7 +1545,7 @@ private void newRootFromSplit( StructurePropagation<MutableLong> split ) throws
assertTrue( split.hasRightKeyInsert );
long rootId = id.acquireNewId( stableGeneration, unstableGeneration );
goTo( cursor, rootId );
TreeNode.initializeInternal( cursor, stableGeneration, unstableGeneration );
node.initializeInternal( cursor, stableGeneration, unstableGeneration );
node.setChildAt( cursor, split.midChild, 0, stableGeneration, unstableGeneration );
node.insertKeyAndRightChildAt( cursor, split.rightKey, split.rightChild, 0, 0, stableGeneration, unstableGeneration );
TreeNode.setKeyCount( cursor, 1 );
Expand Down

0 comments on commit 52ead5b

Please sign in to comment.