@@ -127,8 +127,8 @@ func OpenDB(dataDir string, config config.StateStoreConfig) (*Database, error) {
127127}
128128
129129func (db * Database ) getSlice (storeKey string , version int64 , key []byte ) (* grocksdb.Slice , error ) {
130- readOpts := newTSReadOptions (version )
131- defer readOpts . Destroy ()
130+ readOpts , cleanup := newTSReadOptions (version )
131+ defer cleanup ()
132132 return db .storage .GetCF (
133133 readOpts ,
134134 db .cfHandle ,
@@ -324,9 +324,9 @@ func (db *Database) Iterator(storeKey string, version int64, start, end []byte)
324324 prefix := storePrefix (storeKey )
325325 start , end = util .IterateWithPrefix (prefix , start , end )
326326
327- readOpts := newTSReadOptions (version )
327+ readOpts , cleanup := newTSReadOptions (version )
328328 itr := db .storage .NewIteratorCF (readOpts , db .cfHandle )
329- return NewRocksDBIterator (itr , readOpts , prefix , start , end , version , db .earliestVersion , false ), nil
329+ return NewRocksDBIterator (itr , cleanup , prefix , start , end , version , db .earliestVersion , false ), nil
330330}
331331
332332func (db * Database ) ReverseIterator (storeKey string , version int64 , start , end []byte ) (types.DBIterator , error ) {
@@ -341,9 +341,9 @@ func (db *Database) ReverseIterator(storeKey string, version int64, start, end [
341341 prefix := storePrefix (storeKey )
342342 start , end = util .IterateWithPrefix (prefix , start , end )
343343
344- readOpts := newTSReadOptions (version )
344+ readOpts , cleanup := newTSReadOptions (version )
345345 itr := db .storage .NewIteratorCF (readOpts , db .cfHandle )
346- return NewRocksDBIterator (itr , readOpts , prefix , start , end , version , db .earliestVersion , true ), nil
346+ return NewRocksDBIterator (itr , cleanup , prefix , start , end , version , db .earliestVersion , true ), nil
347347}
348348
349349// Import loads the initial version of the state in parallel with numWorkers goroutines
@@ -416,7 +416,7 @@ func (db *Database) RawIterate(storeKey string, fn func(key []byte, value []byte
416416 readOpts .SetTimestamp (endTs [:])
417417
418418 itr := db .storage .NewIteratorCF (readOpts , db .cfHandle )
419- rocksItr := NewRocksDBIterator (itr , readOpts , prefix , start , end , latestVersion , 1 , false )
419+ rocksItr := NewRocksDBIterator (itr , func () { readOpts . Destroy () } , prefix , start , end , latestVersion , 1 , false )
420420 defer func () { _ = rocksItr .Close () }()
421421
422422 for rocksItr .Valid () {
@@ -443,15 +443,17 @@ func (db *Database) GetLatestMigratedModule() (string, error) {
443443 panic ("not implemented" )
444444}
445445
446- // newTSReadOptions returns ReadOptions used in the RocksDB column family read.
447- func newTSReadOptions (version int64 ) * grocksdb.ReadOptions {
446+ // newTSReadOptions returns ReadOptions used in the RocksDB column family read
447+ // and a cleanup function that destroys them. The caller must ensure cleanup is
448+ // called when the ReadOptions are no longer needed.
449+ func newTSReadOptions (version int64 ) (* grocksdb.ReadOptions , func ()) {
448450 var ts [TimestampSize ]byte
449451 binary .LittleEndian .PutUint64 (ts [:], uint64 (version ))
450452
451453 readOpts := grocksdb .NewDefaultReadOptions ()
452454 readOpts .SetTimestamp (ts [:])
453455
454- return readOpts
456+ return readOpts , func () { readOpts . Destroy () }
455457}
456458
457459func storePrefix (storeKey string ) []byte {
0 commit comments