-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslot.go
160 lines (142 loc) · 3.35 KB
/
slot.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
package rabbitkv
import (
"bytes"
"errors"
"encoding/binary"
"github.com/mmcloughlin/meow"
)
type Pair struct {
key []byte
value []byte
}
type Slot struct {
pairs []Pair
}
func NewSlot(key, value []byte) Slot {
return Slot {
pairs: []Pair {
{key: key, value: value},
},
}
}
func (s *Slot) Empty() bool {
return len(s.pairs) == 0
}
func (s *Slot) Add(pair Pair) {
for i := range s.pairs {
if bytes.Equal(s.pairs[i].key, pair.key) {
// overwrite an existing value
s.pairs[i].value = pair.value
return
}
}
// append to the tail
s.pairs = append(s.pairs, pair)
}
func (s *Slot) Get(key []byte) []byte {
for _, pair := range s.pairs {
if bytes.Equal(pair.key, key) {
return pair.value
}
}
return nil
}
func (s *Slot) Remove(key []byte) (existed bool) {
idx := -1
for i, pair := range s.pairs {
if bytes.Equal(pair.key, key) {
idx = i
break
}
}
if idx == -1 {
return false
}
copy(s.pairs[idx:], s.pairs[idx+1:])
s.pairs = s.pairs[:len(s.pairs)-1]
return true
}
//total-length, pair-count, lengths-of-kv, payload-of-kv, cksum, padding
func (s *Slot) ToSlicesForDump() ([][]byte, int) {
hasher := meow.New32(0)
head := make([]byte, 4, (len(s.pairs)*2+2)*4)
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], uint32(len(s.pairs)))
hasher.Write(buf[:])
head = append(head, buf[:]...)
for _, pair := range s.pairs {
for _, bz := range [][]byte{pair.key, pair.value} {
binary.LittleEndian.PutUint32(buf[:], uint32(len(bz)))
hasher.Write(buf[:])
head = append(head, buf[:]...)
}
}
totalLen := len(head)-4 //exclude the leading 4 bytes
res := make([][]byte, 0, len(s.pairs)*2+3)
res = append(res, head)
for _, pair := range s.pairs {
for _, bz := range [][]byte{pair.key, pair.value} {
hasher.Write(bz)
res = append(res, bz)
totalLen += len(bz)
}
}
binary.LittleEndian.PutUint32(buf[:], hasher.Sum32())
res = append(res, buf[:])
binary.LittleEndian.PutUint32(head[:4], uint32(totalLen))
rem := (totalLen+4)%16 //include the leading 4 bytes
if rem == 0 {
res = append(res, []byte{}) //no padding
} else {
res = append(res, make([]byte, 16 - rem)) //padding
totalLen += 16 - rem
}
return res, totalLen
}
func extractBytes(in []byte, length int) (result, out []byte, err error) {
if len(in) < length {
return nil, nil, errors.New("Not enough bytes to read")
}
return in[:length], in[length:], nil
}
func extractUint32(in []byte) (result uint32, out []byte, err error) {
if len(in) < 4 {
return 0, nil, errors.New("Not enough bytes to read")
}
return binary.LittleEndian.Uint32(in[:4]), in[4:], nil
}
func BytesToSlot(bzIn []byte) (slot Slot, err error) {
pairCount, bz, err := extractUint32(bzIn)
if err != nil {
return
}
lenList := make([]uint32, 2*int(pairCount))
for i := range lenList {
lenList[i], bz, err = extractUint32(bz)
if err != nil {
return
}
}
slot.pairs = make([]Pair, int(pairCount))
for i := range slot.pairs {
slot.pairs[i].key, bz, err = extractBytes(bz, int(lenList[2*i]))
if err != nil {
return
}
slot.pairs[i].value, bz, err = extractBytes(bz, int(lenList[2*i+1]))
if err != nil {
return
}
}
cksum, bz, err := extractUint32(bz)
if err != nil {
return
}
hasher := meow.New32(0)
hasher.Write(bzIn[:len(bzIn)-len(bz)/*padding*/-4/*cksum*/])
if hasher.Sum32() != cksum {
err = errors.New("Checksum Error")
return
}
return
}