Skip to content

Commit

Permalink
TestLayout is responsible for seeding key and value
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Jan 16, 2018
1 parent fc8b874 commit a840f14
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 55 deletions.
Expand Up @@ -63,7 +63,7 @@ public class InternalTreeLogicTest
private final int pageSize = 256;

private final SimpleIdProvider id = new SimpleIdProvider();
private final Layout<MutableLong,MutableLong> layout = new SimpleLongLayout();
private final TestLayout<MutableLong,MutableLong> layout = new SimpleLongLayout();
private final TreeNode<MutableLong,MutableLong> node = new TreeNodeFixedSize<>( pageSize, layout );
private final InternalTreeLogic<MutableLong,MutableLong> treeLogic = new InternalTreeLogic<>( id, node, layout );

Expand Down Expand Up @@ -1587,21 +1587,17 @@ private void printTree() throws IOException

private MutableLong key( long seed )
{
MutableLong key = layout.newKey();
key.setValue( seed );
return key;
return layout.key( seed );
}

private MutableLong value( long seed )
{
MutableLong value = layout.newValue();
value.setValue( seed );
return value;
return layout.value( seed );
}

private long getSeed( MutableLong key )
{
return key.getValue();
return layout.getSeed( key );
}

private void newRootFromSplit( StructurePropagation<MutableLong> split ) throws IOException
Expand Down
@@ -0,0 +1,10 @@
package org.neo4j.index.internal.gbptree;

interface KeyValueSeeder<KEY, VALUE>
{
KEY key( long seed );

VALUE value( long seed );

long getSeed( KEY key );
}
Expand Up @@ -19,9 +19,11 @@
*/
package org.neo4j.index.internal.gbptree;

import java.nio.ByteBuffer;

import org.neo4j.io.pagecache.PageCursor;

public class SimpleByteArrayLayout extends Layout.Adapter<RawBytes,RawBytes>
public class SimpleByteArrayLayout extends TestLayout<RawBytes,RawBytes>
{
@Override
public RawBytes newKey()
Expand Down Expand Up @@ -112,10 +114,11 @@ public int minorVersion()
@Override
public int compare( RawBytes o1, RawBytes o2 )
{
return byteArrayCompare( o1.bytes, o2.bytes );
int compare = Long.compare( getSeed( o1 ), getSeed( o2 ) );
return compare != 0 ? compare : byteArrayCompare( o1.bytes, o2.bytes, Long.BYTES );
}

private int byteArrayCompare( byte[] a, byte[] b )
private int byteArrayCompare( byte[] a, byte[] b, int fromPos )
{
assert a != null && b != null : "Null arrays not supported.";

Expand All @@ -125,7 +128,7 @@ private int byteArrayCompare( byte[] a, byte[] b )
}

int length = Math.min( a.length, b.length );
for ( int i = 0; i < length; i++ )
for ( int i = fromPos; i < length; i++ )
{
int compare = Byte.compare( a[i], b[i] );
if ( compare != 0 )
Expand All @@ -145,4 +148,29 @@ private int byteArrayCompare( byte[] a, byte[] b )
}
return 0;
}

@Override
public RawBytes key( long seed )
{
RawBytes key = newKey();
key.bytes = ByteBuffer.allocate( 16 ).putLong( seed ).putLong( seed ).array();
return key;
}

@Override
public RawBytes value( long seed )
{
RawBytes value = newValue();
value.bytes = ByteBuffer.allocate( 17 ).putLong( seed ).putLong( seed ).array();
return value;
}

@Override
public long getSeed( RawBytes rawBytes )
{
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put( rawBytes.bytes, 0, Long.BYTES );
buffer.flip();
return buffer.getLong();
}
}
Expand Up @@ -25,7 +25,7 @@

import static java.nio.charset.StandardCharsets.UTF_8;

class SimpleLongLayout extends Layout.Adapter<MutableLong,MutableLong>
class SimpleLongLayout extends TestLayout<MutableLong,MutableLong>
{
private final int keyPadding;
private String customNameAsMetaData;
Expand Down Expand Up @@ -180,4 +180,26 @@ private static String readString( PageCursor cursor )
cursor.getBytes( bytes );
return new String( bytes, UTF_8 );
}

