Skip to content

Commit f35fcf0

Browse files
committed
Enhance MerkleTree clone method to create parent directories if they do not exist
1 parent fe0eec2 commit f35fcf0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.rocksdb.*;
1313

1414
import java.io.File;
15+
import java.io.IOException;
1516
import java.nio.ByteBuffer;
1617
import java.util.*;
1718
import java.util.concurrent.ConcurrentHashMap;
@@ -444,7 +445,7 @@ public void close() throws RocksDBException {
444445
}
445446
}
446447

447-
public MerkleTree clone(String newTreeName) throws RocksDBException {
448+
public MerkleTree clone(String newTreeName) throws RocksDBException, IOException {
448449
errorIfClosed();
449450

450451
if (newTreeName == null || newTreeName.isEmpty()) {
@@ -460,6 +461,14 @@ public MerkleTree clone(String newTreeName) throws RocksDBException {
460461

461462
if (destDir.exists()) {
462463
FileUtils.deleteDirectory(destDir);
464+
} else {
465+
// If the directory has sub-directories then create them without creating the directory itself
466+
File parent = destDir.getParentFile();
467+
if (parent != null && !parent.exists()) {
468+
if (!parent.mkdirs()) {
469+
throw new IOException("Failed to create parent directories for " + destDir);
470+
}
471+
}
463472
}
464473

465474
lock.writeLock().lock();
@@ -1130,10 +1139,10 @@ public boolean equals(Object obj) {
11301139
//endregion
11311140

11321141
public static void main(String[] args) throws Exception {
1133-
MerkleTree tree = new MerkleTree("tree1");
1142+
MerkleTree tree = new MerkleTree("bro/tree1");
11341143
tree.addOrUpdateData("key1".getBytes(), "value1".getBytes());
11351144

1136-
MerkleTree tree2 = tree.clone("tree2");
1145+
MerkleTree tree2 = tree.clone("bro/tree2");
11371146
System.out.println(Hex.toHexString(tree2.getData("key1".getBytes())));
11381147
System.out.println("ok");
11391148
}

0 commit comments

Comments
 (0)