| | |
| | | */ |
| | | private void ensureLogFileIsValid(final RecordParser<K, V> parser) throws ChangelogException |
| | | { |
| | | BlockLogReader<K, V> reader = null; |
| | | try |
| | | try(final RandomAccessFile readerWriter = new RandomAccessFile(logfile, "rws"); |
| | | final BlockLogReader<K, V> reader = BlockLogReader.newReader(logfile, readerWriter, parser)) |
| | | { |
| | | final RandomAccessFile readerWriter = new RandomAccessFile(logfile, "rws"); |
| | | reader = BlockLogReader.newReader(logfile, readerWriter, parser) ; |
| | | final long lastValidPosition = reader.checkLogIsValid(); |
| | | if (lastValidPosition != -1) |
| | | { |
| | |
| | | logfile.getPath(), |
| | | StaticUtils.stackTraceToSingleLineString(e))); |
| | | } |
| | | finally |
| | | { |
| | | StaticUtils.close(reader); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | void dumpAsTextFile(File dumpFile) throws ChangelogException |
| | | { |
| | | DBCursor<Record<K, V>> cursor = getCursor(); |
| | | BufferedWriter textWriter = null; |
| | | try |
| | | try(final BufferedWriter textWriter = new BufferedWriter(new FileWriter(dumpFile)); |
| | | final DBCursor<Record<K, V>> cursor = getCursor()) |
| | | { |
| | | textWriter = new BufferedWriter(new FileWriter(dumpFile)); |
| | | while (cursor.getRecord() != null) |
| | | { |
| | | Record<K, V> record = cursor.getRecord(); |
| | |
| | | LocalizableMessage.raw("Error when dumping content of log '%s' in target file : '%s'", getPath(), dumpFile), |
| | | e); |
| | | } |
| | | finally |
| | | { |
| | | StaticUtils.close(textWriter); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | Record<K, V> getOldestRecord() throws ChangelogException |
| | | { |
| | | DBCursor<Record<K, V>> cursor = null; |
| | | try |
| | | try (final DBCursor<Record<K, V>> cursor = getCursor()) |
| | | { |
| | | cursor = getCursor(); |
| | | return cursor.next() ? cursor.getRecord() : null; |
| | | } |
| | | finally |
| | | { |
| | | StaticUtils.close(cursor); |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | long getNumberOfRecords() throws ChangelogException |
| | | { |
| | | // TODO : need a more efficient way to retrieve it |
| | | DBCursor<Record<K, V>> cursor = null; |
| | | try |
| | | try(final DBCursor<Record<K, V>> cursor = getCursor()) |
| | | { |
| | | cursor = getCursor(); |
| | | long counter = 0L; |
| | | while (cursor.next()) |
| | | { |
| | |
| | | } |
| | | return counter; |
| | | } |
| | | finally |
| | | { |
| | | StaticUtils.close(cursor); |
| | | } |
| | | } |
| | | |
| | | /** {@inheritDoc} */ |
| | |
| | | */ |
| | | void delete() throws ChangelogException |
| | | { |
| | | final boolean isDeleted = logfile.delete(); |
| | | if (!isDeleted) |
| | | if (!logfile.delete()) |
| | | { |
| | | throw new ChangelogException(ERR_CHANGELOG_UNABLE_TO_DELETE_LOG_FILE.get(getPath())); |
| | | } |