Skip to content

Commit

Permalink
No explicit cursor offset tracking during reference decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
lutovich committed Mar 24, 2016
1 parent 84d1176 commit 9805b31
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public void put( byte oneByte, PageCursor source )
// Take one copy here since Enum#values() does an unnecessary defensive copy every time.
private static final Reference[] ENCODINGS = Reference.values();

static final int MAX_BITS = 58;

private final int numberOfBytes;
private final short highHeader;
private final int headerShift;
Expand Down Expand Up @@ -189,23 +191,20 @@ public static <SOURCE> long decode( SOURCE source, DataAdapter<SOURCE> adapter )
{
PageCursor cursor = (PageCursor) source;

int offset = cursor.getOffset();
int header = cursor.getByte( offset ) & 0xFF;
int header = cursor.getByte() & 0xFF;
int sizeMarks = Integer.numberOfLeadingZeros( (~(header & 0xF8)) & 0xFF ) - 24;
int signShift = 8 - sizeMarks - (sizeMarks == 5 ? 1 : 2);
long signBit = ~((header >>> signShift) & 1) + 1;
long register = (header & ((1 << signShift) - 1)) << 16;
register += cursor.getShort( ++offset ) & 0xFFFFL; // 3 bytes
offset++;
register += cursor.getShort() & 0xFFFFL; // 3 bytes

while ( sizeMarks > 0 )
{
register <<= 8;
register += cursor.getByte( ++offset ) & 0xFF;
register += cursor.getByte() & 0xFF;
sizeMarks--;
}

cursor.setOffset( offset + 1 );
return signBit ^ register;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@
import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.neo4j.io.pagecache.StubPageCursor;
import org.neo4j.kernel.impl.store.format.highlimit.Reference;
import org.neo4j.test.RandomRule;

import static org.junit.Assert.assertEquals;

import static org.neo4j.kernel.impl.store.format.highlimit.Reference.PAGE_CURSOR_ADAPTER;

public class ReferenceTest
{
/**
* The current scheme only allows us to use 58 bits for a reference. Adhere to that limit here.
*/
private static final long MASK = numberOfBits( Reference.MAX_BITS );
private static final int PAGE_SIZE = 100;

@Rule
public final RandomRule random = new RandomRule();
private final ByteBuffer buffer = ByteBuffer.allocateDirect( 100 );
private final StubPageCursor cursor = new StubPageCursor( 0, buffer );
private final StubPageCursor cursor = new StubPageCursor( 0, PAGE_SIZE );

@Test
public void shouldEncodeRandomLongs() throws Exception
{
// WHEN/THEN
long mask = numberOfBits( 58 );
for ( int i = 0; i < 100_000_000; i++ )
{
long reference = limit( random.nextLong(), mask );
long reference = limit( random.nextLong() );
assertDecodedMatchesEncoded( reference );
}
}
Expand All @@ -65,7 +65,7 @@ public void relativeReferenceConvertion()
assertEquals( "Converted reference should be equal to initial value", absoluteReference, absoluteCandidate );
}

private long numberOfBits( int count )
private static long numberOfBits( int count )
{
long result = 0;
for ( int i = 0; i < count; i++ )
Expand All @@ -75,10 +75,7 @@ private long numberOfBits( int count )
return result;
}

/**
* The current scheme only allows us to use 58 bits for a reference. Adhere to that limit here.
*/
private long limit( long reference, long mask )
private static long limit( long reference )
{
boolean positive = true;
if ( reference < 0 )
Expand All @@ -87,7 +84,7 @@ private long limit( long reference, long mask )
reference = ~reference;
}

reference &= mask;
reference &= MASK;

if ( !positive )
{
Expand Down

0 comments on commit 9805b31

Please sign in to comment.