Skip to content

Commit 5a194f7

Browse files
committed
Enhance MerkleTree main method to improve data comparison and flush logic
1 parent 1b2d271 commit 5a194f7

File tree

1 file changed

+51
-6
lines changed

1 file changed

+51
-6
lines changed

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ public void flushToDisk() throws RocksDBException {
364364
errorIfClosed();
365365
lock.writeLock().lock();
366366
try {
367+
//long startTime = System.currentTimeMillis();
367368
try (WriteBatch batch = new WriteBatch()) {
368369
//Clear old metadata from disk
369370
try (RocksIterator iterator = db.newIterator(metaDataHandle)) {
@@ -402,9 +403,16 @@ public void flushToDisk() throws RocksDBException {
402403
db.write(writeOptions, batch);
403404
}
404405

406+
db.compactRange(metaDataHandle);
407+
db.compactRange(nodesHandle);
408+
db.compactRange(keyDataHandle);
409+
405410
nodesCache.clear();
406411
keyDataCache.clear();
407412
hasUnsavedChanges.set(false);
413+
} finally {
414+
//long endTime = System.currentTimeMillis();
415+
//System.out.println("Flush completed in " + (endTime - startTime) + " ms");
408416
}
409417
} finally {
410418
lock.writeLock().unlock();
@@ -1256,21 +1264,58 @@ public boolean equals(Object obj) {
12561264
//endregion
12571265

12581266
public static void main(String[] args) throws Exception {
1259-
MerkleTree tree = new MerkleTree("b412305666oo/tree1");
1267+
MerkleTree tree = new MerkleTree("w1e2111we3/tree1");
12601268
tree.addOrUpdateData("key1".getBytes(), "value1".getBytes());
12611269

1262-
MerkleTree tree2 = tree.clone("br26150664oo6/tree2");
1270+
MerkleTree tree2 = tree.clone("we211131we/tree2");
12631271

12641272
tree.addOrUpdateData("key2".getBytes(), "value2".getBytes());
1265-
//tree.flushToDisk();
1273+
tree.flushToDisk();
12661274

12671275
System.out.println("u");
12681276
tree2.update(tree);
12691277
System.out.println("ud");
12701278

1271-
System.out.println(Hex.toHexString(tree2.getData(("key2").getBytes())));
1272-
System.out.println(Hex.toHexString(tree.getData(("key2").getBytes())));
1279+
tree.flushToDisk();
1280+
tree2.flushToDisk();
1281+
1282+
//compare all keys and values of both trees
1283+
List<byte[]> keys1 = tree.getAllKeys();
1284+
List<byte[]> keys2 = tree2.getAllKeys();
1285+
1286+
List<byte[]> values1 = tree.getAllData();
1287+
List<byte[]> values2 = tree2.getAllData();
1288+
1289+
if(keys1.size() != keys2.size()) {
1290+
System.out.println("Keys size do not match: " + keys1.size() + " != " + keys2.size());
1291+
} else {
1292+
System.out.println("Keys size match: " + keys1.size());
1293+
}
1294+
1295+
if(values1.size() != values2.size()) {
1296+
System.out.println("Values size do not match: " + values1.size() + " != " + values2.size());
1297+
} else {
1298+
System.out.println("Values size match: " + values1.size());
1299+
}
1300+
1301+
for (int i = 0; i < keys1.size(); i++) {
1302+
byte[] key1 = keys1.get(i);
1303+
byte[] value1 = values1.get(i);
1304+
1305+
byte[] key2 = keys2.get(i);
1306+
byte[] value2 = values2.get(i);
12731307

1274-
System.out.println(Arrays.equals(tree.getRootHash(), tree2.getRootHash()));
1308+
if (!Arrays.equals(key1, key2)) {
1309+
System.out.println("Keys do not match: " + Hex.toHexString(key1) + " != " + Hex.toHexString(key2));
1310+
} else {
1311+
System.out.println("Keys match: " + new String(key1));
1312+
}
1313+
1314+
if (!Arrays.equals(value1, value2)) {
1315+
System.out.println("Values do not match: " + Hex.toHexString(value1) + " != " + Hex.toHexString(value2));
1316+
} else {
1317+
System.out.println("Values match: " + new String(value1));
1318+
}
1319+
}
12751320
}
12761321
}

0 commit comments

Comments
 (0)