Skip to content
Open
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: 12 additions & 5 deletions sei-cosmos/store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewStore(parent types.KVStore, storeKey types.StoreKey, cacheSize int) *Sto
cache: &sync.Map{},
deleted: &sync.Map{},
unsortedCache: &sync.Map{},
sortedCache: dbm.NewMemDB(),
sortedCache: nil,
parent: parent,
storeKey: storeKey,
cacheSize: cacheSize,
Expand Down Expand Up @@ -120,7 +120,7 @@ func (store *Store) Write() {
store.cache = &sync.Map{}
store.deleted = &sync.Map{}
store.unsortedCache = &sync.Map{}
store.sortedCache = dbm.NewMemDB()
store.sortedCache = nil
}

// CacheWrap implements CacheWrapper.
Expand All @@ -146,6 +146,13 @@ func (store *Store) ReverseIterator(start, end []byte) types.Iterator {
return store.iterator(start, end, false)
}

func (store *Store) getOrInitSortedCache() *dbm.MemDB {
if store.sortedCache == nil {
store.sortedCache = dbm.NewMemDB()
}
return store.sortedCache
}

func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {
store.mtx.Lock()
defer store.mtx.Unlock()
Expand All @@ -168,7 +175,7 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {
}
}()
store.dirtyItems(start, end)
cache = newMemIterator(start, end, store.sortedCache, store.deleted, ascending)
cache = newMemIterator(start, end, store.getOrInitSortedCache(), store.deleted, ascending)
return NewCacheMergeIterator(parent, cache, ascending, store.storeKey)
}

Expand Down Expand Up @@ -301,13 +308,13 @@ func (store *Store) clearUnsortedCacheSubset(unsorted []*kv.Pair, sortState sort
if item.Value == nil {
// deleted element, tracked by store.deleted
// setting arbitrary value
if err := store.sortedCache.Set(item.Key, []byte{}); err != nil {
if err := store.getOrInitSortedCache().Set(item.Key, []byte{}); err != nil {
panic(err)
}

continue
}
if err := store.sortedCache.Set(item.Key, item.Value); err != nil {
if err := store.getOrInitSortedCache().Set(item.Key, item.Value); err != nil {
panic(err)
}
}
Expand Down
Loading