Skip to content

Commit

Permalink
GBPTree can create writer with different split ratio
Browse files Browse the repository at this point in the history
Introduce method in GBPTree that can create a writer with a different
ratioToKeepInLeftOnSplit than default one.

Use in GBPTreeITBase.
  • Loading branch information
burqen committed Feb 7, 2019
1 parent 846c8c6 commit 38e3e77
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Expand Up @@ -1026,20 +1026,30 @@ private void internalIndexClose() throws IOException
closed = true;
}

/**
* Use default value for ratioToKeepInLeftOnSplit
* @see GBPTree#writer(double)
*/
public Writer<KEY,VALUE> writer() throws IOException
{
return writer( InternalTreeLogic.DEFAULT_SPLIT_RATIO );
}

/**
* Returns a {@link Writer} able to modify the index, i.e. insert and remove keys/values.
* After usage the returned writer must be closed, typically by using try-with-resource clause.
*
* @param ratioToKeepInLeftOnSplit Decide how much to keep in left node on split, 0=keep nothing, 0.5=split 50-50, 1=keep everything.
* @return the single {@link Writer} for this index. The returned writer must be
* {@link Writer#close() closed} before another caller can acquire this writer.
* @throws IOException on error accessing the index.
* @throws IllegalStateException for calls made between a successful call to this method and closing the
* returned writer.
*/
public Writer<KEY,VALUE> writer() throws IOException
public Writer<KEY,VALUE> writer( double ratioToKeepInLeftOnSplit ) throws IOException
{
assertRecoveryCleanSuccessful();
writer.initialize();
writer.initialize( ratioToKeepInLeftOnSplit );
changesSinceLastCheckpoint = true;
return writer;
}
Expand Down Expand Up @@ -1232,8 +1242,9 @@ private class SingleWriter implements Writer<KEY,VALUE>
* </ul>
*
* @throws IOException if fail to open {@link PageCursor}
* @param ratioToKeepInLeftOnSplit Decide how much to keep in left node on split, 0=keep nothing, 0.5=split 50-50, 1=keep everything.
*/
void initialize() throws IOException
void initialize( double ratioToKeepInLeftOnSplit ) throws IOException
{
if ( !writerTaken.compareAndSet( false, true ) )
{
Expand All @@ -1250,7 +1261,7 @@ void initialize() throws IOException
stableGeneration = stableGeneration( generation );
unstableGeneration = unstableGeneration( generation );
assert assertNoSuccessor( cursor, stableGeneration, unstableGeneration );
treeLogic.initialize( cursor );
treeLogic.initialize( cursor, ratioToKeepInLeftOnSplit );
success = true;
}
catch ( Throwable e )
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.index.internal.gbptree;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
Expand Down Expand Up @@ -57,9 +58,16 @@ public abstract class GBPTreeITBase<KEY,VALUE>
@Rule
public final RuleChain rules = outerRule( fs ).around( directory ).around( pageCacheRule ).around( random );

private double ratioToKeepInLeftOnSplit;
private TestLayout<KEY,VALUE> layout;
private GBPTree<KEY,VALUE> index;

@Before
public void setup()
{
ratioToKeepInLeftOnSplit = random.nextBoolean() ? InternalTreeLogic.DEFAULT_SPLIT_RATIO : random.nextDouble();
}

private GBPTree<KEY,VALUE> createIndex()
throws IOException
{
Expand All @@ -69,11 +77,15 @@ private GBPTree<KEY,VALUE> createIndex()
return index = new GBPTreeBuilder<>( pageCache, directory.file( "index" ), layout ).build();
}

private Writer<KEY,VALUE> createWriter( GBPTree<KEY,VALUE> index ) throws IOException
{
return index.writer( ratioToKeepInLeftOnSplit );
}

abstract TestLayout<KEY,VALUE> getLayout( RandomRule random );

abstract Class<KEY> getKeyClass();

// todo randomize this test to use different split ratios
@Test
public void shouldStayCorrectAfterRandomModifications() throws Exception
{
Expand All @@ -90,7 +102,7 @@ public void shouldStayCorrectAfterRandomModifications() throws Exception
}

// WHEN
try ( Writer<KEY,VALUE> writer = index.writer() )
try ( Writer<KEY,VALUE> writer = createWriter( index ) )
{
for ( Map.Entry<KEY,VALUE> entry : data.entrySet() )
{
Expand Down Expand Up @@ -158,7 +170,7 @@ public void shouldHandleRemoveEntireTree() throws Exception
try ( GBPTree<KEY,VALUE> index = createIndex() )
{
int numberOfNodes = 200_000;
try ( Writer<KEY,VALUE> writer = index.writer() )
try ( Writer<KEY,VALUE> writer = createWriter( index ) )
{
for ( int i = 0; i < numberOfNodes; i++ )
{
Expand All @@ -168,7 +180,7 @@ public void shouldHandleRemoveEntireTree() throws Exception

// when
BitSet removed = new BitSet();
try ( Writer<KEY,VALUE> writer = index.writer() )
try ( Writer<KEY,VALUE> writer = createWriter( index ) )
{
for ( int i = 0; i < numberOfNodes - numberOfNodes / 10; i++ )
{
Expand All @@ -185,7 +197,7 @@ public void shouldHandleRemoveEntireTree() throws Exception
}

int next = 0;
try ( Writer<KEY,VALUE> writer = index.writer() )
try ( Writer<KEY,VALUE> writer = createWriter( index ) )
{
for ( int i = 0; i < numberOfNodes / 10; i++ )
{
Expand Down Expand Up @@ -214,7 +226,7 @@ public void shouldHandleDescendingWithEmptyRange() throws IOException
try ( GBPTree<KEY,VALUE> index = createIndex() )
{
// Write
try ( Writer<KEY, VALUE> writer = index.writer() )
try ( Writer<KEY, VALUE> writer = createWriter( index ) )
{
for ( long seed : seeds )
{
Expand All @@ -238,7 +250,7 @@ private void randomlyModifyIndex( GBPTree<KEY,VALUE> index, Map<KEY,VALUE> data,
throws IOException
{
int changeCount = random.nextInt( 10 ) + 10;
try ( Writer<KEY,VALUE> writer = index.writer() )
try ( Writer<KEY,VALUE> writer = createWriter( index ) )
{
for ( int i = 0; i < changeCount; i++ )
{
Expand Down

0 comments on commit 38e3e77

Please sign in to comment.