@Override
public MutableLong key( long seed )
{
MutableLong key = newKey();
key.setValue( seed );
return key;
}

@Override
public MutableLong value( long seed )
{
MutableLong value = newValue();
value.setValue( seed );
return value;
}

@Override
public long getSeed( MutableLong key )
{
return key.getValue();
}
}
@@ -0,0 +1,5 @@
package org.neo4j.index.internal.gbptree;

public abstract class TestLayout<KEY,VALUE> extends Layout.Adapter<KEY,VALUE> implements KeyValueSeeder<KEY,VALUE>
{
}
@@ -1,7 +1,5 @@
package org.neo4j.index.internal.gbptree;

import java.nio.ByteBuffer;

import org.neo4j.io.pagecache.PageCursor;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -30,7 +28,7 @@ public class TreeNodeDynamicSizeTest extends TreeNodeTestBase<RawBytes,RawBytes>
private SimpleByteArrayLayout layout = new SimpleByteArrayLayout();

@Override
protected Layout<RawBytes,RawBytes> getLayout()
protected TestLayout<RawBytes,RawBytes> getLayout()
{
return layout;
}
Expand All @@ -41,22 +39,6 @@ protected TreeNode<RawBytes,RawBytes> getNode( int pageSize, Layout<RawBytes,Raw
return new TreeNodeDynamicSize<>( pageSize, layout );
}

@Override
protected RawBytes key( long sortOrder )
{
RawBytes key = layout.newKey();
key.bytes = ByteBuffer.allocate( 16 ).putLong( sortOrder ).putLong( sortOrder ).array();
return key;
}

@Override
protected RawBytes value( long someValue )
{
RawBytes value = layout.newValue();
value.bytes = ByteBuffer.allocate( 17 ).putLong( someValue ).putLong( someValue ).array();
return value;
}

@Override
boolean valuesEqual( RawBytes firstValue, RawBytes secondValue )
{
Expand Down
Expand Up @@ -28,7 +28,7 @@ public class TreeNodeFixedSizeTest extends TreeNodeTestBase<MutableLong,MutableL
private SimpleLongLayout layout = new SimpleLongLayout();

@Override
protected Layout<MutableLong,MutableLong> getLayout()
protected TestLayout<MutableLong,MutableLong> getLayout()
{
return layout;
}
Expand All @@ -39,22 +39,6 @@ protected TreeNode<MutableLong,MutableLong> getNode( int pageSize, Layout<Mutabl
return new TreeNodeFixedSize<>( pageSize, layout );
}

@Override
protected MutableLong key( long sortOrder )
{
MutableLong key = layout.newKey();
key.setValue( sortOrder );
return key;
}

@Override
protected MutableLong value( long someValue )
{
MutableLong value = layout.newValue();
value.setValue( someValue );
return value;
}

@Override
boolean valuesEqual( MutableLong firstValue, MutableLong secondValue )
{
Expand Down
Expand Up @@ -50,7 +50,7 @@ public abstract class TreeNodeTestBase<KEY,VALUE>
private static final int PAGE_SIZE = 512;
final PageCursor cursor = new PageAwareByteArrayCursor( PAGE_SIZE );

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

@Rule
Expand All @@ -64,18 +64,24 @@ public void prepareCursor() throws IOException
node = getNode( PAGE_SIZE, layout );
}

protected abstract Layout<KEY,VALUE> getLayout();
protected abstract TestLayout<KEY,VALUE> getLayout();

protected abstract TreeNode<KEY,VALUE> getNode( int pageSize, Layout<KEY,VALUE> layout );

protected abstract KEY key( long sortOrder );

protected abstract VALUE value( long someValue );

abstract boolean valuesEqual( VALUE firstValue, VALUE secondValue );

abstract void assertAdditionalHeader( PageCursor cursor, TreeNode<KEY,VALUE> node, int pageSize );

private KEY key( long seed )
{
return layout.key( seed );
};

private VALUE value( long seed )
{
return layout.value( seed );
}

@Test
public void shouldInitializeLeaf() throws Exception
{
Expand Down

0 comments on commit a840f14

Please sign in to comment.