forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsequence.go
49 lines (42 loc) · 1.35 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
package collections
import (
"context"
"errors"
)
// DefaultSequenceStart defines the default starting number of a sequence.
const DefaultSequenceStart uint64 = 0
// Sequence builds on top of an Item, and represents a monotonically increasing number.
type Sequence Item[uint64]
// NewSequence instantiates a new sequence given
// a Schema, a Prefix and humanized name for the sequence.
func NewSequence(schema *SchemaBuilder, prefix Prefix, name string) Sequence {
return (Sequence)(NewItem(schema, prefix, name, Uint64Value))
}
// Peek returns the current sequence value, if no number
// is set then the DefaultSequenceStart is returned.
// Errors on encoding issues.
func (s Sequence) Peek(ctx context.Context) (uint64, error) {
n, err := (Item[uint64])(s).Get(ctx)
switch {
case err == nil:
return n, nil
case errors.Is(err, ErrNotFound):
return DefaultSequenceStart, nil
default:
return 0, err
}
}
// Next returns the next sequence number, and sets the next expected sequence.
// Errors on encoding issues.
func (s Sequence) Next(ctx context.Context) (uint64, error) {
seq, err := s.Peek(ctx)
if err != nil {
return 0, err
}
return seq, s.Set(ctx, seq+1)
}
// Set hard resets the sequence to the provided value.
// Errors on encoding issues.
func (s Sequence) Set(ctx context.Context, value uint64) error {
return (Item[uint64])(s).Set(ctx, value)
}