-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
file.go
122 lines (98 loc) · 3.13 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ormdb
import (
"bytes"
"encoding/binary"
"math"
"google.golang.org/protobuf/reflect/protoregistry"
"github.com/cosmos/cosmos-sdk/orm/encoding/encodeutil"
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/cosmos/cosmos-sdk/orm/model/ormtable"
)
type fileDescriptorDBOptions struct {
Prefix []byte
ID uint32
TypeResolver ormtable.TypeResolver
JSONValidator func(proto.Message) error
BackendResolver ormtable.BackendResolver
}
type fileDescriptorDB struct {
id uint32
prefix []byte
tablesById map[uint32]ormtable.Table
tablesByName map[protoreflect.FullName]ormtable.Table
fileDescriptor protoreflect.FileDescriptor
}
func newFileDescriptorDB(fileDescriptor protoreflect.FileDescriptor, options fileDescriptorDBOptions) (*fileDescriptorDB, error) {
prefix := encodeutil.AppendVarUInt32(options.Prefix, options.ID)
schema := &fileDescriptorDB{
id: options.ID,
prefix: prefix,
tablesById: map[uint32]ormtable.Table{},
tablesByName: map[protoreflect.FullName]ormtable.Table{},
fileDescriptor: fileDescriptor,
}
resolver := options.TypeResolver
if resolver == nil {
resolver = protoregistry.GlobalTypes
}
messages := fileDescriptor.Messages()
n := messages.Len()
for i := 0; i < n; i++ {
messageDescriptor := messages.Get(i)
tableName := messageDescriptor.FullName()
messageType, err := resolver.FindMessageByName(tableName)
if err != nil {
return nil, err
}
table, err := ormtable.Build(ormtable.Options{
Prefix: prefix,
MessageType: messageType,
TypeResolver: resolver,
JSONValidator: options.JSONValidator,
BackendResolver: options.BackendResolver,
})
if err != nil {
return nil, err
}
id := table.ID()
if _, ok := schema.tablesById[id]; ok {
return nil, ormerrors.InvalidTableId.Wrapf("duplicate ID %d for %s", id, tableName)
}
schema.tablesById[id] = table
if _, ok := schema.tablesByName[tableName]; ok {
return nil, ormerrors.InvalidTableDefinition.Wrapf("duplicate table %s", tableName)
}
schema.tablesByName[tableName] = table
}
return schema, nil
}
func (f fileDescriptorDB) DecodeEntry(k, v []byte) (ormkv.Entry, error) {
r := bytes.NewReader(k)
err := encodeutil.SkipPrefix(r, f.prefix)
if err != nil {
return nil, err
}
id, err := binary.ReadUvarint(r)
if err != nil {
return nil, err
}
if id > math.MaxUint32 {
return nil, ormerrors.UnexpectedDecodePrefix.Wrapf("uint32 varint id out of range %d", id)
}
table, ok := f.tablesById[uint32(id)]
if !ok {
return nil, ormerrors.UnexpectedDecodePrefix.Wrapf("can't find table with id %d", id)
}
return table.DecodeEntry(k, v)
}
func (f fileDescriptorDB) EncodeEntry(entry ormkv.Entry) (k, v []byte, err error) {
table, ok := f.tablesByName[entry.GetTableName()]
if !ok {
return nil, nil, ormerrors.BadDecodeEntry.Wrapf("can't find table %s", entry.GetTableName())
}
return table.EncodeEntry(entry)
}
var _ ormkv.EntryCodec = fileDescriptorDB{}