-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(collections): add sequence #14364
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1c534f
feat(collections): add sequence
5543a18
chore: CHANGELOG.md
ebbe4f8
Update collections/sequence.go
testinginprod be16ce5
change: address feedback
39f8244
Merge branch 'main' into tip/collections-sequence
testinginprod c2df135
Merge branch 'main' into tip/collections-sequence
fdymylja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package collections | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
// DefaultSequenceStart defines the default starting number of a sequence. | ||
const DefaultSequenceStart uint64 = 1 | ||
|
||
// 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 humanised name for the sequence. | ||
func NewSequence(schema Schema, 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package collections | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestSequence(t *testing.T) { | ||
sk, ctx := deps() | ||
schema := NewSchema(sk) | ||
seq := NewSequence(schema, NewPrefix(0), "sequence") | ||
// initially the first available number is DefaultSequenceStart | ||
n, err := seq.Peek(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, DefaultSequenceStart, n) | ||
|
||
// when we call next when sequence is still unset the first expected value is DefaultSequenceStart | ||
n, err = seq.Next(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, DefaultSequenceStart, n) | ||
// when we call peek after the first number is set, then the next expected sequence is DefaultSequenceStart + 1 | ||
n, err = seq.Peek(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, DefaultSequenceStart+1, n) | ||
|
||
// set | ||
err = seq.Set(ctx, 10) | ||
require.NoError(t, err) | ||
n, err = seq.Peek(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, n, uint64(10)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naive q: what is the usecase for this type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a common type, EG: auth account number, everything that uses an ever increasing unique ID.