Skip to content

Commit

Permalink
Inline Reference.DataAdapter.getShort
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Mar 31, 2016
1 parent 2784675 commit 5ad7f5b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 31 deletions.
Expand Up @@ -64,8 +64,6 @@ public interface DataAdapter<SOURCE>
{ {
byte getByte( SOURCE source ); byte getByte( SOURCE source );


short getShort( SOURCE source );

void putByte( byte oneByte, SOURCE source ) throws IOException; void putByte( byte oneByte, SOURCE source ) throws IOException;
} }


Expand All @@ -77,12 +75,6 @@ public byte getByte( PageCursor source )
return source.getByte(); return source.getByte();
} }


@Override
public short getShort( PageCursor cursor )
{
return cursor.getShort();
}

@Override @Override
public void putByte( byte oneByte, PageCursor source ) public void putByte( byte oneByte, PageCursor source )
{ {
Expand Down Expand Up @@ -254,8 +246,8 @@ public static <SOURCE> long decode( SOURCE source, DataAdapter<SOURCE> adapter )
// up 16 places to make room for the next two bytes of payload. // up 16 places to make room for the next two bytes of payload.
// //
// <6> // <6>
// Then we read the next two bytes (with unsigned mask) and save for the sign-bit manipulation, we now have a // Then we read the next two bytes (with unsigned mask) and save for the sign-component manipulation, we now
// complete 3-byte reference. // have a complete 3-byte reference.
// //
// <7> // <7>
// The size marks determines how many more bytes the reference takes up, so we loop through them and shift the // The size marks determines how many more bytes the reference takes up, so we loop through them and shift the
Expand All @@ -269,7 +261,7 @@ public static <SOURCE> long decode( SOURCE source, DataAdapter<SOURCE> adapter )
int signShift = 8 - sizeMarks - (sizeMarks == 5 ? 1 : 2); // <3> int signShift = 8 - sizeMarks - (sizeMarks == 5 ? 1 : 2); // <3>
long signComponent = ~((header >>> signShift) & 1) + 1; // <4> long signComponent = ~((header >>> signShift) & 1) + 1; // <4>
long register = (header & ((1 << signShift) - 1)) << 16; // <5> long register = (header & ((1 << signShift) - 1)) << 16; // <5>
register += adapter.getShort( source ) & 0xFFFFL; // <6> register += ((adapter.getByte( source ) & 0xFF) << 8) + (adapter.getByte( source ) & 0xFF); // <6>


while ( sizeMarks > 0 ) // <7> while ( sizeMarks > 0 ) // <7>
{ {
Expand Down
Expand Up @@ -71,14 +71,6 @@ public byte getByte( PageCursor primaryCursor /*same as the one we have*/ )
return primaryCursor.getByte(); return primaryCursor.getByte();
} }


@Override
public short getShort( PageCursor cursor )
{
byte highByte = getByte( cursor );
byte lowByte = getByte( cursor );
return (short) (((highByte & 0xFF) << 8) | (lowByte & 0xFF));
}

@Override @Override
public void putByte( byte oneByte, PageCursor primaryCursor ) public void putByte( byte oneByte, PageCursor primaryCursor )
{ {
Expand Down
Expand Up @@ -53,12 +53,6 @@ public byte getByte( PageCursor source )
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


@Override
public short getShort( PageCursor cursor )
{
throw new UnsupportedOperationException();
}

@Override @Override
public void putByte( byte oneByte, PageCursor cursor ) throws IOException public void putByte( byte oneByte, PageCursor cursor ) throws IOException
{ {
Expand Down
Expand Up @@ -42,7 +42,7 @@ public class SecondaryPageCursorReadDataAdapterTest
public final RandomRule random = new RandomRule(); public final RandomRule random = new RandomRule();


@Test @Test
public void getShortEntirelyFromPrimaryCursor() throws Exception public void getBytesEntirelyFromPrimaryCursor() throws Exception
{ {
int secondaryPageId = 42; int secondaryPageId = 42;
int secondaryCursorOffset = 0; int secondaryCursorOffset = 0;
Expand All @@ -58,13 +58,20 @@ public void getShortEntirelyFromPrimaryCursor() throws Exception
newPagedFile( secondaryCursor ), secondaryPageId, secondaryCursorOffset, primaryCursorEndOffset, newPagedFile( secondaryCursor ), secondaryPageId, secondaryCursorOffset, primaryCursorEndOffset,
PagedFile.PF_SHARED_READ_LOCK ); PagedFile.PF_SHARED_READ_LOCK );


short read = adapter.getShort( primaryCursor ); short read = getShort( adapter, primaryCursor );


assertEquals( value, read ); assertEquals( value, read );
} }


private short getShort( SecondaryPageCursorReadDataAdapter adapter, PageCursor cursor )
{
int a = adapter.getByte( cursor ) & 0xFF;
int b = adapter.getByte( cursor ) & 0xFF;
return (short) ((a << 8) + b);
}

@Test @Test
public void getShortEntirelyFromSecondaryCursor() throws Exception public void getBytesEntirelyFromSecondaryCursor() throws Exception
{ {
int secondaryPageId = 42; int secondaryPageId = 42;
int secondaryCursorOffset = 0; int secondaryCursorOffset = 0;
Expand All @@ -80,13 +87,13 @@ public void getShortEntirelyFromSecondaryCursor() throws Exception
newPagedFile( secondaryCursor ), secondaryPageId, secondaryCursorOffset, primaryCursorEndOffset, newPagedFile( secondaryCursor ), secondaryPageId, secondaryCursorOffset, primaryCursorEndOffset,
PagedFile.PF_SHARED_READ_LOCK ); PagedFile.PF_SHARED_READ_LOCK );


short read = adapter.getShort( primaryCursor ); short read = getShort( adapter, primaryCursor );


assertEquals( value, read ); assertEquals( value, read );
} }


@Test @Test
public void getShortFromPrimaryAndSecondaryCursor() throws Exception public void getBytesFromPrimaryAndSecondaryCursor() throws Exception
{ {
int secondaryPageId = 42; int secondaryPageId = 42;
int secondaryCursorOffset = 0; int secondaryCursorOffset = 0;
Expand All @@ -107,7 +114,7 @@ public void getShortFromPrimaryAndSecondaryCursor() throws Exception
newPagedFile( secondaryCursor ), secondaryPageId, secondaryCursorOffset, primaryCursorEndOffset, newPagedFile( secondaryCursor ), secondaryPageId, secondaryCursorOffset, primaryCursorEndOffset,
PagedFile.PF_SHARED_READ_LOCK ); PagedFile.PF_SHARED_READ_LOCK );


short read = adapter.getShort( primaryCursor ); short read = getShort( adapter, primaryCursor );


assertEquals( value, read ); assertEquals( value, read );
} }
Expand Down

0 comments on commit 5ad7f5b

Please sign in to comment.