cosmos-sdk/x/group/internal/orm/key_codec.go
Marie Gauthier c5b879a03d
feat: Add Table-Store (aka ORM) package - Index and Iterator (#10451)
<!--
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)
2021-11-09 18:01:27 +00:00

101 lines
2.9 KiB
Go

package orm
import (
"fmt"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/group/errors"
)
// MaxBytesLen is the maximum allowed length for a key part of type []byte
const MaxBytesLen = 255
// buildKeyFromParts encodes and concatenates primary key and index parts.
// They can be []byte, string, and integer types. The function will return
// an error if there is a part of any other type.
// Key parts, except the last part, follow these rules:
// - []byte is encoded with a single byte length prefix
// - strings are null-terminated
// - integers are encoded using 8 byte big endian.
func buildKeyFromParts(parts []interface{}) ([]byte, error) {
bytesSlice := make([][]byte, len(parts))
totalLen := 0
var err error
for i, part := range parts {
bytesSlice[i], err = keyPartBytes(part, len(parts) > 1 && i == len(parts)-1)
if err != nil {
return nil, err
}
totalLen += len(bytesSlice[i])
}
key := make([]byte, 0, totalLen)
for _, bs := range bytesSlice {
key = append(key, bs...)
}
return key, nil
}
func keyPartBytes(part interface{}, last bool) ([]byte, error) {
switch v := part.(type) {
case []byte:
if last || len(v) == 0 {
return v, nil
}
return AddLengthPrefix(v), nil
case string:
if last || len(v) == 0 {
return []byte(v), nil
}
return NullTerminatedBytes(v), nil
case uint64:
return EncodeSequence(v), nil
default:
return nil, fmt.Errorf("type %T not allowed as key part", v)
}
}
// AddLengthPrefix prefixes the byte array with its length as 8 bytes. The function will panic
// if the bytes length is bigger than 255.
func AddLengthPrefix(bytes []byte) []byte {
byteLen := len(bytes)
if byteLen > MaxBytesLen {
panic(sdkerrors.Wrap(errors.ErrORMKeyMaxLength, "Cannot create key part with an []byte of length greater than 255 bytes. Try again with a smaller []byte."))
}
prefixedBytes := make([]byte, 1+len(bytes))
copy(prefixedBytes, []byte{uint8(byteLen)})
copy(prefixedBytes[1:], bytes)
return prefixedBytes
}
// NullTerminatedBytes converts string to byte array and null terminate it
func NullTerminatedBytes(s string) []byte {
bytes := make([]byte, len(s)+1)
copy(bytes, s)
return bytes
}
// stripRowID returns the RowID from the indexKey based on secondaryIndexKey type.
// It is the reverse operation to buildKeyFromParts for index keys
// where the first part is the encoded secondaryIndexKey and the second part is the RowID.
func stripRowID(indexKey []byte, secondaryIndexKey interface{}) (RowID, error) {
switch v := secondaryIndexKey.(type) {
case []byte:
searchableKeyLen := indexKey[0]
return indexKey[1+searchableKeyLen:], nil
case string:
searchableKeyLen := 0
for i, b := range indexKey {
if b == 0 {
searchableKeyLen = i
break
}
}
return indexKey[1+searchableKeyLen:], nil
case uint64:
return indexKey[EncodedSeqLength:], nil
default:
return nil, fmt.Errorf("type %T not allowed as index key", v)
}
}