Skip to content

Commit

Permalink
PageCursor#putBytes( int bytes, byte value )
Browse files Browse the repository at this point in the history
To make it possible to fill part of page with same byte
without creating an array. Similar to zapPage.
  • Loading branch information
burqen committed Jan 16, 2018
1 parent 63d1172 commit 6f29757
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 8 deletions.
Expand Up @@ -260,6 +260,12 @@ public void putBytes( byte[] data, int arrayOffset, int length )
current.putBytes( data, arrayOffset, length ); current.putBytes( data, arrayOffset, length );
} }


@Override
public void putBytes( int bytes, byte value )
{
current.putBytes( bytes, value );
}

@Override @Override
public short getShort() public short getShort()
{ {
Expand Down
Expand Up @@ -159,6 +159,10 @@ public abstract class PageCursor implements AutoCloseable
*/ */
public abstract void putBytes( byte[] data, int arrayOffset, int length ); public abstract void putBytes( byte[] data, int arrayOffset, int length );


/**
* Set the given number of bytes to the given value, beginning at current offset into the page.
*/
public abstract void putBytes( int bytes, byte value );
/** /**
* Get the signed short at the current page offset, and then increment the offset by one. * Get the signed short at the current page offset, and then increment the offset by one.
*/ */
Expand Down
Expand Up @@ -324,6 +324,12 @@ public void putBytes( byte[] data, int arrayOffset, int length )
throw new UnsupportedOperationException( "Composite page cursor does not yet support this operation" ); throw new UnsupportedOperationException( "Composite page cursor does not yet support this operation" );
} }


@Override
public void putBytes( int bytes, byte value )
{
throw new UnsupportedOperationException( "Composite page cursor does not yet support this operation" );
}

@Override @Override
public short getShort() public short getShort()
{ {
Expand Down
Expand Up @@ -140,6 +140,12 @@ public void putBytes( byte[] data, int arrayOffset, int length )
delegate.putBytes( data, arrayOffset, length ); delegate.putBytes( data, arrayOffset, length );
} }


@Override
public void putBytes( int bytes, byte value )
{
delegate.putBytes( bytes, value );
}

@Override @Override
public void rewind() public void rewind()
{ {
Expand Down
Expand Up @@ -686,6 +686,17 @@ public void putBytes( byte[] data, int arrayOffset, int length )
offset += length; offset += length;
} }


@Override
public void putBytes( int bytes, byte value )
{
long p = getBoundedPointer( offset, bytes );
if ( !outOfBounds )
{
UnsafeUtil.setMemory( p, bytes, value );
}
offset += bytes;
}

@Override @Override
public final short getShort() public final short getShort()
{ {
Expand Down
Expand Up @@ -159,6 +159,14 @@ public void putBytes( byte[] data, int arrayOffset, int length )
buffer.put( data, arrayOffset, length ); buffer.put( data, arrayOffset, length );
} }


@Override
public void putBytes( int bytes, byte value )
{
byte[] byteArray = new byte[bytes];
Arrays.fill( byteArray, value );
buffer.put( byteArray );
}

@Override @Override
public short getShort() public short getShort()
{ {
Expand Down
Expand Up @@ -1696,7 +1696,7 @@ public void pagesAddedWithNextWithPageIdMustBeAccessibleWithNoGrowSpecified() th
public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception
{ {
configureStandardPageCache(); configureStandardPageCache();
try ( PagedFile pagedFile = pageCache.map( file( "a" ), 20 ) ) try ( PagedFile pagedFile = pageCache.map( file( "a" ), 23 ) )
{ {
try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) )
{ {
Expand All @@ -1708,6 +1708,7 @@ public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception
cursor.putShort( (short) 41 ); // 12+2 = 14 cursor.putShort( (short) 41 ); // 12+2 = 14
cursor.putByte( (byte) 41 ); // 14+1 = 15 cursor.putByte( (byte) 41 ); // 14+1 = 15
cursor.putBytes( data ); // 15+5 = 20 cursor.putBytes( data ); // 15+5 = 20
cursor.putBytes( 3, (byte) 47 ); // 20+3 = 23
} }
try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) ) try ( PageCursor cursor = pagedFile.io( 0, PF_SHARED_WRITE_LOCK ) )
{ {
Expand All @@ -1723,19 +1724,25 @@ public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception
cursor.getByte(), // 19 cursor.getByte(), // 19
cursor.getByte() // 20 cursor.getByte() // 20
}; };
byte d = cursor.getByte(); // 21
byte e = cursor.getByte(); // 22
byte f = cursor.getByte(); // 23
cursor.setOffset( 0 ); cursor.setOffset( 0 );
cursor.putLong( 1 + a ); cursor.putLong( 1 + a );
cursor.putInt( 1 + b ); cursor.putInt( 1 + b );
cursor.putShort( (short) (1 + c) ); cursor.putShort( (short) (1 + c) );
for ( byte d : data ) for ( byte g : data )
{ {
d++; g++;
cursor.putByte( d ); cursor.putByte( g );
} }
cursor.putByte( (byte) (1 + d) );
cursor.putByte( (byte) (1 + e) );
cursor.putByte( (byte) (1 + f) );
} }
} }


