2018-06-21 14:33:36 -07:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2018-07-12 16:58:51 -07:00
|
|
|
"io"
|
|
|
|
|
2018-06-21 14:33:36 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2018-07-26 18:24:18 -07:00
|
|
|
var _ KVStore = prefixStore{}
|
|
|
|
|
2018-06-21 14:33:36 -07:00
|
|
|
type prefixStore struct {
|
2018-07-26 18:24:18 -07:00
|
|
|
parent KVStore
|
2018-06-21 14:33:36 -07:00
|
|
|
prefix []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Store
|
|
|
|
func (s prefixStore) GetStoreType() StoreType {
|
2018-07-26 18:24:18 -07:00
|
|
|
return s.parent.GetStoreType()
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements CacheWrap
|
|
|
|
func (s prefixStore) CacheWrap() CacheWrap {
|
|
|
|
return NewCacheKVStore(s)
|
|
|
|
}
|
|
|
|
|
2018-07-12 16:58:51 -07:00
|
|
|
// CacheWrapWithTrace implements the KVStore interface.
|
|
|
|
func (s prefixStore) CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap {
|
|
|
|
return NewCacheKVStore(NewTraceKVStore(s, w, tc))
|
|
|
|
}
|
|
|
|
|
2018-06-21 14:33:36 -07:00
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) Get(key []byte) []byte {
|
2018-07-26 18:24:18 -07:00
|
|
|
return s.parent.Get(append(s.prefix, key...))
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) Has(key []byte) bool {
|
2018-07-26 18:24:18 -07:00
|
|
|
return s.parent.Has(append(s.prefix, key...))
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) Set(key, value []byte) {
|
2018-07-26 18:24:18 -07:00
|
|
|
s.parent.Set(append(s.prefix, key...), value)
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) Delete(key []byte) {
|
2018-07-26 18:24:18 -07:00
|
|
|
s.parent.Delete(append(s.prefix, key...))
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) Prefix(prefix []byte) KVStore {
|
|
|
|
return prefixStore{s, prefix}
|
|
|
|
}
|
|
|
|
|
2018-07-26 18:24:18 -07:00
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) Gas(meter GasMeter, config GasConfig) KVStore {
|
|
|
|
return NewGasKVStore(meter, config, s)
|
|
|
|
}
|
|
|
|
|
2018-06-21 14:33:36 -07:00
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) Iterator(start, end []byte) Iterator {
|
2018-06-27 10:21:12 -07:00
|
|
|
if end == nil {
|
|
|
|
end = sdk.PrefixEndBytes(s.prefix)
|
|
|
|
} else {
|
|
|
|
end = append(s.prefix, end...)
|
|
|
|
}
|
2018-06-21 14:33:36 -07:00
|
|
|
return prefixIterator{
|
|
|
|
prefix: s.prefix,
|
2018-07-26 18:24:18 -07:00
|
|
|
iter: s.parent.Iterator(append(s.prefix, start...), end),
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements KVStore
|
|
|
|
func (s prefixStore) ReverseIterator(start, end []byte) Iterator {
|
2018-06-27 10:21:12 -07:00
|
|
|
if end == nil {
|
|
|
|
end = sdk.PrefixEndBytes(s.prefix)
|
|
|
|
} else {
|
|
|
|
end = append(s.prefix, end...)
|
|
|
|
}
|
2018-06-21 14:33:36 -07:00
|
|
|
return prefixIterator{
|
|
|
|
prefix: s.prefix,
|
2018-07-26 18:24:18 -07:00
|
|
|
iter: s.parent.ReverseIterator(start, end),
|
2018-06-21 14:33:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type prefixIterator struct {
|
|
|
|
prefix []byte
|
|
|
|
|
|
|
|
iter Iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Iterator
|
|
|
|
func (iter prefixIterator) Domain() (start []byte, end []byte) {
|
|
|
|
start, end = iter.iter.Domain()
|
|
|
|
start = start[len(iter.prefix):]
|
|
|
|
end = end[len(iter.prefix):]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Iterator
|
|
|
|
func (iter prefixIterator) Valid() bool {
|
|
|
|
return iter.iter.Valid()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Iterator
|
|
|
|
func (iter prefixIterator) Next() {
|
|
|
|
iter.iter.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Iterator
|
|
|
|
func (iter prefixIterator) Key() (key []byte) {
|
|
|
|
key = iter.iter.Key()
|
|
|
|
key = key[len(iter.prefix):]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Iterator
|
|
|
|
func (iter prefixIterator) Value() []byte {
|
|
|
|
return iter.iter.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Iterator
|
|
|
|
func (iter prefixIterator) Close() {
|
|
|
|
iter.iter.Close()
|
|
|
|
}
|