Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions sei-db/db_engine/rocksdb/mvcc/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ func OpenDB(dataDir string, config config.StateStoreConfig) (*Database, error) {
}

func (db *Database) getSlice(storeKey string, version int64, key []byte) (*grocksdb.Slice, error) {
readOpts := newTSReadOptions(version)
defer readOpts.Destroy()
return db.storage.GetCF(
newTSReadOptions(version),
readOpts,
db.cfHandle,
prependStoreKey(storeKey, key),
)
Expand Down Expand Up @@ -322,8 +324,9 @@ func (db *Database) Iterator(storeKey string, version int64, start, end []byte)
prefix := storePrefix(storeKey)
start, end = util.IterateWithPrefix(prefix, start, end)

itr := db.storage.NewIteratorCF(newTSReadOptions(version), db.cfHandle)
return NewRocksDBIterator(itr, prefix, start, end, version, db.earliestVersion, false), nil
readOpts := newTSReadOptions(version)
itr := db.storage.NewIteratorCF(readOpts, db.cfHandle)
return NewRocksDBIterator(itr, readOpts, prefix, start, end, version, db.earliestVersion, false), nil
}

func (db *Database) ReverseIterator(storeKey string, version int64, start, end []byte) (types.DBIterator, error) {
Expand All @@ -338,8 +341,9 @@ func (db *Database) ReverseIterator(storeKey string, version int64, start, end [
prefix := storePrefix(storeKey)
start, end = util.IterateWithPrefix(prefix, start, end)

itr := db.storage.NewIteratorCF(newTSReadOptions(version), db.cfHandle)
return NewRocksDBIterator(itr, prefix, start, end, version, db.earliestVersion, true), nil
readOpts := newTSReadOptions(version)
itr := db.storage.NewIteratorCF(readOpts, db.cfHandle)
return NewRocksDBIterator(itr, readOpts, prefix, start, end, version, db.earliestVersion, true), nil
}

// Import loads the initial version of the state in parallel with numWorkers goroutines
Expand Down Expand Up @@ -410,10 +414,9 @@ func (db *Database) RawIterate(storeKey string, fn func(key []byte, value []byte
readOpts := grocksdb.NewDefaultReadOptions()
readOpts.SetIterStartTimestamp(startTs[:])
readOpts.SetTimestamp(endTs[:])
defer readOpts.Destroy()

itr := db.storage.NewIteratorCF(readOpts, db.cfHandle)
rocksItr := NewRocksDBIterator(itr, prefix, start, end, latestVersion, 1, false)
rocksItr := NewRocksDBIterator(itr, readOpts, prefix, start, end, latestVersion, 1, false)
defer func() { _ = rocksItr.Close() }()

for rocksItr.Valid() {
Expand Down
37 changes: 22 additions & 15 deletions sei-db/db_engine/rocksdb/mvcc/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,25 @@ var _ types.DBIterator = (*iterator)(nil)

type iterator struct {
source *grocksdb.Iterator
readOpts *grocksdb.ReadOptions
prefix, start, end []byte
version int64
reverse bool
invalid bool
}

func NewRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, version int64, earliestVersion int64, reverse bool) *iterator {
func NewRocksDBIterator(source *grocksdb.Iterator, readOpts *grocksdb.ReadOptions, prefix, start, end []byte, version int64, earliestVersion int64, reverse bool) *iterator {
// Return invalid iterator if requested iterator height is lower than earliest version after pruning
if version < earliestVersion {
return &iterator{
source: source,
prefix: prefix,
start: start,
end: end,
version: version,
reverse: reverse,
invalid: true,
source: source,
readOpts: readOpts,
prefix: prefix,
start: start,
end: end,
version: version,
reverse: reverse,
invalid: true,
}
}

Expand Down Expand Up @@ -58,13 +60,14 @@ func NewRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, ve
}

return &iterator{
source: source,
prefix: prefix,
start: start,
end: end,
version: version,
reverse: reverse,
invalid: !source.Valid(),
source: source,
readOpts: readOpts,
prefix: prefix,
start: start,
end: end,
version: version,
reverse: reverse,
invalid: !source.Valid(),
}
}

Expand Down Expand Up @@ -157,6 +160,10 @@ func (itr *iterator) Error() error {
func (itr *iterator) Close() error {
itr.source.Close()
itr.source = nil
if itr.readOpts != nil {
itr.readOpts.Destroy()
itr.readOpts = nil
}
itr.invalid = true
return nil
}
Expand Down
Loading