-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
53 lines (43 loc) · 1.61 KB
/
sequence.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
package collections
import (
"strconv"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// DefaultSequenceStart is the initial starting number of the Sequence.
const DefaultSequenceStart uint64 = 1
// Sequence defines a collection item which contains an always increasing number.
// Useful for those flows which require ever raising unique ids.
type Sequence struct {
sequence Item[uint64]
}
// NewSequence instantiates a new sequence object.
func NewSequence(sk storetypes.StoreKey, namespace Namespace) Sequence {
return Sequence{
sequence: NewItem[uint64](sk, namespace, uint64Value{}),
}
}
// Next returns the next available sequence number
// and also increases the sequence number count.
func (s Sequence) Next(ctx sdk.Context) uint64 {
// get current
seq := s.Peek(ctx)
// increase
s.sequence.Set(ctx, seq+1)
// return current
return seq
}
// Peek gets the next available sequence number without increasing it.
func (s Sequence) Peek(ctx sdk.Context) uint64 {
return s.sequence.GetOr(ctx, DefaultSequenceStart)
}
// Set hard resets the sequence to the provided number.
func (s Sequence) Set(ctx sdk.Context, u uint64) {
s.sequence.Set(ctx, u)
}
// uint64Value implements a ValueEncoder for uint64
type uint64Value struct{}
func (u uint64Value) Encode(value uint64) []byte { return sdk.Uint64ToBigEndian(value) }
func (u uint64Value) Decode(b []byte) uint64 { return sdk.BigEndianToUint64(b) }
func (u uint64Value) Stringify(value uint64) string { return strconv.FormatUint(value, 10) }
func (u uint64Value) Name() string { return "uint64" }