77import io .pwrlabs .util .files .FileUtils ;
88import io .pwrlabs .utils .ObjectsInMemoryTracker ;
99import lombok .Getter ;
10- import lombok .SneakyThrows ;
1110import org .json .JSONObject ;
1211import org .rocksdb .*;
1312
@@ -74,6 +73,7 @@ public class MerkleTree {
7473 private byte [] rootHash = null ;
7574
7675 private AtomicBoolean closed = new AtomicBoolean (false );
76+ private AtomicBoolean absolutelyClosed = new AtomicBoolean (false ); //Marked as true when the database is closed and the object is not usable anymore
7777 private AtomicBoolean hasUnsavedChanges = new AtomicBoolean (false );
7878 private AtomicBoolean trackTimeOfOperations = new AtomicBoolean (false );
7979
@@ -126,7 +126,7 @@ public MerkleTree(String treeName, boolean trackTimeOfOperations) throws RocksDB
126126 * Get the current root hash of the Merkle tree.
127127 */
128128 public byte [] getRootHash () {
129- errorIfClosed ();
129+ errorIfAbsolutelyClosed ();
130130 getReadLock ();
131131 try {
132132 if (rootHash == null ) return null ;
@@ -137,7 +137,7 @@ public byte[] getRootHash() {
137137 }
138138
139139 public byte [] getRootHashSavedOnDisk () {
140- errorIfClosed ();
140+ errorIfAbsolutelyClosed ();
141141 getReadLock ();
142142 try {
143143 return getData (metaDataHandle , KEY_ROOT_HASH .getBytes ());
@@ -149,7 +149,7 @@ public byte[] getRootHashSavedOnDisk() {
149149 }
150150
151151 public int getNumLeaves () {
152- errorIfClosed ();
152+ errorIfAbsolutelyClosed ();
153153 getReadLock ();
154154 try {
155155 return numLeaves ;
@@ -159,7 +159,7 @@ public int getNumLeaves() {
159159 }
160160
161161 public int getDepth () {
162- errorIfClosed ();
162+ errorIfAbsolutelyClosed ();
163163 getReadLock ();
164164 try {
165165 return depth ;
@@ -175,7 +175,7 @@ public int getDepth() {
175175 * @throws RocksDBException If there's an error accessing RocksDB
176176 */
177177 public HashSet <Node > getAllNodes () throws RocksDBException {
178- errorIfClosed ();
178+ errorIfAbsolutelyClosed ();
179179 getReadLock ();
180180 try {
181181 HashSet <Node > allNodes = new HashSet <>();
@@ -213,7 +213,7 @@ public HashSet<Node> getAllNodes() throws RocksDBException {
213213 * @throws IllegalArgumentException If key is null
214214 */
215215 public byte [] getData (byte [] key ) {
216- errorIfClosed ();
216+ errorIfAbsolutelyClosed ();
217217 byte [] data = keyDataCache .get (new ByteArrayWrapper (key ));
218218 if (data != null ) return data ;
219219
@@ -240,7 +240,7 @@ public byte[] getData(byte[] key) {
240240 */
241241 public void addOrUpdateData (byte [] key , byte [] data ) throws RocksDBException {
242242 long startTime = System .currentTimeMillis ();
243- errorIfClosed ();
243+ errorIfAbsolutelyClosed ();
244244
245245 if (key == null ) {
246246 throw new IllegalArgumentException ("Key cannot be null" );
@@ -281,7 +281,7 @@ public void addOrUpdateData(byte[] key, byte[] data) throws RocksDBException {
281281
282282 public void revertUnsavedChanges () {
283283 if (!hasUnsavedChanges .get ()) return ;
284- errorIfClosed ();
284+ errorIfAbsolutelyClosed ();
285285
286286 getWriteLock ();
287287 try {
@@ -300,7 +300,7 @@ public void revertUnsavedChanges() {
300300 }
301301
302302 public boolean containsKey (byte [] key ) {
303- errorIfClosed ();
303+ errorIfAbsolutelyClosed ();
304304
305305 if (key == null ) {
306306 throw new IllegalArgumentException ("Key cannot be null" );
@@ -317,7 +317,7 @@ public boolean containsKey(byte[] key) {
317317 }
318318
319319 public List <byte []> getAllKeys () throws RocksDBException {
320- errorIfClosed ();
320+ errorIfAbsolutelyClosed ();
321321 getReadLock ();
322322 try {
323323 ensureDbOpen ();
@@ -337,7 +337,7 @@ public List<byte[]> getAllKeys() throws RocksDBException {
337337 }
338338
339339 public List <byte []> getAllData () throws RocksDBException {
340- errorIfClosed ();
340+ errorIfAbsolutelyClosed ();
341341 getReadLock ();
342342 try {
343343 ensureDbOpen ();
@@ -357,7 +357,7 @@ public List<byte[]> getAllData() throws RocksDBException {
357357 }
358358
359359 public BiResult <List <byte []>, List <byte []>> keysAndTheirValues () throws RocksDBException {
360- errorIfClosed ();
360+ errorIfAbsolutelyClosed ();
361361 getReadLock ();
362362 try {
363363 ensureDbOpen ();
@@ -383,7 +383,7 @@ public BiResult<List<byte[]>, List<byte[]>> keysAndTheirValues() throws RocksDBE
383383 */
384384 public void flushToDisk (boolean releaseDb ) throws RocksDBException {
385385 long startTime = System .currentTimeMillis ();
386- errorIfClosed ();
386+ errorIfAbsolutelyClosed ();
387387 getWriteLock ();
388388 try {
389389 if (hasUnsavedChanges .get ()) {
@@ -450,9 +450,10 @@ public void close() throws RocksDBException {
450450 long startTime = System .currentTimeMillis ();
451451 getWriteLock ();
452452 try {
453- if (closed .get ()) return ;
453+ if (absolutelyClosed .get ()) return ;
454454 flushToDisk (true );
455455 openTrees .remove (treeName );
456+ absolutelyClosed .set (true );
456457 } finally {
457458 releaseWriteLock ();
458459 long endTime = System .currentTimeMillis ();
@@ -462,7 +463,7 @@ public void close() throws RocksDBException {
462463
463464 public MerkleTree clone (String newTreeName ) throws RocksDBException , IOException {
464465 long startTime = System .currentTimeMillis ();
465- errorIfClosed ();
466+ errorIfAbsolutelyClosed ();
466467
467468 if (newTreeName == null || newTreeName .isEmpty ()) {
468469 throw new IllegalArgumentException ("New tree name cannot be null or empty" );
@@ -509,7 +510,7 @@ public MerkleTree clone(String newTreeName) throws RocksDBException, IOException
509510
510511 public void update (MerkleTree sourceTree ) throws RocksDBException , IOException {
511512 long startTime = System .currentTimeMillis ();
512- errorIfClosed ();
513+ errorIfAbsolutelyClosed ();
513514 getWriteLock ();
514515 sourceTree .getWriteLock ();
515516 try {
@@ -581,7 +582,7 @@ public void update(MerkleTree sourceTree) throws RocksDBException, IOException {
581582 */
582583 public void clear () throws RocksDBException {
583584 long startTime = System .currentTimeMillis ();
584- errorIfClosed ();
585+ errorIfAbsolutelyClosed ();
585586 getWriteLock ();
586587 try {
587588 ensureDbOpen ();
@@ -1018,8 +1019,8 @@ private void addNode(int level, Node node) throws RocksDBException {
10181019 }
10191020 }
10201021
1021- private void errorIfClosed () {
1022- if (closed .get ()) {
1022+ private void errorIfAbsolutelyClosed () {
1023+ if (absolutelyClosed .get ()) {
10231024 throw new IllegalStateException ("MerkleTree is closed" );
10241025 }
10251026 }
0 commit comments