forked from go-bond/bond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
347 lines (275 loc) · 7.42 KB
/
keys.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package bond
import (
"bytes"
"encoding/binary"
"math/big"
"github.com/cockroachdb/pebble"
)
type KeyBuilder struct {
buff []byte
fid byte
}
func NewKeyBuilder(buff []byte) KeyBuilder {
return KeyBuilder{buff: buff}
}
func (b KeyBuilder) AddInt64Field(i int64) KeyBuilder {
bt := b.putFieldID()
if i > 0 {
bt.buff = append(bt.buff, 0x02)
} else if i == 0 {
bt.buff = append(bt.buff, 0x01)
} else {
bt.buff = append(bt.buff, 0x00)
i = ^-i
}
bt.buff = append(bt.buff, []byte{0, 0, 0, 0, 0, 0, 0, 0}...)
binary.BigEndian.PutUint64(bt.buff[len(bt.buff)-8:], uint64(i))
return bt
}
func (b KeyBuilder) AddInt32Field(i int32) KeyBuilder {
bt := b.putFieldID()
if i > 0 {
bt.buff = append(bt.buff, 0x02)
} else if i == 0 {
bt.buff = append(bt.buff, 0x01)
} else {
bt.buff = append(bt.buff, 0x00)
i = ^-i
}
bt.buff = append(bt.buff, []byte{0, 0, 0, 0}...)
binary.BigEndian.PutUint32(bt.buff[len(bt.buff)-4:], uint32(i))
return bt
}
func (b KeyBuilder) AddInt16Field(i int16) KeyBuilder {
bt := b.putFieldID()
if i > 0 {
bt.buff = append(bt.buff, 0x02)
} else if i == 0 {
bt.buff = append(bt.buff, 0x01)
} else {
bt.buff = append(bt.buff, 0x00)
i = ^-i
}
bt.buff = append(bt.buff, []byte{0, 0}...)
binary.BigEndian.PutUint16(bt.buff[len(bt.buff)-2:], uint16(i))
return bt
}
func (b KeyBuilder) AddUint64Field(i uint64) KeyBuilder {
bt := b.putFieldID()
bt.buff = append(bt.buff, []byte{0, 0, 0, 0, 0, 0, 0, 0}...)
binary.BigEndian.PutUint64(bt.buff[len(bt.buff)-8:], i)
return bt
}
func (b KeyBuilder) AddUint32Field(i uint32) KeyBuilder {
bt := b.putFieldID()
bt.buff = append(bt.buff, []byte{0, 0, 0, 0}...)
binary.BigEndian.PutUint32(bt.buff[len(bt.buff)-4:], i)
return bt
}
func (b KeyBuilder) AddUint16Field(i uint16) KeyBuilder {
bt := b.putFieldID()
bt.buff = append(bt.buff, []byte{0, 0}...)
binary.BigEndian.PutUint16(bt.buff[len(bt.buff)-2:], i)
return bt
}
func (b KeyBuilder) AddByteField(btt byte) KeyBuilder {
bt := b.putFieldID()
bt.buff = append(bt.buff, btt)
return bt
}
func (b KeyBuilder) AddStringField(s string) KeyBuilder {
bt := b.putFieldID()
bt.buff = append(bt.buff, []byte(s)...)
return bt
}
func (b KeyBuilder) AddBytesField(bs []byte) KeyBuilder {
bt := b.putFieldID()
bt.buff = append(bt.buff, bs...)
return bt
}
func (b KeyBuilder) AddBigIntField(bi *big.Int, bits int) KeyBuilder {
bt := b.putFieldID()
sign := bi.Sign() + 1
bt.buff = append(bt.buff, byte(sign)) // 0 - negative, 1 - zero, 2 - positive
if sign == 0 {
bi = big.NewInt(0).Sub(big.NewInt(0), bi)
}
bytesLen := bits / 8
for i := 0; i < bytesLen; i++ {
bt.buff = append(bt.buff, 0x00)
}
bi.FillBytes(bt.buff[len(bt.buff)-bytesLen:])
if sign == 0 {
index := len(bt.buff)
for i := 0; i < bytesLen; i++ {
bt.buff[index-i-1] = 0xFF - bt.buff[index-i-1]
}
}
return bt
}
func (b KeyBuilder) putFieldID() KeyBuilder {
return KeyBuilder{
buff: append(b.buff, b.fid+1),
fid: b.fid + 1,
}
}
func (b KeyBuilder) Bytes() []byte {
return b.buff
}
type Key struct {
TableID TableID
IndexID IndexID
Index []byte
IndexOrder []byte
PrimaryKey []byte
}
func NewUserKey(key string) KeyBytes {
return KeyEncode(Key{
TableID: BOND_DB_DATA_TABLE_ID,
IndexID: BOND_DB_DATA_USER_SPACE_INDEX_ID,
Index: []byte{},
IndexOrder: []byte{},
PrimaryKey: []byte(key),
})
}
func (k Key) ToDataKey() Key {
return Key{
TableID: k.TableID,
IndexID: PrimaryIndexID,
Index: []byte{},
IndexOrder: []byte{},
PrimaryKey: k.PrimaryKey,
}
}
func (k Key) ToKeyPrefix() Key {
return Key{
TableID: k.TableID,
IndexID: k.IndexID,
Index: k.Index,
IndexOrder: []byte{},
PrimaryKey: []byte{},
}
}
func (k Key) IsDataKey() bool {
return k.IndexID == PrimaryIndexID && len(k.Index) == 0
}
func (k Key) IsIndexKey() bool {
return k.IndexID != PrimaryIndexID && len(k.Index) != 0
}
func (k Key) IsKeyPrefix() bool {
return len(k.PrimaryKey) == 0
}
func KeyEncode(key Key, rawBuffs ...[]byte) []byte {
var rawBuff []byte
if len(rawBuffs) > 0 && rawBuffs[0] != nil {
rawBuff = rawBuffs[0]
}
buff := bytes.NewBuffer(rawBuff)
buff.Write([]byte{byte(key.TableID)})
buff.Write([]byte{byte(key.IndexID)})
var indexLenBuff [4]byte
binary.BigEndian.PutUint32(indexLenBuff[:4], uint32(len(key.Index)))
buff.Write(indexLenBuff[:4])
buff.Write(key.Index)
if !key.IsKeyPrefix() {
binary.BigEndian.PutUint32(indexLenBuff[:4], uint32(len(key.IndexOrder)))
buff.Write(indexLenBuff[:4])
buff.Write(key.IndexOrder)
buff.Write(key.PrimaryKey)
}
return buff.Bytes()
}
func KeyEncodeRaw(tableID TableID, indexID IndexID, indexFunc, indexOrderFunc, primaryKeyFunc func(buff []byte) []byte, buffs ...[]byte) []byte {
var buff []byte
if len(buffs) > 0 && buffs[0] != nil {
buff = buffs[0]
}
buff = append(buff, byte(tableID))
buff = append(buff, byte(indexID))
// placeholder for len
buff = append(buff, []byte{0x00, 0x00, 0x00, 0x00}...)
lenIndex := len(buff) - 4
if indexFunc != nil {
buff = indexFunc(buff)
binary.BigEndian.PutUint32(buff[lenIndex:lenIndex+4], uint32(len(buff)-(lenIndex+4)))
}
if primaryKeyFunc != nil {
// placeholder for len
buff = append(buff, []byte{0x00, 0x00, 0x00, 0x00}...)
lenIndex = len(buff) - 4
if indexOrderFunc != nil {
buff = indexOrderFunc(buff)
binary.BigEndian.PutUint32(buff[lenIndex:lenIndex+4], uint32(len(buff)-(lenIndex+4)))
}
buff = primaryKeyFunc(buff)
}
return buff
}
func KeyDecode(keyBytes []byte) Key {
buff := bytes.NewBuffer(keyBytes)
tableID, _ := buff.ReadByte()
indexID, _ := buff.ReadByte()
indexLenBuff := make([]byte, 4)
_, _ = buff.Read(indexLenBuff)
indexLen := binary.BigEndian.Uint32(indexLenBuff)
index := make([]byte, indexLen)
_, _ = buff.Read(index)
indexOrderLenBuff := make([]byte, 4)
_, _ = buff.Read(indexOrderLenBuff)
indexOrderLen := binary.BigEndian.Uint32(indexOrderLenBuff)
indexOrder := make([]byte, indexOrderLen)
_, _ = buff.Read(indexOrder)
primaryKeyLen := len(keyBytes) - int(indexLen) - int(indexOrderLen) - 10
primaryKey := make([]byte, primaryKeyLen)
_, _ = buff.Read(primaryKey)
return Key{
TableID: TableID(tableID),
IndexID: IndexID(indexID),
Index: index,
IndexOrder: indexOrder,
PrimaryKey: primaryKey,
}
}
type KeyBytes []byte
func (key KeyBytes) ToDataKeyBytes(rawBuffs ...[]byte) KeyBytes {
var rawBuff []byte
if len(rawBuffs) > 0 && rawBuffs[0] != nil {
rawBuff = rawBuffs[0]
}
buff := bytes.NewBuffer(rawBuff)
buff.WriteByte(key[0])
buff.WriteByte(0)
buff.Write([]byte{0, 0, 0, 0})
buff.Write([]byte{0, 0, 0, 0})
indexLen := int(binary.BigEndian.Uint32(key[2:6]))
indexOrderLen := int(binary.BigEndian.Uint32(key[6+indexLen : 10+indexLen]))
buff.Write(key[10+indexLen+indexOrderLen:])
return buff.Bytes()
}
func (key KeyBytes) TableID() TableID {
return TableID(key[0])
}
func (key KeyBytes) IndexID() IndexID {
return IndexID(key[1])
}
func (key KeyBytes) Index() []byte {
keyLen := binary.BigEndian.Uint32(key[2:6])
return key[6 : 6+keyLen]
}
func (key KeyBytes) IsDataKey() bool {
return key.IndexID() == PrimaryIndexID
}
func (key KeyBytes) IsIndexKey() bool {
return key.IndexID() != PrimaryIndexID
}
func (key KeyBytes) ToKey() Key {
return KeyDecode(key)
}
func DefaultKeyComparer() *pebble.Comparer {
comparer := *pebble.DefaultComparer
comparer.Split = _KeyPrefixSplitIndex
return &comparer
}
func _KeyPrefixSplitIndex(rawKey []byte) int {
return 6 + int(binary.BigEndian.Uint32(rawKey[2:6]))
}