expose QueueInfo

This commit is contained in:
mossid 2018-04-12 18:19:28 +02:00
parent fbfdbd4266
commit 373e408fad
2 changed files with 21 additions and 13 deletions

View File

@ -1,4 +1,4 @@
package stdlib package lib
import ( import (
"fmt" "fmt"
@ -129,6 +129,7 @@ type QueueMapper interface {
IsEmpty(sdk.Context) bool IsEmpty(sdk.Context) bool
// Iterate() removes elements it processed; return true in the continuation to break // Iterate() removes elements it processed; return true in the continuation to break
Iterate(sdk.Context, interface{}, func(sdk.Context) bool) Iterate(sdk.Context, interface{}, func(sdk.Context) bool)
Info(sdk.Context) QueueInfo
} }
type queueMapper struct { type queueMapper struct {
@ -153,30 +154,30 @@ func NewQueueMapper(cdc *wire.Codec, key sdk.StoreKey, prefix string) QueueMappe
} }
} }
type queueInfo struct { type QueueInfo struct {
// begin <= elems < end // begin <= elems < end
Begin int64 Begin int64
End int64 End int64
} }
func (info queueInfo) validateBasic() error { func (info QueueInfo) validateBasic() error {
if info.End < info.Begin || info.Begin < 0 || info.End < 0 { if info.End < info.Begin || info.Begin < 0 || info.End < 0 {
return fmt.Errorf("Invalid queue information: {Begin: %d, End: %d}", info.Begin, info.End) return fmt.Errorf("Invalid queue information: {Begin: %d, End: %d}", info.Begin, info.End)
} }
return nil return nil
} }
func (info queueInfo) isEmpty() bool { func (info QueueInfo) isEmpty() bool {
return info.Begin == info.End return info.Begin == info.End
} }
func (qm queueMapper) getQueueInfo(store sdk.KVStore) queueInfo { func (qm queueMapper) getQueueInfo(store sdk.KVStore) QueueInfo {
bz := store.Get(qm.InfoKey()) bz := store.Get(qm.InfoKey())
if bz == nil { if bz == nil {
store.Set(qm.InfoKey(), marshalQueueInfo(qm.cdc, queueInfo{0, 0})) store.Set(qm.InfoKey(), marshalQueueInfo(qm.cdc, QueueInfo{0, 0}))
return queueInfo{0, 0} return QueueInfo{0, 0}
} }
var info queueInfo var info QueueInfo
if err := qm.cdc.UnmarshalBinary(bz, &info); err != nil { if err := qm.cdc.UnmarshalBinary(bz, &info); err != nil {
panic(err) panic(err)
} }
@ -186,7 +187,7 @@ func (qm queueMapper) getQueueInfo(store sdk.KVStore) queueInfo {
return info return info
} }
func (qm queueMapper) setQueueInfo(store sdk.KVStore, info queueInfo) { func (qm queueMapper) setQueueInfo(store sdk.KVStore, info QueueInfo) {
bz, err := qm.cdc.MarshalBinary(info) bz, err := qm.cdc.MarshalBinary(info)
if err != nil { if err != nil {
panic(err) panic(err)
@ -231,8 +232,7 @@ func (qm queueMapper) Iterate(ctx sdk.Context, ptr interface{}, fn func(sdk.Cont
var i int64 var i int64
for i = info.Begin; i < info.End; i++ { for i = info.Begin; i < info.End; i++ {
qm.lm.Get(ctx, i, ptr) qm.lm.Get(ctx, i, ptr)
key := marshalInt64(qm.cdc, i) qm.lm.Delete(ctx, i)
store.Delete(key)
if fn(ctx) { if fn(ctx) {
break break
} }
@ -242,11 +242,17 @@ func (qm queueMapper) Iterate(ctx sdk.Context, ptr interface{}, fn func(sdk.Cont
qm.setQueueInfo(store, info) qm.setQueueInfo(store, info)
} }
func (qm queueMapper) Info(ctx sdk.Context) QueueInfo {
store := ctx.KVStore(qm.key)
return qm.getQueueInfo(store)
}
func (qm queueMapper) InfoKey() []byte { func (qm queueMapper) InfoKey() []byte {
return []byte(fmt.Sprintf("%s/%s", qm.prefix, qm.ik)) return []byte(fmt.Sprintf("%s/%s", qm.prefix, qm.ik))
} }
func marshalQueueInfo(cdc *wire.Codec, info queueInfo) []byte { func marshalQueueInfo(cdc *wire.Codec, info QueueInfo) []byte {
bz, err := cdc.MarshalBinary(info) bz, err := cdc.MarshalBinary(info)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -1,4 +1,4 @@
package stdlib package lib
import ( import (
"testing" "testing"
@ -84,6 +84,8 @@ func TestQueueMapper(t *testing.T) {
}) })
assert.False(t, qm.IsEmpty(ctx)) assert.False(t, qm.IsEmpty(ctx))
assert.Equal(t, QueueInfo{3, 4}, qm.Info(ctx))
qm.Pop(ctx) qm.Pop(ctx)
assert.True(t, qm.IsEmpty(ctx)) assert.True(t, qm.IsEmpty(ctx))
} }