<!--
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: https://github.com/cosmos/cosmos-sdk/issues/9237, https://github.com/cosmos/cosmos-sdk/discussions/9156
This is the first step towards the migration of the [Table-Store (ORM) package](c99dbedd1f/orm) currently in regen-ledger into the SDK. This won't be exposed as a public API to start with but rather be internal to `x/group`.
This first PR brings these core concepts:
- `table` is the high level object for storage mapper functionality where entities are stored by an unique identifier called `RowID`
- `Indexable` types can be used to setup new tables.
There will be follow-up PRs for adding the following features:
- table types with auto-incrementing `uint64` primary keys and natural primary keys
- secondary indexes
- iterator and pagination
- import/export genesis
---
### 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
- [x] 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) _not applicable_
- [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` _not applicable_
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [x] 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)
82 lines
2.4 KiB
Go
82 lines
2.4 KiB
Go
package orm
|
|
|
|
import (
|
|
"encoding/binary"
|
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/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 errors.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
|
|
}
|