cosmos-sdk/types/lib/mapper.go

287 lines
7.2 KiB
Go
Raw Normal View History

2018-04-12 09:19:28 -07:00
package lib
2018-03-14 11:20:15 -07:00
import (
"fmt"
2018-04-13 05:10:55 -07:00
"strconv"
2018-03-14 11:20:15 -07:00
2018-03-15 02:59:36 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-14 11:20:15 -07:00
wire "github.com/cosmos/cosmos-sdk/wire"
)
2018-04-25 05:45:22 -07:00
// Mapper defines a primitive mapper type
2018-04-25 04:44:29 -07:00
type Mapper struct {
2018-04-18 08:55:40 -07:00
key sdk.StoreKey
cdc *wire.Codec
prefix string
}
// ListMapper is a Mapper interface that provides list-like functions
// It panics when the element type cannot be (un/)marshalled by the codec
type ListMapper interface {
2018-04-25 05:45:22 -07:00
// Len() returns the length of the list
// The length is only increased by Push() and not decreased
2018-04-13 05:10:55 -07:00
// ListMapper dosen't check if an index is in bounds
// The user should check Len() before doing any actions
2018-04-13 05:10:55 -07:00
Len(sdk.Context) uint64
2018-04-25 05:45:22 -07:00
// Get() returns the element by its index
2018-04-13 05:10:55 -07:00
Get(sdk.Context, uint64, interface{}) error
2018-04-25 05:45:22 -07:00
// Set() stores the element to the given position
2018-04-20 14:25:43 -07:00
// Setting element out of range will break length counting
2018-04-13 05:10:55 -07:00
// Use Push() instead of Set() to append a new element
Set(sdk.Context, uint64, interface{})
2018-04-25 05:45:22 -07:00
// Delete() deletes the element in the given position
2018-04-20 14:25:43 -07:00
// Other elements' indices are preserved after deletion
2018-04-25 04:44:29 -07:00
// Panics when the index is out of range
2018-04-13 05:10:55 -07:00
Delete(sdk.Context, uint64)
2018-04-25 05:45:22 -07:00
// Push() inserts the element to the end of the list
// It will increase the length when it is called
2018-03-15 02:59:36 -07:00
Push(sdk.Context, interface{})
2018-04-13 05:10:55 -07:00
// Iterate*() is used to iterate over all existing elements in the list
// Return true in the continuation to break
2018-04-25 04:44:29 -07:00
// The second element of the continuation will indicate the position of the element
// Using it with Get() will return the same one with the provided element
2018-04-13 05:10:55 -07:00
2018-04-20 14:25:43 -07:00
// CONTRACT: No writes may happen within a domain while iterating over it.
2018-04-13 05:10:55 -07:00
IterateRead(sdk.Context, interface{}, func(sdk.Context, uint64) bool)
// IterateWrite() is safe to write over the domain
IterateWrite(sdk.Context, interface{}, func(sdk.Context, uint64) bool)
2018-03-14 11:20:15 -07:00
2018-04-18 08:55:40 -07:00
// Key for the length of the list
LengthKey() []byte
// Key for getting elements
ElemKey(uint64) []byte
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// NewListMapper constructs new ListMapper
2018-04-12 06:46:55 -07:00
func NewListMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) ListMapper {
2018-04-25 04:44:29 -07:00
return Mapper{
2018-04-12 06:46:55 -07:00
key: key,
cdc: cdc,
prefix: prefix,
2018-03-14 11:20:15 -07:00
}
}
2018-04-25 05:45:22 -07:00
// Len implements ListMapper
func (m Mapper) Len(ctx sdk.Context) uint64 {
store := ctx.KVStore(m.key)
bz := store.Get(m.LengthKey())
2018-03-14 11:20:15 -07:00
if bz == nil {
2018-04-25 05:45:22 -07:00
zero, err := m.cdc.MarshalBinary(0)
2018-03-14 11:20:15 -07:00
if err != nil {
panic(err)
}
2018-04-25 05:45:22 -07:00
store.Set(m.LengthKey(), zero)
2018-03-14 11:20:15 -07:00
return 0
}
2018-04-13 05:10:55 -07:00
var res uint64
2018-04-25 05:45:22 -07:00
if err := m.cdc.UnmarshalBinary(bz, &res); err != nil {
2018-03-14 11:20:15 -07:00
panic(err)
}
return res
}
2018-04-25 05:45:22 -07:00
// Get implements ListMapper
func (m Mapper) Get(ctx sdk.Context, index uint64, ptr interface{}) error {
store := ctx.KVStore(m.key)
bz := store.Get(m.ElemKey(index))
return m.cdc.UnmarshalBinary(bz, ptr)
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// Set implements ListMapper
func (m Mapper) Set(ctx sdk.Context, index uint64, value interface{}) {
store := ctx.KVStore(m.key)
bz, err := m.cdc.MarshalBinary(value)
2018-03-14 11:20:15 -07:00
if err != nil {
panic(err)
}
2018-04-25 05:45:22 -07:00
store.Set(m.ElemKey(index), bz)
2018-04-12 06:46:55 -07:00
}
2018-04-25 05:45:22 -07:00
// Delete implements ListMapper
func (m Mapper) Delete(ctx sdk.Context, index uint64) {
store := ctx.KVStore(m.key)
store.Delete(m.ElemKey(index))
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// Push implements ListMapper
func (m Mapper) Push(ctx sdk.Context, value interface{}) {
length := m.Len(ctx)
m.Set(ctx, length, value)
2018-03-14 11:20:15 -07:00
2018-04-25 05:45:22 -07:00
store := ctx.KVStore(m.key)
store.Set(m.LengthKey(), marshalUint64(m.cdc, length+1))
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// IterateRead implements ListMapper
func (m Mapper) IterateRead(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, uint64) bool) {
store := ctx.KVStore(m.key)
start, end := subspace([]byte(fmt.Sprintf("%s/elem/", m.prefix)))
2018-04-13 05:10:55 -07:00
iter := store.Iterator(start, end)
for ; iter.Valid(); iter.Next() {
v := iter.Value()
2018-04-25 05:45:22 -07:00
if err := m.cdc.UnmarshalBinary(v, ptr); err != nil {
2018-04-13 05:10:55 -07:00
panic(err)
}
2018-04-25 05:45:22 -07:00
s := string(iter.Key()[len(m.prefix)+6:])
2018-04-20 14:25:43 -07:00
index, err := strconv.ParseUint(s, 10, 64)
2018-04-13 05:10:55 -07:00
if err != nil {
panic(err)
}
if fn(ctx, index) {
break
}
}
iter.Close()
}
2018-04-25 05:45:22 -07:00
// IterateWrite implements ListMapper
func (m Mapper) IterateWrite(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, uint64) bool) {
length := m.Len(ctx)
2018-04-13 05:10:55 -07:00
for i := uint64(0); i < length; i++ {
2018-04-25 05:45:22 -07:00
if err := m.Get(ctx, i, ptr); err != nil {
2018-04-13 05:10:55 -07:00
continue
}
2018-04-12 06:46:55 -07:00
if fn(ctx, i) {
break
}
2018-03-14 11:20:15 -07:00
}
}
2018-04-25 05:45:22 -07:00
// LengthKey implements ListMapper
func (m Mapper) LengthKey() []byte {
return []byte(fmt.Sprintf("%s/length", m.prefix))
2018-04-12 06:46:55 -07:00
}
2018-04-25 05:45:22 -07:00
// ElemKey implements ListMapper
func (m Mapper) ElemKey(i uint64) []byte {
return []byte(fmt.Sprintf("%s/elem/%020d", m.prefix, i))
2018-04-12 06:46:55 -07:00
}
// QueueMapper is a Mapper interface that provides queue-like functions
// It panics when the element type cannot be (un/)marshalled by the codec
2018-03-14 11:20:15 -07:00
type QueueMapper interface {
2018-04-25 05:45:22 -07:00
// Push() inserts the elements to the rear of the queue
2018-03-15 02:59:36 -07:00
Push(sdk.Context, interface{})
2018-04-25 05:45:22 -07:00
// Popping/Peeking on an empty queue will cause panic
// The user should check IsEmpty() before doing any actions
2018-04-25 05:45:22 -07:00
// Peek() returns the element at the front of the queue without removing it
2018-04-13 05:10:55 -07:00
Peek(sdk.Context, interface{}) error
2018-04-25 05:45:22 -07:00
// Pop() returns the element at the front of the queue and removes it
2018-03-15 02:59:36 -07:00
Pop(sdk.Context)
2018-04-25 05:45:22 -07:00
// IsEmpty() checks if the queue is empty
2018-03-15 02:59:36 -07:00
IsEmpty(sdk.Context) bool
2018-04-20 14:25:43 -07:00
// Flush() removes elements it processed
2018-04-13 05:10:55 -07:00
// Return true in the continuation to break
// The interface{} is unmarshalled before the continuation is called
// Starts from the top(head) of the queue
2018-04-20 14:25:43 -07:00
// CONTRACT: Pop() or Push() should not be performed while flushing
2018-04-13 05:10:55 -07:00
Flush(sdk.Context, interface{}, func(sdk.Context) bool)
2018-03-14 11:20:15 -07:00
2018-04-18 08:55:40 -07:00
// Key for the index of top element
TopKey() []byte
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// NewQueueMapper constructs new QueueMapper
2018-04-12 06:46:55 -07:00
func NewQueueMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) QueueMapper {
2018-04-25 04:44:29 -07:00
return Mapper{
2018-04-12 06:46:55 -07:00
key: key,
cdc: cdc,
prefix: prefix,
2018-03-14 11:20:15 -07:00
}
}
2018-04-25 05:45:22 -07:00
func (m Mapper) getTop(store sdk.KVStore) (res uint64) {
bz := store.Get(m.TopKey())
2018-03-14 11:20:15 -07:00
if bz == nil {
2018-04-25 05:45:22 -07:00
store.Set(m.TopKey(), marshalUint64(m.cdc, 0))
2018-04-13 05:10:55 -07:00
return 0
2018-03-14 11:20:15 -07:00
}
2018-04-13 05:10:55 -07:00
2018-04-25 05:45:22 -07:00
if err := m.cdc.UnmarshalBinary(bz, &res); err != nil {
2018-03-14 11:20:15 -07:00
panic(err)
}
2018-04-13 05:10:55 -07:00
return
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
func (m Mapper) setTop(store sdk.KVStore, top uint64) {
bz := marshalUint64(m.cdc, top)
store.Set(m.TopKey(), bz)
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// Peek implements QueueMapper
func (m Mapper) Peek(ctx sdk.Context, ptr interface{}) error {
store := ctx.KVStore(m.key)
top := m.getTop(store)
return m.Get(ctx, top, ptr)
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// Pop implements QueueMapper
func (m Mapper) Pop(ctx sdk.Context) {
store := ctx.KVStore(m.key)
top := m.getTop(store)
m.Delete(ctx, top)
m.setTop(store, top+1)
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// IsEmpty implements QueueMapper
func (m Mapper) IsEmpty(ctx sdk.Context) bool {
store := ctx.KVStore(m.key)
top := m.getTop(store)
length := m.Len(ctx)
2018-04-13 05:10:55 -07:00
return top >= length
2018-03-14 11:20:15 -07:00
}
2018-04-25 05:45:22 -07:00
// Flush implements QueueMapper
func (m Mapper) Flush(ctx sdk.Context, ptr interface{}, fn func(sdk.Context) bool) {
store := ctx.KVStore(m.key)
top := m.getTop(store)
length := m.Len(ctx)
2018-03-14 11:20:15 -07:00
2018-04-13 05:10:55 -07:00
var i uint64
for i = top; i < length; i++ {
2018-04-25 05:45:22 -07:00
m.Get(ctx, i, ptr)
m.Delete(ctx, i)
2018-03-14 11:20:15 -07:00
if fn(ctx) {
break
}
}
2018-04-25 05:45:22 -07:00
m.setTop(store, i)
2018-04-12 09:19:28 -07:00
}
2018-04-25 05:45:22 -07:00
// TopKey implements QueueMapper
func (m Mapper) TopKey() []byte {
return []byte(fmt.Sprintf("%s/top", m.prefix))
2018-04-12 06:46:55 -07:00
}
2018-04-13 05:10:55 -07:00
func marshalUint64(cdc *wire.Codec, i uint64) []byte {
bz, err := cdc.MarshalBinary(i)
2018-03-14 11:20:15 -07:00
if err != nil {
panic(err)
}
return bz
}
2018-04-13 05:10:55 -07:00
func subspace(prefix []byte) (start, end []byte) {
end = make([]byte, len(prefix))
copy(end, prefix)
end[len(end)-1]++
return prefix, end
2018-03-14 11:20:15 -07:00
}