## Description Closes: #10729 Includes: * table, auto-increment table, and singleton `Table` implementations * primary key, index and unique index `Index` implementations * store wrappers based on tm-db but that could be retargeted to the new ADR 040 db which separate index and commitment stores, with a debug wrapper * streaming JSON import and export * full logical decoding (and encoding) --- ### 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 - [x] reviewed "Files changed" and left comments if necessary - [ ] 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)
143 lines
3.7 KiB
Go
143 lines
3.7 KiB
Go
package ormkv
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
)
|
|
|
|
// PrimaryKeyCodec is the codec for primary keys.
|
|
type PrimaryKeyCodec struct {
|
|
*KeyCodec
|
|
unmarshalOptions proto.UnmarshalOptions
|
|
}
|
|
|
|
var _ IndexCodec = &PrimaryKeyCodec{}
|
|
|
|
// NewPrimaryKeyCodec creates a new PrimaryKeyCodec for the provided msg and
|
|
// fields, with an optional prefix and unmarshal options.
|
|
func NewPrimaryKeyCodec(prefix []byte, msgType protoreflect.MessageType, fieldNames []protoreflect.Name, unmarshalOptions proto.UnmarshalOptions) (*PrimaryKeyCodec, error) {
|
|
keyCodec, err := NewKeyCodec(prefix, msgType, fieldNames)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &PrimaryKeyCodec{
|
|
KeyCodec: keyCodec,
|
|
unmarshalOptions: unmarshalOptions,
|
|
}, nil
|
|
}
|
|
|
|
var _ IndexCodec = PrimaryKeyCodec{}
|
|
|
|
func (p PrimaryKeyCodec) DecodeIndexKey(k, _ []byte) (indexFields, primaryKey []protoreflect.Value, err error) {
|
|
indexFields, err = p.DecodeKey(bytes.NewReader(k))
|
|
|
|
// got prefix key
|
|
if err == io.EOF {
|
|
return indexFields, nil, nil
|
|
} else if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
if len(indexFields) == len(p.fieldCodecs) {
|
|
// for primary keys the index fields are the primary key
|
|
// but only if we don't have a prefix key
|
|
primaryKey = indexFields
|
|
}
|
|
return indexFields, primaryKey, nil
|
|
|
|
}
|
|
|
|
func (p PrimaryKeyCodec) DecodeEntry(k, v []byte) (Entry, error) {
|
|
values, err := p.DecodeKey(bytes.NewReader(k))
|
|
if err == io.EOF {
|
|
return &PrimaryKeyEntry{
|
|
TableName: p.messageType.Descriptor().FullName(),
|
|
Key: values,
|
|
}, nil
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
msg := p.messageType.New().Interface()
|
|
err = p.Unmarshal(values, v, msg)
|
|
|
|
return &PrimaryKeyEntry{
|
|
TableName: p.messageType.Descriptor().FullName(),
|
|
Key: values,
|
|
Value: msg,
|
|
}, err
|
|
}
|
|
|
|
func (p PrimaryKeyCodec) EncodeEntry(entry Entry) (k, v []byte, err error) {
|
|
pkEntry, ok := entry.(*PrimaryKeyEntry)
|
|
if !ok {
|
|
return nil, nil, ormerrors.BadDecodeEntry.Wrapf("expected %T, got %T", &PrimaryKeyEntry{}, entry)
|
|
}
|
|
|
|
if pkEntry.TableName != p.messageType.Descriptor().FullName() {
|
|
return nil, nil, ormerrors.BadDecodeEntry.Wrapf(
|
|
"wrong table name, got %s, expected %s",
|
|
pkEntry.TableName,
|
|
p.messageType.Descriptor().FullName(),
|
|
)
|
|
}
|
|
|
|
k, err = p.KeyCodec.EncodeKey(pkEntry.Key)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
v, err = p.marshal(pkEntry.Key, pkEntry.Value)
|
|
return k, v, err
|
|
}
|
|
|
|
func (p PrimaryKeyCodec) marshal(key []protoreflect.Value, message proto.Message) (v []byte, err error) {
|
|
// first clear the priamry key values because these are already stored in
|
|
// the key so we don't need to store them again in the value
|
|
p.ClearValues(message.ProtoReflect())
|
|
|
|
v, err = proto.MarshalOptions{Deterministic: true}.Marshal(message)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// set the primary key values again returning the message to its original state
|
|
p.SetKeyValues(message.ProtoReflect(), key)
|
|
|
|
return v, nil
|
|
}
|
|
|
|
func (p *PrimaryKeyCodec) ClearValues(message protoreflect.Message) {
|
|
for _, f := range p.fieldDescriptors {
|
|
message.Clear(f)
|
|
}
|
|
}
|
|
|
|
func (p *PrimaryKeyCodec) Unmarshal(key []protoreflect.Value, value []byte, message proto.Message) error {
|
|
err := p.unmarshalOptions.Unmarshal(value, message)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// rehydrate primary key
|
|
p.SetKeyValues(message.ProtoReflect(), key)
|
|
return nil
|
|
}
|
|
|
|
func (p PrimaryKeyCodec) EncodeKVFromMessage(message protoreflect.Message) (k, v []byte, err error) {
|
|
ks, k, err := p.KeyCodec.EncodeKeyFromMessage(message)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
v, err = p.marshal(ks, message.Interface())
|
|
return k, v, err
|
|
}
|