Skip to content

Commit 1e70ec7

Browse files
committed
Refactor MerkleTree initialization for improved checkpointing and database options
1 parent 48d9b97 commit 1e70ec7

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

src/main/java/io/pwrlabs/database/rocksdb/MerkleTree.java

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -508,24 +508,18 @@ public MerkleTree clone(String newTreeName) throws RocksDBException {
508508
existingTree.close();
509509
}
510510

511+
File destDir = new File("merkleTree/" + newTreeName);
512+
513+
if (destDir.exists()) {
514+
FileUtils.deleteDirectory(destDir);
515+
}
516+
511517
lock.writeLock().lock();
512518
try {
513519
flushToDisk();
514520

515-
File origDir = new File(path);
516-
File destDir = new File("merkleTree/" + newTreeName);
517521
try (Checkpoint checkpoint = Checkpoint.create(db)) {
518-
if (destDir.exists()) {
519-
FileUtils.deleteDirectory(destDir);
520-
} else {
521-
destDir.mkdirs();
522-
FileUtils.deleteDirectory(destDir);
523-
}
524-
525-
if (origDir.exists()) {
526-
// Create a checkpoint and copy the state to the destination directory
527-
checkpoint.createCheckpoint(destDir.getAbsolutePath());
528-
}
522+
checkpoint.createCheckpoint(destDir.getAbsolutePath());
529523
} catch (Exception e) {
530524
e.printStackTrace();
531525
throw new RuntimeException(e);
@@ -603,37 +597,38 @@ public void clear() {
603597
throw new RocksDBException("Failed to create directory: " + treeDir.getPath());
604598
}
605599

606-
// Configure DB options
600+
// 1) DBOptions
607601
DBOptions dbOptions = new DBOptions()
608602
.setCreateIfMissing(true)
609603
.setCreateMissingColumnFamilies(true)
610-
.setParanoidChecks(true)
611604
.setUseDirectReads(true)
612-
.setUseDirectIoForFlushAndCompaction(true);
613-
614-
// Re-apply all the options from the constructor
615-
dbOptions.setMaxTotalWalSize(45 * 1024 * 1024L)
616-
.setWalSizeLimitMB(15)
617-
.setWalTtlSeconds(24 * 60 * 60)
605+
.setAllowMmapReads(false)
606+
.setUseDirectIoForFlushAndCompaction(true)
607+
.setMaxOpenFiles(100)
608+
.setMaxBackgroundJobs(1)
618609
.setInfoLogLevel(InfoLogLevel.FATAL_LEVEL)
619-
.setDbLogDir("")
620-
.setLogFileTimeToRoll(0);
621-
622-
dbOptions.setAllowMmapReads(false)
623-
.setAllowMmapWrites(false)
624-
.setMaxOpenFiles(1000)
625-
.setMaxFileOpeningThreads(10);
610+
.setMaxManifestFileSize(64L * 1024 * 1024) // e.g. 64 MB
611+
.setMaxTotalWalSize(250L * 1024 * 1024) // total WAL across all CFs ≤ 250 MB
612+
.setWalSizeLimitMB(250) // (optional) per-WAL-file soft limit
613+
.setKeepLogFileNum(3); // keep at most 3 WAL files, regardless of age/size
626614

627-
dbOptions.setEnableWriteThreadAdaptiveYield(true)
628-
.setAllowConcurrentMemtableWrite(true);
615+
// 2) Table format: no cache, small blocks
616+
BlockBasedTableConfig tableConfig = new BlockBasedTableConfig()
617+
.setNoBlockCache(true)
618+
.setBlockSize(4 * 1024) // 4 KB blocks
619+
.setFormatVersion(5)
620+
.setChecksumType(ChecksumType.kxxHash);
629621

630-
// Configure column family options
622+
// 3) ColumnFamilyOptions: no compression, single write buffer
631623
ColumnFamilyOptions cfOptions = new ColumnFamilyOptions()
632-
.optimizeUniversalStyleCompaction()
633-
.setWriteBufferSize(64 * 1024 * 1024L)
634-
.setMaxWriteBufferNumber(3);
635-
636-
// Prepare column families
624+
.setTableFormatConfig(tableConfig)
625+
.setCompressionType(CompressionType.NO_COMPRESSION)
626+
.setWriteBufferSize(16 * 1024 * 1024) // 16 MB memtable
627+
.setMaxWriteBufferNumber(1)
628+
.setMinWriteBufferNumberToMerge(1)
629+
.optimizeUniversalStyleCompaction();
630+
631+
// 4. Prepare column families
637632
List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>();
638633
List<ColumnFamilyHandle> cfHandles = new ArrayList<>();
639634

@@ -649,10 +644,10 @@ public void clear() {
649644
cfDescriptors.add(new ColumnFamilyDescriptor(
650645
KEY_DATA_DB_NAME.getBytes(), cfOptions));
651646

652-
// Open a fresh DB with all column families
653-
this.db = RocksDB.open(dbOptions, treeDir.getPath(), cfDescriptors, cfHandles);
647+
// 5. Open DB with all column families
648+
this.db = RocksDB.open(dbOptions, path, cfDescriptors, cfHandles);
654649

655-
// Assign handles
650+
// 6. Assign handles
656651
this.metaDataHandle = cfHandles.get(1);
657652
this.nodesHandle = cfHandles.get(2);
658653
this.keyDataHandle = cfHandles.get(3);

0 commit comments

Comments
 (0)