From 2df2c00e36e649098d38e6e9e22813b8434d55dc Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Sun, 27 Jun 2021 16:52:45 +0200 Subject: [PATCH] Fix unmodifiable read options --- .../database/disk/LLLocalDictionary.java | 86 +++++++++---------- .../disk/LLLocalKeyValueDatabase.java | 1 + 2 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDictionary.java b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDictionary.java index 543d382..21a0b23 100644 --- a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDictionary.java +++ b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalDictionary.java @@ -1251,8 +1251,7 @@ public class LLLocalDictionary implements LLDictionary { public Flux badBlocks(LLRange range) { return Flux .create(sink -> { - try { - var ro = new ReadOptions(getReadOptions(null)); + try (var ro = new ReadOptions(getReadOptions(null))) { ro.setFillCache(false); if (!range.isSingle()) { ro.setReadaheadSize(32 * 1024); @@ -1666,53 +1665,54 @@ public class LLLocalDictionary implements LLDictionary { public Mono clear() { return Mono .fromCallable(() -> { - var readOpts = getReadOptions(null); - readOpts.setVerifyChecksums(VERIFY_CHECKSUMS_WHEN_NOT_NEEDED); + try (var readOpts = new ReadOptions(getReadOptions(null))) { + readOpts.setVerifyChecksums(VERIFY_CHECKSUMS_WHEN_NOT_NEEDED); - // readOpts.setIgnoreRangeDeletions(true); - readOpts.setFillCache(false); - readOpts.setReadaheadSize(32 * 1024); // 32KiB - try (CappedWriteBatch writeBatch = new CappedWriteBatch(db, - CAPPED_WRITE_BATCH_CAP, - RESERVED_WRITE_BATCH_SIZE, - MAX_WRITE_BATCH_SIZE, - BATCH_WRITE_OPTIONS - )) { + // readOpts.setIgnoreRangeDeletions(true); + readOpts.setFillCache(false); + readOpts.setReadaheadSize(32 * 1024); // 32KiB + try (CappedWriteBatch writeBatch = new CappedWriteBatch(db, + CAPPED_WRITE_BATCH_CAP, + RESERVED_WRITE_BATCH_SIZE, + MAX_WRITE_BATCH_SIZE, + BATCH_WRITE_OPTIONS + )) { - byte[] firstDeletedKey = null; - byte[] lastDeletedKey = null; - try (RocksIterator rocksIterator = db.newIterator(cfh, readOpts)) { - rocksIterator.seekToLast(); + byte[] firstDeletedKey = null; + byte[] lastDeletedKey = null; + try (RocksIterator rocksIterator = db.newIterator(cfh, readOpts)) { + rocksIterator.seekToLast(); - rocksIterator.status(); - if (rocksIterator.isValid()) { - firstDeletedKey = FIRST_KEY; - lastDeletedKey = rocksIterator.key(); - writeBatch.deleteRange(cfh, FIRST_KEY, rocksIterator.key()); - writeBatch.delete(cfh, rocksIterator.key()); + rocksIterator.status(); + if (rocksIterator.isValid()) { + firstDeletedKey = FIRST_KEY; + lastDeletedKey = rocksIterator.key(); + writeBatch.deleteRange(cfh, FIRST_KEY, rocksIterator.key()); + writeBatch.delete(cfh, rocksIterator.key()); + } } + + writeBatch.writeToDbAndClose(); + + + // Compact range + db.suggestCompactRange(cfh); + if (firstDeletedKey != null && lastDeletedKey != null) { + db.compactRange(cfh, + firstDeletedKey, + lastDeletedKey, + new CompactRangeOptions() + .setAllowWriteStall(false) + .setExclusiveManualCompaction(false) + .setChangeLevel(false) + ); + } + + db.flush(new FlushOptions().setWaitForFlush(true).setAllowWriteStall(true), cfh); + db.flushWal(true); } - - writeBatch.writeToDbAndClose(); - - - // Compact range - db.suggestCompactRange(cfh); - if (firstDeletedKey != null && lastDeletedKey != null) { - db.compactRange(cfh, - firstDeletedKey, - lastDeletedKey, - new CompactRangeOptions() - .setAllowWriteStall(false) - .setExclusiveManualCompaction(false) - .setChangeLevel(false) - ); - } - - db.flush(new FlushOptions().setWaitForFlush(true).setAllowWriteStall(true), cfh); - db.flushWal(true); + return null; } - return null; }) .onErrorMap(cause -> new IOException("Failed to clear", cause)) .subscribeOn(dbScheduler); diff --git a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java index cdd6f8d..685a782 100644 --- a/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java +++ b/src/main/java/it/cavallium/dbengine/database/disk/LLLocalKeyValueDatabase.java @@ -481,6 +481,7 @@ public class LLLocalKeyValueDatabase implements LLKeyValueDatabase { private ColumnFamilyHandle getCfh(byte[] columnName) throws RocksDBException { ColumnFamilyHandle cfh = handles.get(Column.special(Column.toString(columnName))); + //noinspection RedundantIfStatement if (!enableColumnsBug) { assert Arrays.equals(cfh.getName(), columnName); }