add comments for golint
This commit is contained in:
parent
8ff316c99e
commit
06d47bebed
|
@ -42,7 +42,7 @@ func (ms multiStore) LoadLatestVersion() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (md multiStore) LoadVersion(ver int64) error {
|
||||
func (ms multiStore) LoadVersion(ver int64) error {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
wire "github.com/cosmos/cosmos-sdk/wire"
|
||||
)
|
||||
|
||||
// Mapper defines a primitive mapper type
|
||||
type Mapper struct {
|
||||
key sdk.StoreKey
|
||||
cdc *wire.Codec
|
||||
|
@ -16,24 +17,29 @@ type Mapper struct {
|
|||
|
||||
// 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 {
|
||||
|
||||
// Len() returns the length of the list
|
||||
// The length is only increased by Push() and not decreased
|
||||
// ListMapper dosen't check if an index is in bounds
|
||||
// The user should check Len() before doing any actions
|
||||
Len(sdk.Context) uint64
|
||||
|
||||
// Get() returns the element by its index
|
||||
Get(sdk.Context, uint64, interface{}) error
|
||||
|
||||
// Set() stores the element to the given position
|
||||
// Setting element out of range will break length counting
|
||||
// Use Push() instead of Set() to append a new element
|
||||
Set(sdk.Context, uint64, interface{})
|
||||
|
||||
// Delete() deletes the element in the given position
|
||||
// Other elements' indices are preserved after deletion
|
||||
// Panics when the index is out of range
|
||||
Delete(sdk.Context, uint64)
|
||||
|
||||
// Push will increase the length when it is called
|
||||
// The other methods does not modify the length
|
||||
// Push() inserts the element to the end of the list
|
||||
// It will increase the length when it is called
|
||||
Push(sdk.Context, interface{})
|
||||
|
||||
// Iterate*() is used to iterate over all existing elements in the list
|
||||
|
@ -54,6 +60,7 @@ type ListMapper interface {
|
|||
ElemKey(uint64) []byte
|
||||
}
|
||||
|
||||
// NewListMapper constructs new ListMapper
|
||||
func NewListMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) ListMapper {
|
||||
return Mapper{
|
||||
key: key,
|
||||
|
@ -62,62 +69,68 @@ func NewListMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) ListMapper
|
|||
}
|
||||
}
|
||||
|
||||
func (lm Mapper) Len(ctx sdk.Context) uint64 {
|
||||
store := ctx.KVStore(lm.key)
|
||||
bz := store.Get(lm.LengthKey())
|
||||
// Len implements ListMapper
|
||||
func (m Mapper) Len(ctx sdk.Context) uint64 {
|
||||
store := ctx.KVStore(m.key)
|
||||
bz := store.Get(m.LengthKey())
|
||||
if bz == nil {
|
||||
zero, err := lm.cdc.MarshalBinary(0)
|
||||
zero, err := m.cdc.MarshalBinary(0)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
store.Set(lm.LengthKey(), zero)
|
||||
store.Set(m.LengthKey(), zero)
|
||||
return 0
|
||||
}
|
||||
var res uint64
|
||||
if err := lm.cdc.UnmarshalBinary(bz, &res); err != nil {
|
||||
if err := m.cdc.UnmarshalBinary(bz, &res); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (lm Mapper) Get(ctx sdk.Context, index uint64, ptr interface{}) error {
|
||||
store := ctx.KVStore(lm.key)
|
||||
bz := store.Get(lm.ElemKey(index))
|
||||
return lm.cdc.UnmarshalBinary(bz, ptr)
|
||||
// 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)
|
||||
}
|
||||
|
||||
func (lm Mapper) Set(ctx sdk.Context, index uint64, value interface{}) {
|
||||
store := ctx.KVStore(lm.key)
|
||||
bz, err := lm.cdc.MarshalBinary(value)
|
||||
// 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)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
store.Set(lm.ElemKey(index), bz)
|
||||
store.Set(m.ElemKey(index), bz)
|
||||
}
|
||||
|
||||
func (lm Mapper) Delete(ctx sdk.Context, index uint64) {
|
||||
store := ctx.KVStore(lm.key)
|
||||
store.Delete(lm.ElemKey(index))
|
||||
// Delete implements ListMapper
|
||||
func (m Mapper) Delete(ctx sdk.Context, index uint64) {
|
||||
store := ctx.KVStore(m.key)
|
||||
store.Delete(m.ElemKey(index))
|
||||
}
|
||||
|
||||
func (lm Mapper) Push(ctx sdk.Context, value interface{}) {
|
||||
length := lm.Len(ctx)
|
||||
lm.Set(ctx, length, value)
|
||||
// Push implements ListMapper
|
||||
func (m Mapper) Push(ctx sdk.Context, value interface{}) {
|
||||
length := m.Len(ctx)
|
||||
m.Set(ctx, length, value)
|
||||
|
||||
store := ctx.KVStore(lm.key)
|
||||
store.Set(lm.LengthKey(), marshalUint64(lm.cdc, length+1))
|
||||
store := ctx.KVStore(m.key)
|
||||
store.Set(m.LengthKey(), marshalUint64(m.cdc, length+1))
|
||||
}
|
||||
|
||||
func (lm Mapper) 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)))
|
||||
// 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)))
|
||||
iter := store.Iterator(start, end)
|
||||
for ; iter.Valid(); iter.Next() {
|
||||
v := iter.Value()
|
||||
if err := lm.cdc.UnmarshalBinary(v, ptr); err != nil {
|
||||
if err := m.cdc.UnmarshalBinary(v, ptr); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
s := string(iter.Key()[len(lm.prefix)+6:])
|
||||
s := string(iter.Key()[len(m.prefix)+6:])
|
||||
index, err := strconv.ParseUint(s, 10, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -130,11 +143,12 @@ func (lm Mapper) IterateRead(ctx sdk.Context, ptr interface{}, fn func(sdk.Conte
|
|||
iter.Close()
|
||||
}
|
||||
|
||||
func (lm Mapper) IterateWrite(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, uint64) bool) {
|
||||
length := lm.Len(ctx)
|
||||
// IterateWrite implements ListMapper
|
||||
func (m Mapper) IterateWrite(ctx sdk.Context, ptr interface{}, fn func(sdk.Context, uint64) bool) {
|
||||
length := m.Len(ctx)
|
||||
|
||||
for i := uint64(0); i < length; i++ {
|
||||
if err := lm.Get(ctx, i, ptr); err != nil {
|
||||
if err := m.Get(ctx, i, ptr); err != nil {
|
||||
continue
|
||||
}
|
||||
if fn(ctx, i) {
|
||||
|
@ -143,23 +157,32 @@ func (lm Mapper) IterateWrite(ctx sdk.Context, ptr interface{}, fn func(sdk.Cont
|
|||
}
|
||||
}
|
||||
|
||||
func (lm Mapper) LengthKey() []byte {
|
||||
return []byte(fmt.Sprintf("%s/length", lm.prefix))
|
||||
// LengthKey implements ListMapper
|
||||
func (m Mapper) LengthKey() []byte {
|
||||
return []byte(fmt.Sprintf("%s/length", m.prefix))
|
||||
}
|
||||
|
||||
func (lm Mapper) ElemKey(i uint64) []byte {
|
||||
return []byte(fmt.Sprintf("%s/elem/%020d", lm.prefix, i))
|
||||
// ElemKey implements ListMapper
|
||||
func (m Mapper) ElemKey(i uint64) []byte {
|
||||
return []byte(fmt.Sprintf("%s/elem/%020d", m.prefix, i))
|
||||
}
|
||||
|
||||
// QueueMapper is a Mapper interface that provides queue-like functions
|
||||
// It panics when the element type cannot be (un/)marshalled by the codec
|
||||
|
||||
type QueueMapper interface {
|
||||
// Push() inserts the elements to the rear of the queue
|
||||
Push(sdk.Context, interface{})
|
||||
|
||||
// Popping/Peeking on an empty queue will cause panic
|
||||
// The user should check IsEmpty() before doing any actions
|
||||
|
||||
// Peek() returns the element at the front of the queue without removing it
|
||||
Peek(sdk.Context, interface{}) error
|
||||
|
||||
// Pop() returns the element at the front of the queue and removes it
|
||||
Pop(sdk.Context)
|
||||
|
||||
// IsEmpty() checks if the queue is empty
|
||||
IsEmpty(sdk.Context) bool
|
||||
|
||||
// Flush() removes elements it processed
|
||||
|
@ -173,6 +196,7 @@ type QueueMapper interface {
|
|||
TopKey() []byte
|
||||
}
|
||||
|
||||
// NewQueueMapper constructs new QueueMapper
|
||||
func NewQueueMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) QueueMapper {
|
||||
return Mapper{
|
||||
key: key,
|
||||
|
@ -181,64 +205,69 @@ func NewQueueMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) QueueMappe
|
|||
}
|
||||
}
|
||||
|
||||
func (qm Mapper) getTop(store sdk.KVStore) (res uint64) {
|
||||
bz := store.Get(qm.TopKey())
|
||||
func (m Mapper) getTop(store sdk.KVStore) (res uint64) {
|
||||
bz := store.Get(m.TopKey())
|
||||
if bz == nil {
|
||||
store.Set(qm.TopKey(), marshalUint64(qm.cdc, 0))
|
||||
store.Set(m.TopKey(), marshalUint64(m.cdc, 0))
|
||||
return 0
|
||||
}
|
||||
|
||||
if err := qm.cdc.UnmarshalBinary(bz, &res); err != nil {
|
||||
if err := m.cdc.UnmarshalBinary(bz, &res); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (qm Mapper) setTop(store sdk.KVStore, top uint64) {
|
||||
bz := marshalUint64(qm.cdc, top)
|
||||
store.Set(qm.TopKey(), bz)
|
||||
func (m Mapper) setTop(store sdk.KVStore, top uint64) {
|
||||
bz := marshalUint64(m.cdc, top)
|
||||
store.Set(m.TopKey(), bz)
|
||||
}
|
||||
|
||||
func (qm Mapper) Peek(ctx sdk.Context, ptr interface{}) error {
|
||||
store := ctx.KVStore(qm.key)
|
||||
top := qm.getTop(store)
|
||||
return qm.Get(ctx, top, ptr)
|
||||
// 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)
|
||||
}
|
||||
|
||||
func (qm Mapper) Pop(ctx sdk.Context) {
|
||||
store := ctx.KVStore(qm.key)
|
||||
top := qm.getTop(store)
|
||||
qm.Delete(ctx, top)
|
||||
qm.setTop(store, top+1)
|
||||
// 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)
|
||||
}
|
||||
|
||||
func (qm Mapper) IsEmpty(ctx sdk.Context) bool {
|
||||
store := ctx.KVStore(qm.key)
|
||||
top := qm.getTop(store)
|
||||
length := qm.Len(ctx)
|
||||
// IsEmpty implements QueueMapper
|
||||
func (m Mapper) IsEmpty(ctx sdk.Context) bool {
|
||||
store := ctx.KVStore(m.key)
|
||||
top := m.getTop(store)
|
||||
length := m.Len(ctx)
|
||||
return top >= length
|
||||
}
|
||||
|
||||
func (qm Mapper) Flush(ctx sdk.Context, ptr interface{}, fn func(sdk.Context) bool) {
|
||||
store := ctx.KVStore(qm.key)
|
||||
top := qm.getTop(store)
|
||||
length := qm.Len(ctx)
|
||||
// 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)
|
||||
|
||||
var i uint64
|
||||
for i = top; i < length; i++ {
|
||||
qm.Get(ctx, i, ptr)
|
||||
qm.Delete(ctx, i)
|
||||
m.Get(ctx, i, ptr)
|
||||
m.Delete(ctx, i)
|
||||
if fn(ctx) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
qm.setTop(store, i)
|
||||
m.setTop(store, i)
|
||||
}
|
||||
|
||||
func (qm Mapper) TopKey() []byte {
|
||||
return []byte(fmt.Sprintf("%s/top", qm.prefix))
|
||||
// TopKey implements QueueMapper
|
||||
func (m Mapper) TopKey() []byte {
|
||||
return []byte(fmt.Sprintf("%s/top", m.prefix))
|
||||
}
|
||||
|
||||
func marshalUint64(cdc *wire.Codec, i uint64) []byte {
|
||||
|
|
Loading…
Reference in New Issue