@Override public long getVersion() { return channel.getVersion(); }
@Override public LogPositionMarker getCurrentPosition( LogPositionMarker positionMarker ) throws IOException { positionMarker.mark( logVersionedStoreChannel.getVersion(), channel.position() ); return positionMarker; }
@Override public LogPositionMarker getCurrentPosition( LogPositionMarker positionMarker ) throws IOException { positionMarker.mark( channel.getVersion(), position() ); return positionMarker; }
@Override public LogVersionedStoreChannel next( LogVersionedStoreChannel channel ) throws IOException { PhysicalLogVersionedStoreChannel nextChannel; try { nextChannel = logFiles.openForVersion( channel.getVersion() + 1 ); } catch ( FileNotFoundException | IncompleteLogHeaderException e ) { // See PhysicalLogFile#rotate() for description as to why these exceptions are OK return channel; } channel.close(); return nextChannel; } }
@Test public void shouldReturnOldChannelWhenThereIsNoNextChannel() throws IOException { // given final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge( logFiles ); when( channel.getVersion() ).thenReturn( version ); when( fs.open( any( File.class ), eq( OpenMode.READ ) ) ).thenThrow( new FileNotFoundException() ); // when final LogVersionedStoreChannel result = bridge.next( channel ); // then assertEquals( channel, result ); verify( channel, never() ).close(); }
@Test public void shouldReturnOldChannelWhenNextChannelHasntGottenCompleteHeaderYet() throws Exception { // given final ReaderLogVersionBridge bridge = new ReaderLogVersionBridge( logFiles ); final StoreChannel nextVersionWithIncompleteHeader = mock( StoreChannel.class ); when( nextVersionWithIncompleteHeader.read( any( ByteBuffer.class ) ) ).thenReturn( LOG_HEADER_SIZE / 2 ); when( channel.getVersion() ).thenReturn( version ); when( fs.fileExists( any( File.class ) ) ).thenReturn( true ); when( fs.open( any( File.class ), eq( OpenMode.READ ) ) ).thenReturn( nextVersionWithIncompleteHeader ); // when final LogVersionedStoreChannel result = bridge.next( channel ); // then assertEquals( channel, result ); verify( channel, never() ).close(); }
@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 long getVersion() { return channel.getVersion(); }
@Override public LogPositionMarker getCurrentPosition( LogPositionMarker positionMarker ) throws IOException { positionMarker.mark( channel.getVersion(), position() ); return positionMarker; }
@Override public LogPositionMarker getCurrentPosition( LogPositionMarker positionMarker ) throws IOException { positionMarker.mark( logVersionedStoreChannel.getVersion(), channel.position() ); return positionMarker; }
@Override public LogVersionedStoreChannel next( LogVersionedStoreChannel channel ) throws IOException { PhysicalLogVersionedStoreChannel nextChannel; try { nextChannel = logFiles.openForVersion( channel.getVersion() + 1 ); } catch ( FileNotFoundException | IncompleteLogHeaderException e ) { // See PhysicalLogFile#rotate() for description as to why these exceptions are OK return channel; } channel.close(); return nextChannel; } }