@@ -117,7 +117,8 @@ private static void checkArgNotNull(Object value, String name) {
117117 @ Override
118118 public Set <byte []> allKeys () throws RuntimeException {
119119 resetDbLock .readLock ().lock ();
120- try (final RocksIterator iter = getRocksIterator ()) {
120+ try (final ReadOptions readOptions = getReadOptions ();
121+ final RocksIterator iter = getRocksIterator (readOptions )) {
121122 Set <byte []> result = Sets .newHashSet ();
122123 for (iter .seekToFirst (); iter .isValid (); iter .next ()) {
123124 result .add (iter .key ());
@@ -133,7 +134,8 @@ public Set<byte[]> allKeys() throws RuntimeException {
133134 @ Override
134135 public Set <byte []> allValues () throws RuntimeException {
135136 resetDbLock .readLock ().lock ();
136- try (final RocksIterator iter = getRocksIterator ()) {
137+ try (final ReadOptions readOptions = getReadOptions ();
138+ final RocksIterator iter = getRocksIterator (readOptions )) {
137139 Set <byte []> result = Sets .newHashSet ();
138140 for (iter .seekToFirst (); iter .isValid (); iter .next ()) {
139141 result .add (iter .value ());
@@ -149,7 +151,8 @@ public Set<byte[]> allValues() throws RuntimeException {
149151 @ Override
150152 public long getTotal () throws RuntimeException {
151153 resetDbLock .readLock ().lock ();
152- try (final RocksIterator iter = getRocksIterator ()) {
154+ try (final ReadOptions readOptions = getReadOptions ();
155+ final RocksIterator iter = getRocksIterator (readOptions )) {
153156 long total = 0 ;
154157 for (iter .seekToFirst (); iter .isValid (); iter .next ()) {
155158 total ++;
@@ -282,7 +285,8 @@ public boolean flush() {
282285 */
283286 @ Override
284287 public org .tron .core .db .common .iterator .DBIterator iterator () {
285- return new RockStoreIterator (getRocksIterator ());
288+ ReadOptions readOptions = getReadOptions ();
289+ return new RockStoreIterator (getRocksIterator (readOptions ), readOptions );
286290 }
287291
288292 private void updateByBatchInner (Map <byte [], byte []> rows , WriteOptions options )
@@ -333,7 +337,8 @@ public List<byte[]> getKeysNext(byte[] key, long limit) {
333337 return new ArrayList <>();
334338 }
335339 resetDbLock .readLock ().lock ();
336- try (RocksIterator iter = getRocksIterator ()) {
340+ try (final ReadOptions readOptions = getReadOptions ();
341+ final RocksIterator iter = getRocksIterator (readOptions )) {
337342 List <byte []> result = new ArrayList <>();
338343 long i = 0 ;
339344 for (iter .seek (key ); iter .isValid () && i < limit ; iter .next (), i ++) {
@@ -350,7 +355,8 @@ public Map<byte[], byte[]> getNext(byte[] key, long limit) {
350355 return Collections .emptyMap ();
351356 }
352357 resetDbLock .readLock ().lock ();
353- try (RocksIterator iter = getRocksIterator ()) {
358+ try (final ReadOptions readOptions = getReadOptions ();
359+ final RocksIterator iter = getRocksIterator (readOptions )) {
354360 Map <byte [], byte []> result = new HashMap <>();
355361 long i = 0 ;
356362 for (iter .seek (key ); iter .isValid () && i < limit ; iter .next (), i ++) {
@@ -365,7 +371,8 @@ public Map<byte[], byte[]> getNext(byte[] key, long limit) {
365371 @ Override
366372 public Map <WrappedByteArray , byte []> prefixQuery (byte [] key ) {
367373 resetDbLock .readLock ().lock ();
368- try (RocksIterator iterator = getRocksIterator ()) {
374+ try (final ReadOptions readOptions = getReadOptions ();
375+ final RocksIterator iterator = getRocksIterator (readOptions )) {
369376 Map <WrappedByteArray , byte []> result = new HashMap <>();
370377 for (iterator .seek (key ); iterator .isValid (); iterator .next ()) {
371378 if (Bytes .indexOf (iterator .key (), key ) == 0 ) {
@@ -385,7 +392,8 @@ public Set<byte[]> getlatestValues(long limit) {
385392 return Sets .newHashSet ();
386393 }
387394 resetDbLock .readLock ().lock ();
388- try (RocksIterator iter = getRocksIterator ()) {
395+ try (final ReadOptions readOptions = getReadOptions ();
396+ final RocksIterator iter = getRocksIterator (readOptions )) {
389397 Set <byte []> result = Sets .newHashSet ();
390398 long i = 0 ;
391399 for (iter .seekToLast (); iter .isValid () && i < limit ; iter .prev (), i ++) {
@@ -402,7 +410,8 @@ public Set<byte[]> getValuesNext(byte[] key, long limit) {
402410 return Sets .newHashSet ();
403411 }
404412 resetDbLock .readLock ().lock ();
405- try (RocksIterator iter = getRocksIterator ()) {
413+ try (final ReadOptions readOptions = getReadOptions ();
414+ final RocksIterator iter = getRocksIterator (readOptions )) {
406415 Set <byte []> result = Sets .newHashSet ();
407416 long i = 0 ;
408417 for (iter .seek (key ); iter .isValid () && i < limit ; iter .next (), i ++) {
@@ -430,18 +439,41 @@ public void backup(String dir) throws RocksDBException {
430439 *
431440 * <p>Example of correct usage:
432441 * <pre>{@code
433- * try (RocksIterator iterator = getRocksIterator()) {
442+ * try ( ReadOptions readOptions = new ReadOptions().setFillCache(false);
443+ * RocksIterator iterator = getRocksIterator(readOptions)) {
444+ * iterator.seekToFirst();
434445 * // do something
435446 * }
436447 * }</pre>
437448 *
438449 * @return a new database iterator that must be closed.
439450 */
440- private RocksIterator getRocksIterator () {
441- try (ReadOptions readOptions = new ReadOptions ().setFillCache (false )) {
442- throwIfNotAlive ();
443- return database .newIterator (readOptions );
444- }
451+ private RocksIterator getRocksIterator (ReadOptions readOptions ) {
452+ throwIfNotAlive ();
453+ return database .newIterator (readOptions );
454+ }
455+
456+ /**
457+ * Returns an ReadOptions.
458+ *
459+ * <p><b>CRITICAL:</b> The returned ReadOptions holds native resources and <b>MUST</b> be closed
460+ * after use to prevent memory leaks. It is strongly recommended to use a try-with-resources
461+ * statement.
462+ *
463+ * <p>Example of correct usage:
464+ * <pre>{@code
465+ * try ( ReadOptions readOptions = getReadOptions();
466+ * RocksIterator iterator = getRocksIterator(readOptions)) {
467+ * iterator.seekToFirst();
468+ * // do something
469+ * }
470+ * }</pre>
471+ *
472+ * @return a new database iterator that must be closed.
473+ */
474+ private ReadOptions getReadOptions () {
475+ throwIfNotAlive ();
476+ return new ReadOptions ().setFillCache (false );
445477 }
446478
447479 public boolean deleteDbBakPath (String dir ) {
0 commit comments