ByteBuffer buf = ByteBuffer.allocate( 20 ); ByteBuffer buf = ByteBuffer.allocate( 23 );
try ( StoreChannel channel = fs.open( file( "a" ), OpenMode.READ ) ) try ( StoreChannel channel = fs.open( file( "a" ), OpenMode.READ ) )
{ {
channel.readAll( buf ); channel.readAll( buf );
Expand All @@ -1751,6 +1758,9 @@ public void writesOfDifferentUnitsMustHaveCorrectEndianess() throws Exception
assertThat( buf.get(), is( (byte) 45 ) ); assertThat( buf.get(), is( (byte) 45 ) );
assertThat( buf.get(), is( (byte) 46 ) ); assertThat( buf.get(), is( (byte) 46 ) );
assertThat( buf.get(), is( (byte) 47 ) ); assertThat( buf.get(), is( (byte) 47 ) );
assertThat( buf.get(), is( (byte) 48 ) );
assertThat( buf.get(), is( (byte) 48 ) );
assertThat( buf.get(), is( (byte) 48 ) );
} }


@Test( timeout = SHORT_TIMEOUT_MILLIS ) @Test( timeout = SHORT_TIMEOUT_MILLIS )
Expand Down Expand Up @@ -2469,6 +2479,8 @@ private void verifyWriteOffsets( PageCursor cursor )
assertThat( cursor.getOffset(), is( 18 ) ); assertThat( cursor.getOffset(), is( 18 ) );
cursor.putBytes( new byte[]{1, 2, 3}, 1, 1 ); cursor.putBytes( new byte[]{1, 2, 3}, 1, 1 );
assertThat( cursor.getOffset(), is( 19 ) ); assertThat( cursor.getOffset(), is( 19 ) );
cursor.putBytes( 5, (byte)1 );
assertThat( cursor.getOffset(), is( 24 ) );
} }


private void verifyReadOffsets( PageCursor cursor ) private void verifyReadOffsets( PageCursor cursor )
Expand All @@ -2486,16 +2498,19 @@ private void verifyReadOffsets( PageCursor cursor )
assertThat( cursor.getOffset(), is( 18 ) ); assertThat( cursor.getOffset(), is( 18 ) );
cursor.getBytes( new byte[3], 1, 1 ); cursor.getBytes( new byte[3], 1, 1 );
assertThat( cursor.getOffset(), is( 19 ) ); assertThat( cursor.getOffset(), is( 19 ) );
cursor.getBytes( new byte[5] );
assertThat( cursor.getOffset(), is( 24 ) );


byte[] expectedBytes = new byte[] { byte[] expectedBytes = new byte[] {
0, 0, 0, 0, 0, 0, 0, 1, // first; long 0, 0, 0, 0, 0, 0, 0, 1, // first; long
0, 0, 0, 1, // second; int 0, 0, 0, 1, // second; int
0, 1, // third; short 0, 1, // third; short
1, // fourth; byte 1, // fourth; byte
1, 2, 3, // lastly; more bytes 1, 2, 3, // fifth; more bytes
2 2, // sixth; additional bytes
1, 1, 1, 1, 1, // lastly; more bytes
}; };
byte[] actualBytes = new byte[19]; byte[] actualBytes = new byte[24];
cursor.setOffset( 0 ); cursor.setOffset( 0 );
cursor.getBytes( actualBytes ); cursor.getBytes( actualBytes );
assertThat( actualBytes, byteArray( expectedBytes ) ); assertThat( actualBytes, byteArray( expectedBytes ) );
Expand Down Expand Up @@ -2973,6 +2988,12 @@ public void putBytesBeyondPageEndMustThrow() throws IOException
verifyPageBounds( cursor -> cursor.putBytes( bytes ) ); verifyPageBounds( cursor -> cursor.putBytes( bytes ) );
} }


@Test( timeout = SHORT_TIMEOUT_MILLIS )
public void putBytesRepeatedByteBeyondPageEndMustThrow() throws IOException
{
verifyPageBounds( cursor -> cursor.putBytes( 3, (byte) 1 ) );
}

@Test( timeout = SHORT_TIMEOUT_MILLIS ) @Test( timeout = SHORT_TIMEOUT_MILLIS )
public void getBytesBeyondPageEndMustThrow() throws IOException public void getBytesBeyondPageEndMustThrow() throws IOException
{ {
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.nio.BufferOverflowException; import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException; import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;


/** /**
Expand Down Expand Up @@ -343,6 +344,14 @@ public void putBytes( byte[] data, int arrayOffset, int length )
} }
} }


@Override
public void putBytes( int bytes, byte value )
{
byte[] byteArray = new byte[bytes];
Arrays.fill( byteArray, value );
putBytes( byteArray, 0, bytes );
}

@Override @Override
public short getShort() public short getShort()
{ {
Expand Down

0 comments on commit 6f29757

Please sign in to comment.