@Test public void createShouldClearExistingFile() throws Exception { // given byte[] someBytes = fileWithContent(); // when populator.create(); // then try ( StoreChannel r = fs.open( getIndexFile(), OpenMode.READ ) ) { byte[] firstBytes = new byte[someBytes.length]; r.readAll( ByteBuffer.wrap( firstBytes ) ); assertNotEquals( "Expected previous file content to have been cleared but was still there", someBytes, firstBytes ); } populator.close( true ); }
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 ); } }
private void removeLastCheckpointRecordFromLastLogFile() throws IOException { LogPosition checkpointPosition = null; LogFile transactionLogFile = logFiles.getLogFile(); VersionAwareLogEntryReader<ReadableLogChannel> entryReader = new VersionAwareLogEntryReader<>(); LogPosition startPosition = LogPosition.start( logFiles.getHighestLogVersion() ); try ( ReadableLogChannel reader = transactionLogFile.getReader( startPosition ) ) { LogEntry logEntry; do { logEntry = entryReader.readLogEntry( reader ); if ( logEntry instanceof CheckPoint ) { checkpointPosition = ((CheckPoint) logEntry).getLogPosition(); } } while ( logEntry != null ); } if ( checkpointPosition != null ) { try ( StoreChannel storeChannel = fileSystemRule.open( logFiles.getHighestLogFile(), OpenMode.READ_WRITE ) ) { storeChannel.truncate( checkpointPosition.getByteOffset() ); } } }