forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
88 lines (75 loc) · 2.02 KB
/
queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package store
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Key for the top element position in the queue
func TopKey() []byte {
return []byte{0x02}
}
// Queue is a List wrapper that provides queue-like functions
// It panics when the element type cannot be (un/)marshalled by the codec
type Queue struct {
List List
}
// NewQueue constructs new Queue
func NewQueue(cdc *codec.Codec, store sdk.KVStore) Queue {
return Queue{NewList(cdc, store)}
}
func (m Queue) getTop() (res uint64) {
bz := m.List.store.Get(TopKey())
if bz == nil {
return 0
}
m.List.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &res)
return
}
func (m Queue) setTop(top uint64) {
bz := m.List.cdc.MustMarshalBinaryLengthPrefixed(top)
m.List.store.Set(TopKey(), bz)
}
// Push() inserts the elements to the rear of the queue
func (m Queue) Push(value interface{}) {
m.List.Push(value)
}
// 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
func (m Queue) Peek(ptr interface{}) error {
top := m.getTop()
return m.List.Get(top, ptr)
}
// Pop() returns the element at the front of the queue and removes it
func (m Queue) Pop() {
top := m.getTop()
m.List.Delete(top)
m.setTop(top + 1)
}
// IsEmpty() checks if the queue is empty
func (m Queue) IsEmpty() bool {
top := m.getTop()
length := m.List.Len()
return top >= length
}
// Flush() 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
// CONTRACT: Pop() or Push() should not be performed while flushing
func (m Queue) Flush(ptr interface{}, fn func() bool) {
top := m.getTop()
length := m.List.Len()
var i uint64
for i = top; i < length; i++ {
err := m.List.Get(i, ptr)
if err != nil {
// TODO: Handle with #870
panic(err)
}
m.List.Delete(i)
if fn() {
break
}
}
m.setTop(i)
}