public PositionAwarePhysicalFlushableChannel( LogVersionedStoreChannel logVersionedStoreChannel ) { this.logVersionedStoreChannel = logVersionedStoreChannel; this.channel = new PhysicalFlushableChannel( logVersionedStoreChannel ); }
@Override public void close() throws IOException { channel.close(); }
public void setCurrentPosition( LogPosition position ) throws IOException, UnsupportedOperationException { channel.position( position.getByteOffset() ); } }
PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel( storeChannel, 1, (byte) -1 /* ignored */ ); PhysicalFlushableChannel channel = new PhysicalFlushableChannel( versionedStoreChannel ); byte[] byteArrayValue = new byte[] {1,4,2,5,3,6}; channel.put( byteValue ); channel.putShort( shortValue ); channel.putInt( intValue ); channel.putLong( longValue ); channel.prepareForFlush().flush(); channel.close(); channel.setChannel( new PhysicalLogVersionedStoreChannel( storeChannel, 2, (byte) -1 /* ignored */ ) ); channel.putFloat( floatValue ); channel.putDouble( doubleValue ); channel.put( byteArrayValue, byteArrayValue.length ); channel.close();
@Test public void shouldThrowIllegalStateExceptionAfterClosed() throws Exception { // GIVEN final File file = new File( directory.directory(), "file" ); StoreChannel storeChannel = fileSystemRule.get().open( file, OpenMode.READ_WRITE ); PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel( storeChannel, 1, (byte) -1 /* ignored */ ); PhysicalFlushableChannel channel = new PhysicalFlushableChannel( versionedStoreChannel ); // closing the WritableLogChannel, then the underlying channel is what PhysicalLogFile does channel.close(); storeChannel.close(); // WHEN just appending something to the buffer channel.put( (byte) 0 ); // and wanting to empty that into the channel try { channel.prepareForFlush(); fail( "Should have thrown exception" ); } catch ( IllegalStateException e ) { // THEN we should get an IllegalStateException, not a ClosedChannelException } }
@Test public void shouldThrowClosedChannelExceptionWhenChannelUnexpectedlyClosed() throws Exception { // GIVEN final File file = new File( directory.directory(), "file" ); StoreChannel storeChannel = fileSystemRule.get().open( file, OpenMode.READ_WRITE ); PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel( storeChannel, 1, (byte) -1 /* ignored */ ); PhysicalFlushableChannel channel = new PhysicalFlushableChannel( versionedStoreChannel ); // just close the underlying channel storeChannel.close(); // WHEN just appending something to the buffer channel.put( (byte) 0 ); // and wanting to empty that into the channel try { channel.prepareForFlush(); fail( "Should have thrown exception" ); } catch ( ClosedChannelException e ) { // THEN we should get a ClosedChannelException } }
@Override public Flushable prepareForFlush() throws IOException { return channel.prepareForFlush(); }
private static void writeHeader( FileSystemAbstraction fileSystem, File file, SegmentHeader header ) throws IOException { try ( StoreChannel channel = fileSystem.open( file, OpenMode.READ_WRITE ) ) { channel.position( 0 ); PhysicalFlushableChannel writer = new PhysicalFlushableChannel( channel, SegmentHeader.SIZE ); headerMarshal.marshal( header, writer ); writer.prepareForFlush().flush(); } }
@Test public void arraysShouldCalculateCorrectLength() throws IOException { // given int[] value = new int[3]; for ( int i = 0; i < value.length; i++ ) { value[i] = 100 + i; } ValueType valueType = ValueType.typeOf( value ); PhysicalFlushableChannel channel = new PhysicalFlushableChannel( fs.open( directory.file( "file" ), OpenMode.READ_WRITE ) ); // when int length = valueType.length( value ); valueType.write( value, channel ); // then int expected = 1 + // component type Integer.BYTES + // array length value.length * Integer.BYTES; // array data assertEquals( expected, length ); assertEquals( expected, channel.position() ); } }
@Override public FlushableChannel put( byte value ) throws IOException { return channel.put( value ); }
@Override public FlushableChannel putInt( int value ) throws IOException { return channel.putInt( value ); }
@Override public FlushableChannel putLong( long value ) throws IOException { return channel.putLong( value ); }
@Override public FlushableChannel putDouble( double value ) throws IOException { return channel.putDouble( value ); }
@Override public FlushableChannel putShort( short value ) throws IOException { return channel.putShort( value ); }
@Override public FlushableChannel putFloat( float value ) throws IOException { return channel.putFloat( value ); }
private ByteBuffer bufferWithGuaranteedSpace( int spaceInBytes ) throws IOException { assert spaceInBytes < buffer.capacity(); if ( buffer.remaining() < spaceInBytes ) { prepareForFlush(); } return buffer; }
private static void writeHeader( FileSystemAbstraction fileSystem, File file, SegmentHeader header ) throws IOException { try ( StoreChannel channel = fileSystem.open( file, "rw" ) ) { channel.position( 0 ); PhysicalFlushableChannel writer = new PhysicalFlushableChannel( channel, SegmentHeader.SIZE ); headerMarshal.marshal( header, writer ); writer.prepareForFlush().flush(); } }
@Override public FlushableChannel put( byte[] value, int length ) throws IOException { return channel.put( value, length ); }
@Override public FlushableChannel putInt( int value ) throws IOException { return channel.putInt( value ); }
@Override public FlushableChannel putLong( long value ) throws IOException { return channel.putLong( value ); }