@Override public Flushable prepareForFlush() throws IOException { return channel.prepareForFlush(); }
private ByteBuffer bufferWithGuaranteedSpace( int spaceInBytes ) throws IOException { assert spaceInBytes < buffer.capacity(); if ( buffer.remaining() < spaceInBytes ) { prepareForFlush(); } return buffer; }
/** * External synchronization between this method and emptyBufferIntoChannelAndClearIt is required so that they * aren't called concurrently. Currently that's done by acquiring the PhysicalLogFile monitor. */ @Override public void close() throws IOException { prepareForFlush().flush(); closed = true; channel.close(); }
/** * Sets position of this channel to the new {@code position}. This works only if the underlying channel * supports positioning. * * @param position new position (byte offset) to set as new current position. * @throws IOException if underlying channel throws {@link IOException}. */ public void position( long position ) throws IOException { // Currently we take the pessimistic approach of flushing (doesn't imply forcing) buffer to // channel before moving to a new position. This works in all cases, but there could be // made an optimization where we could see that we're moving within the current buffer range // and if so skip flushing and simply move the cursor in the buffer. prepareForFlush().flush(); channel.position( position ); } }
@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 } }
channel.putInt( intValue ); channel.putLong( longValue ); channel.prepareForFlush().flush(); 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 } }
@Override public Flushable prepareForFlush() throws IOException { return channel.prepareForFlush(); }
synchronized void flush() throws IOException { bufferedWriter.prepareForFlush().flush(); }
synchronized void flush() throws IOException { bufferedWriter.prepareForFlush().flush(); }
private ByteBuffer bufferWithGuaranteedSpace( int spaceInBytes ) throws IOException { assert spaceInBytes < buffer.capacity(); if ( buffer.remaining() < spaceInBytes ) { prepareForFlush(); } return buffer; }
/** * External synchronization between this method and emptyBufferIntoChannelAndClearIt is required so that they * aren't called concurrently. Currently that's done by acquiring the PhysicalLogFile monitor. */ @Override public void close() throws IOException { prepareForFlush().flush(); closed = true; channel.close(); }
/** * Sets position of this channel to the new {@code position}. This works only if the underlying channel * supports positioning. * * @param position new position (byte offset) to set as new current position. * @throws IOException if underlying channel throws {@link IOException}. */ public void position( long position ) throws IOException { // Currently we take the pessimistic approach of flushing (doesn't imply forcing) buffer to // channel before moving to a new position. This works in all cases, but there could be // made an optimization where we could see that we're moving within the current buffer range // and if so skip flushing and simply move the cursor in the buffer. prepareForFlush().flush(); channel.position( position ); } }
@Override public synchronized void persistStoreData( STATE state ) throws IOException { if ( numberOfEntriesWrittenInActiveFile >= numberOfEntriesBeforeRotation ) { switchStoreFile(); numberOfEntriesWrittenInActiveFile = 0; } marshal.marshal( state, currentStoreChannel ); currentStoreChannel.prepareForFlush().flush(); numberOfEntriesWrittenInActiveFile++; }
@Override public synchronized void persistStoreData( STATE state ) throws IOException { if ( numberOfEntriesWrittenInActiveFile >= numberOfEntriesBeforeRotation ) { switchStoreFile(); numberOfEntriesWrittenInActiveFile = 0; } marshal.marshal( state, currentStoreChannel ); currentStoreChannel.prepareForFlush().flush(); numberOfEntriesWrittenInActiveFile++; }
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(); } }
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(); } }