@Override public long size() throws IOException { return inner.size(); }
@Override public long size() throws IOException { return delegateChannel.size(); }
@Override public long size() throws IOException { return delegate.size() - offset; }
static long countFreeIds( StoreChannel channel ) throws IOException { return channel.size() / ID_ENTRY_SIZE; }
/** * A keeper of freed IDs. * * @param channel a channel to the free ID file. * @param batchSize the number of IDs which are read/written to disk in one go. * @param aggressiveMode whether to reuse freed IDs during this lifecycle. * @throws IOException if an I/O error occurs. */ FreeIdKeeper( StoreChannel channel, int batchSize, boolean aggressiveMode ) throws IOException { this.channel = channel; this.batchSize = batchSize; this.aggressiveMode = aggressiveMode; this.initialPosition = channel.size(); this.stackPosition = initialPosition; this.freeIdCount = stackPosition / ID_ENTRY_SIZE; }
@Override public long size() throws IOException { return delegate.size(); }
@Override public int available() throws IOException { return (int) (position - channel.size()); }
private String readFailure( File failureFile ) throws IOException { try ( StoreChannel channel = fs.open( failureFile, OpenMode.READ ) ) { byte[] data = new byte[(int) channel.size()]; channel.readAll( ByteBuffer.wrap( data ) ); return UTF8.decode( withoutZeros( data ) ); } }
private boolean isFailed( File failureFile ) throws IOException { try ( StoreChannel channel = fs.open( failureFile, OpenMode.READ ) ) { byte[] data = new byte[(int) channel.size()]; channel.readAll( ByteBuffer.wrap( data ) ); channel.close(); return !allZero( data ); } }
public ChannelOutputStream( StoreChannel channel, boolean append ) throws IOException { this.channel = channel; if ( append ) { this.channel.position( this.channel.size() ); } }
@Override public long size() throws IOException { adversary.injectFailure( IOException.class ); return delegate.size(); }
@Test( expected = ClosedByInterruptException.class ) public void ch_size() throws IOException { chan( true ).size(); }
private static void verifyFileIsFullOfLongIntegerOnes( StoreChannel channel ) { try { long claimedSize = channel.size(); ByteBuffer buffer = allocate( (int) claimedSize ); channel.readAll( buffer ); buffer.flip(); for ( int position = 0; position < claimedSize; position += 8 ) { long value = buffer.getLong( position ); assertEquals( 1, value ); } } catch ( IOException e ) { throw new RuntimeException( e ); } }
private ByteBuffer readFile( File file ) throws IOException { try ( StoreChannel channel = fileSystemRule.get().open( file, OpenMode.READ ) ) { ByteBuffer buffer = ByteBuffer.allocate( (int) channel.size() ); channel.readAll( buffer ); buffer.flip(); return buffer; } }
/** * Store failure in failure file for index with the given id * * @param failure message describing the failure that needs to be stored * @throws IOException if the failure could not be stored */ public synchronized void storeIndexFailure( String failure ) throws IOException { File failureFile = failureFile(); try ( StoreChannel channel = fs.open( failureFile, OpenMode.READ_WRITE ) ) { byte[] existingData = new byte[(int) channel.size()]; channel.readAll( ByteBuffer.wrap( existingData ) ); channel.position( lengthOf( existingData ) ); byte[] data = UTF8.encode( failure ); channel.writeAll( ByteBuffer.wrap( data, 0, Math.min( data.length, MAX_FAILURE_SIZE ) ) ); channel.force( true ); channel.close(); } }
private void copyFile( File from, FileSystemAbstraction fromFs, File to, ByteBuffer buffer ) throws IOException { try ( StoreChannel source = fromFs.open( from, OpenMode.READ ); StoreChannel sink = this.open( to, OpenMode.READ_WRITE ) ) { sink.truncate( 0 ); for ( int available; (available = (int) (source.size() - source.position())) > 0; ) { buffer.clear(); buffer.limit( min( available, buffer.capacity() ) ); source.read( buffer ); buffer.flip(); sink.write( buffer ); } } }
public final void assertRecordsWrittenCorrectly( File file, StoreChannel channel ) throws IOException { int recordSize = getRecordSize(); long recordsInFile = channel.size() / recordSize; ByteBuffer buffer = ByteBuffer.allocate( recordSize ); StubPageCursor cursor = new StubPageCursor( 0, buffer ); for ( int i = 0; i < recordsInFile; i++ ) { assertThat( "reading record id " + i, channel.read( buffer ), is( recordSize ) ); buffer.flip(); Record expectedRecord = createRecord( file, i ); cursor.setOffset( 0 ); Record actualRecord = readRecord( cursor ); buffer.clear(); assertThat( actualRecord, isOneOf( expectedRecord, zeroRecord() ) ); } } }
@Test void shouldNotLoseDataForcedBeforeFileSystemCrashes() throws Exception { try ( EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction() ) { // given int numberOfBytesForced = 8; File aFile = new File( "yo" ); StoreChannel channel = fs.open( aFile, OpenMode.READ_WRITE ); writeLong( channel, 1111 ); // when channel.force( true ); writeLong( channel, 2222 ); fs.crash(); // then StoreChannel readChannel = fs.open( aFile, OpenMode.READ ); assertEquals( numberOfBytesForced, readChannel.size() ); assertEquals( 1111, readLong( readChannel ).getLong() ); } }
private void appendDropTransactionToTransactionLog( File databaseDirectory, CommittedTransactionRepresentation dropTransaction ) throws IOException { LogFiles logFiles = LogFilesBuilder.logFilesBasedOnlyBuilder( databaseDirectory, fs ).build(); File logFile = logFiles.getLogFileForVersion( logFiles.getHighestLogVersion() ); StoreChannel writeStoreChannel = fs.open( logFile, OpenMode.READ_WRITE ); writeStoreChannel.position( writeStoreChannel.size() ); try ( PhysicalFlushableChannel writeChannel = new PhysicalFlushableChannel( writeStoreChannel ) ) { new LogEntryWriter( writeChannel ).serialize( dropTransaction ); } }