/
key_format.go
285 lines (259 loc) · 6.76 KB
/
key_format.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package keyformat
import (
"encoding"
"encoding/binary"
"fmt"
)
// CustomFormat specifies a custom encoding format for a key element.
type CustomFormat interface {
// Size returns the size of the encoded element.
Size() int
// MarshalBinary encodes the passed value into its binary representation.
MarshalBinary(v interface{}) ([]byte, error)
// UnmarshalBinary decodes the passed value from its binary representation.
UnmarshalBinary(v interface{}, data []byte) error
}
type elementMeta struct {
size int
custom CustomFormat
}
func (m *elementMeta) checkSize(index, size int) {
if m.size != size {
panic(fmt.Sprintf("key format: incompatible element %d (size: %d expected: %d)",
index,
size,
m.size,
))
}
}
// KeyFormat is a key formatting helper to be used together with key-value
// backends for constructing keys.
type KeyFormat struct {
// prefix is the one-byte key prefix that denotes the type of the key.
prefix byte
// meta is a list of key format element metadata.
meta []*elementMeta
// size is the total size of the key in bytes.
size int
}
// New constructs a new key format.
func New(prefix byte, layout ...interface{}) *KeyFormat {
kf := &KeyFormat{
prefix: prefix,
meta: make([]*elementMeta, len(layout)),
}
hasVarSize := false
for i, item := range layout {
meta := kf.getElementMeta(item)
if meta.size == -1 {
if hasVarSize {
panic("key format: there can be only one variable-sized element")
}
hasVarSize = true
} else {
kf.size += meta.size
}
kf.meta[i] = meta
}
return kf
}
// Size returns the minimum size in bytes of the resulting key.
func (k *KeyFormat) Size() int {
return 1 + k.size
}
// Prefix returns the key format's prefix byte.
func (k *KeyFormat) Prefix() byte {
return k.prefix
}
// Encode encodes values into a key.
//
// You can pass either the same amount of values as specified in the layout
// or less. In case less values are specified this will generate a shorter
// key containing only the specified values.
//
// In case no values are specified this will only output the key prefix.
func (k *KeyFormat) Encode(values ...interface{}) []byte {
if len(values) > len(k.meta) {
panic("key format: number of values greater than layout")
}
size := 1
for i := range values {
meta := k.meta[i]
elemLen := meta.size
if elemLen == -1 {
// Variable-sized element, the passed value must be a []byte.
elemLen = len(values[i].([]byte))
}
size += elemLen
}
result := make([]byte, size)
result[0] = k.prefix
offset := 1
for i, v := range values {
meta := k.meta[i]
elemLen := meta.size
if elemLen == -1 {
// Variable-sized element, the passed value must be a []byte (was checked above).
elemLen = len(v.([]byte))
}
buf := result[offset : offset+elemLen]
offset += elemLen
switch t := v.(type) {
case uint8:
meta.checkSize(i, 1)
buf[0] = t
case *uint8:
meta.checkSize(i, 1)
buf[0] = *t
case uint32:
// Use big endian encoding so the keys sort correctly when doing
// range queries.
meta.checkSize(i, 4)
binary.BigEndian.PutUint32(buf, t)
case *uint32:
meta.checkSize(i, 4)
binary.BigEndian.PutUint32(buf, *t)
case uint64:
meta.checkSize(i, 8)
binary.BigEndian.PutUint64(buf, t)
case *uint64:
meta.checkSize(i, 8)
binary.BigEndian.PutUint64(buf, *t)
case int64:
meta.checkSize(i, 8)
binary.BigEndian.PutUint64(buf, uint64(t))
case *int64:
meta.checkSize(i, 8)
binary.BigEndian.PutUint64(buf, uint64(*t))
case encoding.BinaryMarshaler:
var (
data []byte
err error
)
if meta.custom != nil {
data, err = meta.custom.MarshalBinary(t)
} else {
data, err = t.MarshalBinary()
}
if err != nil {
panic(fmt.Sprintf("key format: failed to marshal element %d: %s", i, err))
}
if len(data) != meta.size {
panic(fmt.Sprintf("key format: unexpected marshalled size %d for element %d", len(data), i))
}
copy(buf[:], data)
case []byte:
if meta.custom != nil {
var err error
t, err = meta.custom.MarshalBinary(t)
if err != nil {
panic(fmt.Sprintf("key format: failed to marshal element %d: %s", i, err))
}
}
copy(buf[:], t)
default:
panic(fmt.Sprintf("unsupported type: %T", t))
}
}
return result
}
// Decode decodes a key into its individual values.
//
// Returns false and doesn't modify the passed values if the key prefix
// doesn't match.
//
// *NOTE:* If decoding fails for one of the values, previous values
// will be modified.
func (k *KeyFormat) Decode(data []byte, values ...interface{}) bool {
if data[0] != k.prefix {
return false
}
if len(values) > len(k.meta) {
panic("key format: number of values greater than layout")
}
if len(data) < k.Size() {
panic("key format: malformed input")
}
offset := 1
for i, v := range values {
meta := k.meta[i]
elemLen := meta.size
if elemLen == -1 {
// Variable-sized element, compute its size.
elemLen = len(data) - k.Size()
}
buf := data[offset : offset+elemLen]
offset += elemLen
switch t := v.(type) {
case *uint8:
meta.checkSize(i, 1)
*t = buf[0]
case *uint32:
// Use big endian encoding so the keys sort correctly when doing
// range queries.
meta.checkSize(i, 4)
*t = binary.BigEndian.Uint32(buf)
case *uint64:
meta.checkSize(i, 8)
*t = binary.BigEndian.Uint64(buf)
case *int64:
meta.checkSize(i, 8)
*t = int64(binary.BigEndian.Uint64(buf))
case encoding.BinaryUnmarshaler:
var err error
if meta.custom != nil {
err = meta.custom.UnmarshalBinary(t, buf)
} else {
err = t.UnmarshalBinary(buf)
}
if err != nil {
return false
}
case *[]byte:
if meta.custom != nil {
if err := meta.custom.UnmarshalBinary(t, buf); err != nil {
return false
}
} else {
meta.checkSize(i, -1)
}
*t = make([]byte, elemLen)
copy(*t, buf)
default:
panic(fmt.Sprintf("unsupported type: %T", t))
}
}
return true
}
func (k *KeyFormat) getElementMeta(l interface{}) *elementMeta {
switch t := l.(type) {
case uint8:
return &elementMeta{size: 1}
case *uint8:
return &elementMeta{size: 1}
case uint32:
return &elementMeta{size: 4}
case *uint32:
return &elementMeta{size: 4}
case uint64:
return &elementMeta{size: 8}
case *uint64:
return &elementMeta{size: 8}
case int64:
return &elementMeta{size: 8}
case *int64:
return &elementMeta{size: 8}
case CustomFormat:
return &elementMeta{size: t.Size(), custom: t}
case encoding.BinaryMarshaler:
data, _ := t.MarshalBinary()
return &elementMeta{size: len(data)}
case []byte:
// A variable-size element -- there can be only one such element
// in the whole key and during decoding its size is derived from
// the key length and the sizes of other elements.
return &elementMeta{size: -1}
default:
panic(fmt.Sprintf("unsupported type: %T", l))
}
}