-
Notifications
You must be signed in to change notification settings - Fork 289
/
Copy pathprefix_formatter.go
42 lines (33 loc) · 978 Bytes
/
prefix_formatter.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
package keyformat
import "encoding/binary"
// This file builds some dedicated key formatters for what appears in benchmarks.
// Prefixes a single byte before a 32 byte hash.
type FastPrefixFormatter struct {
prefix byte
length int
prefixSlice []byte
}
func NewFastPrefixFormatter(prefix byte, length int) *FastPrefixFormatter {
return &FastPrefixFormatter{prefix: prefix, length: length, prefixSlice: []byte{prefix}}
}
func (f *FastPrefixFormatter) Key(bz []byte) []byte {
key := make([]byte, 1+f.length)
key[0] = f.prefix
copy(key[1:], bz)
return key
}
func (f *FastPrefixFormatter) Scan(key []byte, a interface{}) {
scan(a, key[1:])
}
func (f *FastPrefixFormatter) KeyInt64(bz int64) []byte {
key := make([]byte, 1+f.length)
key[0] = f.prefix
binary.BigEndian.PutUint64(key[1:], uint64(bz))
return key
}
func (f *FastPrefixFormatter) Prefix() []byte {
return f.prefixSlice
}
func (f *FastPrefixFormatter) Length() int {
return 1 + f.length
}