1212import org .rocksdb .*;
1313
1414import java .io .File ;
15+ import java .io .IOException ;
1516import java .nio .ByteBuffer ;
1617import java .util .*;
1718import 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