header != null ? header.toString() : "null header." ) ); return new PhysicalLogVersionedStoreChannel( rawChannel, version, header.logFormatVersion );
@Test public void shouldOpenTheNextChannelWhenItExists() throws IOException { // given final StoreChannel newStoreChannel = mock( StoreChannel.class ); final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge( logFiles ); when( channel.getVersion() ).thenReturn( version ); when( channel.getLogFormatVersion() ).thenReturn( CURRENT_LOG_VERSION ); when( fs.fileExists( any( File.class ) ) ).thenReturn( true ); when( fs.open( any( File.class ), eq( OpenMode.READ ) ) ).thenReturn( newStoreChannel ); when( newStoreChannel.read( ArgumentMatchers.<ByteBuffer>any() ) ).then( invocationOnMock -> { ByteBuffer buffer = invocationOnMock.getArgument( 0 ); buffer.putLong( encodeLogVersion( version + 1 ) ); buffer.putLong( 42 ); return LOG_HEADER_SIZE; } ); // when final LogVersionedStoreChannel result = bridge.next( channel ); // then PhysicalLogVersionedStoreChannel expected = new PhysicalLogVersionedStoreChannel( newStoreChannel, version + 1, CURRENT_LOG_VERSION ); assertEquals( expected, result ); verify( channel, times( 1 ) ).close(); }
@Override public LogVersionedStoreChannel next( LogVersionedStoreChannel channel ) throws IOException { if ( !returned ) { returned = true; channel.close(); return new PhysicalLogVersionedStoreChannel( fileSystemRule.get().open( file( 1 ), OpenMode.READ ), -1 /* ignored */, (byte) -1 /* ignored */ ); } return channel; } }, 10 ) )
return new PhysicalLogVersionedStoreChannel( storeChannel, forVersion, formatVersion );
public static List<LogEntry> logEntries( FileSystemAbstraction fileSystem, String logPath ) throws IOException { File logFile = new File( logPath ); StoreChannel fileChannel = fileSystem.open( logFile, OpenMode.READ ); // Always a header LogHeader header = readLogHeader( ByteBuffer.allocate( LOG_HEADER_SIZE ), fileChannel, true, logFile ); // Read all log entries PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel( fileChannel, header.logVersion, header.logFormatVersion ); ReadableLogChannel logChannel = new ReadAheadLogChannel( versionedStoreChannel ); LogEntryCursor logEntryCursor = new LogEntryCursor( new VersionAwareLogEntryReader<>(), logChannel ); return Iterables.asList( new IOCursorAsResourceIterable<>( logEntryCursor ) ); }
static void filterTransactionLogFile( FileSystemAbstraction fileSystem, File file, final LogHook<LogEntry> filter ) throws IOException { filter.file( file ); try ( StoreChannel in = fileSystem.open( file, OpenMode.READ ) ) { LogHeader logHeader = readLogHeader( ByteBuffer.allocate( LOG_HEADER_SIZE ), in, true, file ); PhysicalLogVersionedStoreChannel inChannel = new PhysicalLogVersionedStoreChannel( in, logHeader.logVersion, logHeader.logFormatVersion ); ReadableLogChannel inBuffer = new ReadAheadLogChannel( inChannel ); LogEntryReader<ReadableLogChannel> entryReader = new VersionAwareLogEntryReader<>(); LogEntry entry; while ( (entry = entryReader.readLogEntry( inBuffer )) != null ) { filter.test( entry ); } } } }
StoreChannel storeChannel = fileSystemRule.get().open( firstFile, OpenMode.READ_WRITE ); PhysicalLogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel( storeChannel, 1, (byte) -1 /* ignored */ ); PhysicalFlushableChannel channel = new PhysicalFlushableChannel( versionedStoreChannel ); channel.setChannel( new PhysicalLogVersionedStoreChannel( storeChannel, 2, (byte) -1 /* ignored */ ) ); channel.putFloat( floatValue ); channel.putDouble( doubleValue );
private void writeSomeData( File file, Visitor<Pair<LogEntryWriter,Consumer<LogPositionMarker>>,IOException> visitor ) throws IOException { try ( LogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel( fileSystemRule.get().open( file, OpenMode.READ_WRITE ), logVersion, CURRENT_LOG_VERSION ); PositionAwarePhysicalFlushableChannel writableLogChannel = new PositionAwarePhysicalFlushableChannel( versionedStoreChannel ) ) { writeLogHeader( writableLogChannel, logVersion, 2L ); Consumer<LogPositionMarker> consumer = marker -> { try { writableLogChannel.getCurrentPosition( marker ); } catch ( IOException e ) { throw new RuntimeException( e ); } }; LogEntryWriter first = new LogEntryWriter( writableLogChannel ); visitor.visit( Pair.of( first, consumer ) ); } } }
@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 } }
new PhysicalLogVersionedStoreChannel( storeChannel, -1 /* ignored */, (byte) -1 /* ignored */ ); try ( ReadAheadLogChannel channel = new ReadAheadLogChannel( versionedStoreChannel, NO_MORE_CHANNELS, 16 ) )
@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 } }
new PhysicalLogVersionedStoreChannel( storeChannel, -1 /* ignored */, (byte) -1 /* ignored */ ); try ( ReadAheadLogChannel channel = new ReadAheadLogChannel( versionedStoreChannel, new LogVersionBridge()
@Test public void shouldSeeCorrectPositionEvenBeforeEmptyingDataIntoChannel() 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 */ ); PositionAwarePhysicalFlushableChannel channel = new PositionAwarePhysicalFlushableChannel( versionedStoreChannel ); LogPositionMarker positionMarker = new LogPositionMarker(); LogPosition initialPosition = channel.getCurrentPosition( positionMarker ).newPosition(); // WHEN channel.putLong( 67 ); channel.putInt( 1234 ); LogPosition positionAfterSomeData = channel.getCurrentPosition( positionMarker ).newPosition(); // THEN assertEquals( 12, positionAfterSomeData.getByteOffset() - initialPosition.getByteOffset() ); channel.close(); }
header != null ? header.toString() : "null header." ) ); return new PhysicalLogVersionedStoreChannel( rawChannel, version, header.logFormatVersion );
return new PhysicalLogVersionedStoreChannel( storeChannel, forVersion, formatVersion );