cosmos-sdk/types/lib/stdlib.go

275 lines
6.5 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"
"strings"
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"
)
// 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-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
Get(sdk.Context, uint64, interface{}) error
// Setting element out of range is harmful
// Use Push() instead of Set() to append a new element
Set(sdk.Context, uint64, interface{})
Delete(sdk.Context, uint64)
2018-03-15 02:59:36 -07:00
Push(sdk.Context, interface{})
2018-04-13 05:10:55 -07:00
2018-04-18 07:56:39 -07:00
// Getter/Setter for meta information - can be customized
GetMeta(sdk.Context, interface{}) error
SetMeta(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
// CONTRACT: No writes may happen within a domain while an iterator exists over it.
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
}
type listMapper struct {
2018-04-12 06:46:55 -07:00
key sdk.StoreKey
cdc *wire.Codec
prefix string
2018-03-14 11:20:15 -07:00
}
2018-04-12 06:46:55 -07:00
func NewListMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) ListMapper {
2018-03-14 11:20:15 -07:00
return listMapper{
2018-04-12 06:46:55 -07:00
key: key,
cdc: cdc,
prefix: prefix,
2018-03-14 11:20:15 -07:00
}
}
2018-04-13 05:10:55 -07:00
func (lm listMapper) Len(ctx sdk.Context) uint64 {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(lm.key)
2018-04-12 06:46:55 -07:00
bz := store.Get(lm.LengthKey())
2018-03-14 11:20:15 -07:00
if bz == nil {
zero, err := lm.cdc.MarshalBinary(0)
if err != nil {
panic(err)
}
2018-04-12 06:46:55 -07:00
store.Set(lm.LengthKey(), zero)
2018-03-14 11:20:15 -07:00
return 0
}
2018-04-13 05:10:55 -07:00
var res uint64
2018-03-14 11:20:15 -07:00
if err := lm.cdc.UnmarshalBinary(bz, &res); err != nil {
panic(err)
}
return res
}
2018-04-13 05:10:55 -07:00
func (lm listMapper) Get(ctx sdk.Context, index uint64, ptr interface{}) error {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(lm.key)
2018-04-12 06:46:55 -07:00
bz := store.Get(lm.ElemKey(index))
2018-04-13 05:10:55 -07:00
return lm.cdc.UnmarshalBinary(bz, ptr)
2018-03-14 11:20:15 -07:00
}
2018-04-13 05:10:55 -07:00
func (lm listMapper) Set(ctx sdk.Context, index uint64, value interface{}) {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(lm.key)
bz, err := lm.cdc.MarshalBinary(value)
if err != nil {
panic(err)
}
2018-04-12 06:46:55 -07:00
store.Set(lm.ElemKey(index), bz)
}
2018-04-13 05:10:55 -07:00
func (lm listMapper) Delete(ctx sdk.Context, index uint64) {
2018-04-12 06:46:55 -07:00
store := ctx.KVStore(lm.key)
store.Delete(lm.ElemKey(index))
2018-03-14 11:20:15 -07:00
}
2018-03-15 02:59:36 -07:00
func (lm listMapper) Push(ctx sdk.Context, value interface{}) {
2018-03-14 11:20:15 -07:00
length := lm.Len(ctx)
lm.Set(ctx, length, value)
store := ctx.KVStore(lm.key)
2018-04-13 05:10:55 -07:00
store.Set(lm.LengthKey(), marshalUint64(lm.cdc, length+1))
2018-03-14 11:20:15 -07:00
}
2018-04-18 07:56:39 -07:00
func (lm listMapper) GetMeta(ctx sdk.Context, ptr interface{}) error {
store := ctx.KVStore(lm.key)
bz := store.Get(lm.MetaKey())
return lm.cdc.UnmarshalBinary(bz, ptr)
}
func (lm listMapper) SetMeta(ctx sdk.Context, value interface{}) {
store := ctx.KVStore(lm.key)
bz, err := lm.cdc.MarshalBinary(value)
if err != nil {
panic(err)
}
store.Set(lm.MetaKey(), bz)
}
2018-04-13 05:10:55 -07:00
func (lm listMapper) IterateRead(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, uint64) bool) {
store := ctx.KVStore(lm.key)
start, end := subspace([]byte(fmt.Sprintf("%s/elem/", lm.prefix)))
iter := store.Iterator(start, end)
for ; iter.Valid(); iter.Next() {
v := iter.Value()
if err := lm.cdc.UnmarshalBinary(v, ptr); err != nil {
panic(err)
}
s := strings.Split(string(iter.Key()), "/")
index, err := strconv.ParseUint(s[len(s)-1], 10, 64)
if err != nil {
panic(err)
}
if fn(ctx, index) {
break
}
}
iter.Close()
}
func (lm listMapper) IterateWrite(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, uint64) bool) {
2018-03-14 11:20:15 -07:00
length := lm.Len(ctx)
2018-04-13 05:10:55 -07:00
for i := uint64(0); i < length; i++ {
if err := lm.Get(ctx, i, ptr); err != nil {
continue
}
2018-04-12 06:46:55 -07:00
if fn(ctx, i) {
break
}
2018-03-14 11:20:15 -07:00
}
}
2018-04-12 06:46:55 -07:00
func (lm listMapper) LengthKey() []byte {
2018-04-13 05:10:55 -07:00
return []byte(fmt.Sprintf("%s/length", lm.prefix))
2018-04-12 06:46:55 -07:00
}
2018-04-13 05:10:55 -07:00
func (lm listMapper) ElemKey(i uint64) []byte {
return []byte(fmt.Sprintf("%s/elem/%020d", lm.prefix, i))
2018-04-12 06:46:55 -07:00
}
2018-04-18 07:56:39 -07:00
func (lm listMapper)
// 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-03-15 02:59:36 -07:00
Push(sdk.Context, interface{})
// Popping/Peeking on an empty queue will cause panic
// The user should check IsEmpty() before doing any actions
2018-04-13 05:10:55 -07:00
Peek(sdk.Context, interface{}) error
2018-03-15 02:59:36 -07:00
Pop(sdk.Context)
IsEmpty(sdk.Context) bool
2018-04-13 05:10:55 -07:00
// Iterate() removes elements it processed
// Return true in the continuation to break
// The interface{} is unmarshalled before the continuation is called
// Starts from the top(head) of the queue
Flush(sdk.Context, interface{}, func(sdk.Context) bool)
2018-03-14 11:20:15 -07:00
}
type queueMapper struct {
2018-04-12 06:46:55 -07:00
key sdk.StoreKey
cdc *wire.Codec
prefix string
lm ListMapper
2018-03-14 11:20:15 -07:00
}
2018-04-12 06:46:55 -07:00
func NewQueueMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) QueueMapper {
2018-03-14 11:20:15 -07:00
return queueMapper{
2018-04-12 06:46:55 -07:00
key: key,
cdc: cdc,
prefix: prefix,
2018-04-13 05:10:55 -07:00
lm: NewListMapper(cdc, key, prefix+"list"),
2018-03-14 11:20:15 -07:00
}
}
2018-04-13 05:10:55 -07:00
func (qm queueMapper) getTop(store sdk.KVStore) (res uint64) {
bz := store.Get(qm.TopKey())
2018-03-14 11:20:15 -07:00
if bz == nil {
2018-04-13 05:10:55 -07:00
store.Set(qm.TopKey(), marshalUint64(qm.cdc, 0))
return 0
2018-03-14 11:20:15 -07:00
}
2018-04-13 05:10:55 -07:00
if err := qm.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-13 05:10:55 -07:00
func (qm queueMapper) setTop(store sdk.KVStore, top uint64) {
bz := marshalUint64(qm.cdc, top)
store.Set(qm.TopKey(), bz)
2018-03-14 11:20:15 -07:00
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) Push(ctx sdk.Context, value interface{}) {
2018-04-13 05:10:55 -07:00
qm.lm.Push(ctx, value)
2018-03-14 11:20:15 -07:00
}
2018-04-13 05:10:55 -07:00
func (qm queueMapper) Peek(ctx sdk.Context, ptr interface{}) error {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
2018-04-13 05:10:55 -07:00
top := qm.getTop(store)
return qm.lm.Get(ctx, top, ptr)
2018-03-14 11:20:15 -07:00
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) Pop(ctx sdk.Context) {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
2018-04-13 05:10:55 -07:00
top := qm.getTop(store)
qm.lm.Delete(ctx, top)
qm.setTop(store, top+1)
2018-03-14 11:20:15 -07:00
}
2018-03-15 02:59:36 -07:00
func (qm queueMapper) IsEmpty(ctx sdk.Context) bool {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
2018-04-13 05:10:55 -07:00
top := qm.getTop(store)
length := qm.lm.Len(ctx)
return top >= length
2018-03-14 11:20:15 -07:00
}
2018-04-13 05:10:55 -07:00
func (qm queueMapper) Flush(ctx sdk.Context, ptr interface{}, fn func(sdk.Context) bool) {
2018-03-14 11:20:15 -07:00
store := ctx.KVStore(qm.key)
2018-04-13 05:10:55 -07:00
top := qm.getTop(store)
length := qm.lm.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-12 06:46:55 -07:00
qm.lm.Get(ctx, i, ptr)
2018-04-12 09:19:28 -07:00
qm.lm.Delete(ctx, i)
2018-03-14 11:20:15 -07:00
if fn(ctx) {
break
}
}
2018-04-13 05:10:55 -07:00
qm.setTop(store, i)
2018-04-12 09:19:28 -07:00
}
2018-04-13 05:10:55 -07:00
func (qm queueMapper) TopKey() []byte {
return []byte(fmt.Sprintf("%s/top", qm.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
}