2019-02-01 17:03:09 -08:00
|
|
|
package cachekv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
2020-07-17 08:34:37 -07:00
|
|
|
"time"
|
2019-02-01 17:03:09 -08:00
|
|
|
|
2019-08-02 06:20:39 -07:00
|
|
|
dbm "github.com/tendermint/tm-db"
|
2019-02-01 17:03:09 -08:00
|
|
|
|
2021-03-01 07:10:22 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/internal/conv"
|
2021-03-30 13:13:51 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/listenkv"
|
2019-02-01 17:03:09 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/tracekv"
|
2020-01-16 13:46:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/types"
|
2020-06-18 11:12:44 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/telemetry"
|
2020-07-30 07:53:02 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/kv"
|
2019-02-01 17:03:09 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// If value is nil but deleted is false, it means the parent doesn't have the
|
|
|
|
// key. (No need to delete upon Write())
|
|
|
|
type cValue struct {
|
2021-09-10 12:42:56 -07:00
|
|
|
value []byte
|
|
|
|
dirty bool
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store wraps an in-memory cache around an underlying types.KVStore.
|
|
|
|
type Store struct {
|
2019-05-15 07:42:06 -07:00
|
|
|
mtx sync.Mutex
|
|
|
|
cache map[string]*cValue
|
2021-09-10 12:42:56 -07:00
|
|
|
deleted map[string]struct{}
|
2019-05-15 07:42:06 -07:00
|
|
|
unsortedCache map[string]struct{}
|
2021-09-10 12:42:56 -07:00
|
|
|
sortedCache *dbm.MemDB // always ascending sorted
|
2019-05-15 07:42:06 -07:00
|
|
|
parent types.KVStore
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.CacheKVStore = (*Store)(nil)
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// NewStore creates a new Store object
|
2019-02-01 17:03:09 -08:00
|
|
|
func NewStore(parent types.KVStore) *Store {
|
|
|
|
return &Store{
|
2019-05-15 07:42:06 -07:00
|
|
|
cache: make(map[string]*cValue),
|
2021-09-10 12:42:56 -07:00
|
|
|
deleted: make(map[string]struct{}),
|
2019-05-15 07:42:06 -07:00
|
|
|
unsortedCache: make(map[string]struct{}),
|
2021-09-10 12:42:56 -07:00
|
|
|
sortedCache: dbm.NewMemDB(),
|
2019-05-15 07:42:06 -07:00
|
|
|
parent: parent,
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// GetStoreType implements Store.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) GetStoreType() types.StoreType {
|
|
|
|
return store.parent.GetStoreType()
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// Get implements types.KVStore.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) Get(key []byte) (value []byte) {
|
|
|
|
store.mtx.Lock()
|
|
|
|
defer store.mtx.Unlock()
|
2019-09-04 10:33:32 -07:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
types.AssertValidKey(key)
|
|
|
|
|
2021-05-06 06:33:01 -07:00
|
|
|
cacheValue, ok := store.cache[conv.UnsafeBytesToStr(key)]
|
2019-02-01 17:03:09 -08:00
|
|
|
if !ok {
|
|
|
|
value = store.parent.Get(key)
|
|
|
|
store.setCacheValue(key, value, false, false)
|
|
|
|
} else {
|
|
|
|
value = cacheValue.value
|
|
|
|
}
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// Set implements types.KVStore.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) Set(key []byte, value []byte) {
|
|
|
|
store.mtx.Lock()
|
|
|
|
defer store.mtx.Unlock()
|
2019-09-04 10:33:32 -07:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
types.AssertValidKey(key)
|
|
|
|
types.AssertValidValue(value)
|
|
|
|
|
|
|
|
store.setCacheValue(key, value, false, true)
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// Has implements types.KVStore.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) Has(key []byte) bool {
|
|
|
|
value := store.Get(key)
|
|
|
|
return value != nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// Delete implements types.KVStore.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) Delete(key []byte) {
|
|
|
|
store.mtx.Lock()
|
|
|
|
defer store.mtx.Unlock()
|
2020-07-17 12:33:50 -07:00
|
|
|
defer telemetry.MeasureSince(time.Now(), "store", "cachekv", "delete")
|
2019-09-04 10:33:32 -07:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
types.AssertValidKey(key)
|
|
|
|
store.setCacheValue(key, nil, true, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Cachetypes.KVStore.
|
|
|
|
func (store *Store) Write() {
|
|
|
|
store.mtx.Lock()
|
|
|
|
defer store.mtx.Unlock()
|
2020-07-17 12:33:50 -07:00
|
|
|
defer telemetry.MeasureSince(time.Now(), "store", "cachekv", "write")
|
2019-02-01 17:03:09 -08:00
|
|
|
|
|
|
|
// We need a copy of all of the keys.
|
|
|
|
// Not the best, but probably not a bottleneck depending.
|
|
|
|
keys := make([]string, 0, len(store.cache))
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
for key, dbValue := range store.cache {
|
|
|
|
if dbValue.dirty {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
// TODO: Consider allowing usage of Batch, which would allow the write to
|
|
|
|
// at least happen atomically.
|
|
|
|
for _, key := range keys {
|
|
|
|
cacheValue := store.cache[key]
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2019-08-19 09:06:27 -07:00
|
|
|
switch {
|
2021-09-10 12:42:56 -07:00
|
|
|
case store.isDeleted(key):
|
2019-02-01 17:03:09 -08:00
|
|
|
store.parent.Delete([]byte(key))
|
2019-08-19 09:06:27 -07:00
|
|
|
case cacheValue.value == nil:
|
2019-02-01 17:03:09 -08:00
|
|
|
// Skip, it already doesn't exist in parent.
|
2019-08-19 09:06:27 -07:00
|
|
|
default:
|
2019-02-01 17:03:09 -08:00
|
|
|
store.parent.Set([]byte(key), cacheValue.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the cache
|
2019-05-15 07:42:06 -07:00
|
|
|
store.cache = make(map[string]*cValue)
|
2021-09-10 12:42:56 -07:00
|
|
|
store.deleted = make(map[string]struct{})
|
2019-05-15 07:42:06 -07:00
|
|
|
store.unsortedCache = make(map[string]struct{})
|
2021-09-10 12:42:56 -07:00
|
|
|
store.sortedCache = dbm.NewMemDB()
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// CacheWrap implements CacheWrapper.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) CacheWrap() types.CacheWrap {
|
|
|
|
return NewStore(store)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheWrapWithTrace implements the CacheWrapper interface.
|
|
|
|
func (store *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
|
|
|
|
return NewStore(tracekv.NewStore(store, w, tc))
|
|
|
|
}
|
|
|
|
|
2021-03-30 13:13:51 -07:00
|
|
|
// CacheWrapWithListeners implements the CacheWrapper interface.
|
|
|
|
func (store *Store) CacheWrapWithListeners(storeKey types.StoreKey, listeners []types.WriteListener) types.CacheWrap {
|
|
|
|
return NewStore(listenkv.NewStore(store, storeKey, listeners))
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
//----------------------------------------
|
|
|
|
// Iteration
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// Iterator implements types.KVStore.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) Iterator(start, end []byte) types.Iterator {
|
|
|
|
return store.iterator(start, end, true)
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:57:33 -08:00
|
|
|
// ReverseIterator implements types.KVStore.
|
2019-02-01 17:03:09 -08:00
|
|
|
func (store *Store) ReverseIterator(start, end []byte) types.Iterator {
|
|
|
|
return store.iterator(start, end, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {
|
2019-05-15 07:42:06 -07:00
|
|
|
store.mtx.Lock()
|
|
|
|
defer store.mtx.Unlock()
|
|
|
|
|
2019-02-01 17:03:09 -08:00
|
|
|
var parent, cache types.Iterator
|
|
|
|
|
|
|
|
if ascending {
|
|
|
|
parent = store.parent.Iterator(start, end)
|
|
|
|
} else {
|
|
|
|
parent = store.parent.ReverseIterator(start, end)
|
|
|
|
}
|
|
|
|
|
2019-05-15 07:42:06 -07:00
|
|
|
store.dirtyItems(start, end)
|
2021-09-10 12:42:56 -07:00
|
|
|
cache = newMemIterator(start, end, store.sortedCache, store.deleted, ascending)
|
2019-02-01 17:03:09 -08:00
|
|
|
|
|
|
|
return newCacheMergeIterator(parent, cache, ascending)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructs a slice of dirty items, to use w/ memIterator.
|
2019-05-15 07:42:06 -07:00
|
|
|
func (store *Store) dirtyItems(start, end []byte) {
|
2021-02-27 07:26:22 -08:00
|
|
|
n := len(store.unsortedCache)
|
2021-09-10 12:42:56 -07:00
|
|
|
unsorted := make([]*kv.Pair, 0)
|
|
|
|
// If the unsortedCache is too big, its costs too much to determine
|
|
|
|
// whats in the subset we are concerned about.
|
|
|
|
// If you are interleaving iterator calls with writes, this can easily become an
|
|
|
|
// O(N^2) overhead.
|
|
|
|
// Even without that, too many range checks eventually becomes more expensive
|
|
|
|
// than just not having the cache.
|
|
|
|
if n >= 1024 {
|
|
|
|
for key := range store.unsortedCache {
|
2021-02-27 07:26:22 -08:00
|
|
|
cacheValue := store.cache[key]
|
2020-07-30 07:53:02 -07:00
|
|
|
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})
|
2021-02-27 07:26:22 -08:00
|
|
|
}
|
2021-09-10 12:42:56 -07:00
|
|
|
} else {
|
|
|
|
// else do a linear scan to determine if the unsorted pairs are in the pool.
|
|
|
|
for key := range store.unsortedCache {
|
|
|
|
if dbm.IsKeyInDomain(conv.UnsafeStrToBytes(key), start, end) {
|
|
|
|
cacheValue := store.cache[key]
|
|
|
|
unsorted = append(unsorted, &kv.Pair{Key: []byte(key), Value: cacheValue.value})
|
|
|
|
}
|
|
|
|
}
|
2021-02-27 07:26:22 -08:00
|
|
|
}
|
2021-09-10 12:42:56 -07:00
|
|
|
store.clearUnsortedCacheSubset(unsorted)
|
|
|
|
}
|
2020-04-29 19:36:34 -07:00
|
|
|
|
2021-09-10 12:42:56 -07:00
|
|
|
func (store *Store) clearUnsortedCacheSubset(unsorted []*kv.Pair) {
|
|
|
|
n := len(store.unsortedCache)
|
2021-02-27 07:26:22 -08:00
|
|
|
if len(unsorted) == n { // This pattern allows the Go compiler to emit the map clearing idiom for the entire map.
|
|
|
|
for key := range store.unsortedCache {
|
2019-05-15 07:42:06 -07:00
|
|
|
delete(store.unsortedCache, key)
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
2021-02-27 07:26:22 -08:00
|
|
|
} else { // Otherwise, normally delete the unsorted keys from the map.
|
|
|
|
for _, kv := range unsorted {
|
2021-03-01 07:10:22 -08:00
|
|
|
delete(store.unsortedCache, conv.UnsafeBytesToStr(kv.Key))
|
2021-02-27 07:26:22 -08:00
|
|
|
}
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
2019-05-15 07:42:06 -07:00
|
|
|
sort.Slice(unsorted, func(i, j int) bool {
|
|
|
|
return bytes.Compare(unsorted[i].Key, unsorted[j].Key) < 0
|
2019-02-01 17:03:09 -08:00
|
|
|
})
|
|
|
|
|
2021-09-10 12:42:56 -07:00
|
|
|
for _, item := range unsorted {
|
|
|
|
if item.Value == nil {
|
|
|
|
// deleted element, tracked by store.deleted
|
|
|
|
// setting arbitrary value
|
|
|
|
store.sortedCache.Set(item.Key, []byte{})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := store.sortedCache.Set(item.Key, item.Value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-05-15 07:42:06 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
// etc
|
|
|
|
|
|
|
|
// Only entrypoint to mutate store.cache.
|
|
|
|
func (store *Store) setCacheValue(key, value []byte, deleted bool, dirty bool) {
|
2021-08-24 01:59:38 -07:00
|
|
|
keyStr := conv.UnsafeBytesToStr(key)
|
|
|
|
store.cache[keyStr] = &cValue{
|
2021-09-10 12:42:56 -07:00
|
|
|
value: value,
|
|
|
|
dirty: dirty,
|
|
|
|
}
|
|
|
|
if deleted {
|
|
|
|
store.deleted[keyStr] = struct{}{}
|
|
|
|
} else {
|
|
|
|
delete(store.deleted, keyStr)
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
2019-05-15 07:42:06 -07:00
|
|
|
if dirty {
|
2021-08-24 01:59:38 -07:00
|
|
|
store.unsortedCache[keyStr] = struct{}{}
|
2019-05-15 07:42:06 -07:00
|
|
|
}
|
2019-02-01 17:03:09 -08:00
|
|
|
}
|
2021-09-10 12:42:56 -07:00
|
|
|
|
|
|
|
func (store *Store) isDeleted(key string) bool {
|
|
|
|
_, ok := store.deleted[key]
|
|
|
|
return ok
|
|
|
|
}
|