<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description ref: #9237, #9156 This PR is a follow-up of #10415 and #9751. It adds multi-key secondary indexes, iterator and pagination support. There will be one last follow-up PR for adding import/export genesis features. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
83 lines
2.5 KiB
Go
83 lines
2.5 KiB
Go
package orm
|
|
|
|
import (
|
|
"encoding/binary"
|
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/x/group/errors"
|
|
)
|
|
|
|
// sequenceStorageKey is a fix key to read/ write data on the storage layer
|
|
var sequenceStorageKey = []byte{0x1}
|
|
|
|
// sequence is a persistent unique key generator based on a counter.
|
|
type Sequence struct {
|
|
prefix byte
|
|
}
|
|
|
|
func NewSequence(prefix byte) Sequence {
|
|
return Sequence{
|
|
prefix: prefix,
|
|
}
|
|
}
|
|
|
|
// NextVal increments and persists the counter by one and returns the value.
|
|
func (s Sequence) NextVal(store sdk.KVStore) uint64 {
|
|
pStore := prefix.NewStore(store, []byte{s.prefix})
|
|
v := pStore.Get(sequenceStorageKey)
|
|
seq := DecodeSequence(v)
|
|
seq++
|
|
pStore.Set(sequenceStorageKey, EncodeSequence(seq))
|
|
return seq
|
|
}
|
|
|
|
// CurVal returns the last value used. 0 if none.
|
|
func (s Sequence) CurVal(store sdk.KVStore) uint64 {
|
|
pStore := prefix.NewStore(store, []byte{s.prefix})
|
|
v := pStore.Get(sequenceStorageKey)
|
|
return DecodeSequence(v)
|
|
}
|
|
|
|
// PeekNextVal returns the CurVal + increment step. Not persistent.
|
|
func (s Sequence) PeekNextVal(store sdk.KVStore) uint64 {
|
|
pStore := prefix.NewStore(store, []byte{s.prefix})
|
|
v := pStore.Get(sequenceStorageKey)
|
|
return DecodeSequence(v) + 1
|
|
}
|
|
|
|
// InitVal sets the start value for the sequence. It must be called only once on an empty DB.
|
|
// Otherwise an error is returned when the key exists. The given start value is stored as current
|
|
// value.
|
|
//
|
|
// It is recommended to call this method only for a sequence start value other than `1` as the
|
|
// method consumes unnecessary gas otherwise. A scenario would be an import from genesis.
|
|
func (s Sequence) InitVal(store sdk.KVStore, seq uint64) error {
|
|
pStore := prefix.NewStore(store, []byte{s.prefix})
|
|
if pStore.Has(sequenceStorageKey) {
|
|
return sdkerrors.Wrap(errors.ErrORMUniqueConstraint, "already initialized")
|
|
}
|
|
pStore.Set(sequenceStorageKey, EncodeSequence(seq))
|
|
return nil
|
|
}
|
|
|
|
// DecodeSequence converts the binary representation into an Uint64 value.
|
|
func DecodeSequence(bz []byte) uint64 {
|
|
if bz == nil {
|
|
return 0
|
|
}
|
|
val := binary.BigEndian.Uint64(bz)
|
|
return val
|
|
}
|
|
|
|
// EncodedSeqLength number of bytes used for the binary representation of a sequence value.
|
|
const EncodedSeqLength = 8
|
|
|
|
// EncodeSequence converts the sequence value into the binary representation.
|
|
func EncodeSequence(val uint64) []byte {
|
|
bz := make([]byte, EncodedSeqLength)
|
|
binary.BigEndian.PutUint64(bz, val)
|
|
return bz
|
|
}
|