-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard.go
317 lines (271 loc) · 7.47 KB
/
shard.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
package fastcache
import (
"errors"
"io"
"math"
"unsafe"
)
var (
sizeOfShardArray = unsafe.Sizeof(shards{})
sizeOfShard = unsafe.Sizeof(shard{})
)
type shards struct {
len uint32
arrOffset uint64
}
func (s *shards) init(all *allocator, shardLen uint32, maxLen uint64) error {
var err error
size := uint64(shardLen) * uint64(sizeOfShard)
if _, s.arrOffset, err = all.alloc(size); err != nil {
return err
}
preMaxLen := uint64(math.Ceil(float64(maxLen) / float64(shardLen)))
for i := 0; i < int(shardLen); i++ {
shr := s.shard(all, i)
if err = shr.init(all, preMaxLen); err != nil {
return err
}
}
s.len = shardLen
return nil
}
func (s *shards) shard(all *allocator, index int) *shard {
offset := uint64(index)*uint64(sizeOfShard) + s.arrOffset
shr := (*shard)(unsafe.Pointer(all.base() + uintptr(offset)))
return shr
}
func (s *shards) Len() uint32 {
return s.len
}
type shard struct {
hashmapOffset uint64
lruStoreOffset uint64
freeStoreOffset uint64
lockerOffset uint64
maxLen uint64 // 当前shard, 最大容纳数量, 超过触发LRU
}
func (s *shard) init(all *allocator, maxLen uint64) error {
var err error
if _, s.hashmapOffset, err = all.alloc(uint64(sizeOfHashmap)); err != nil {
return err
}
hm := s.hashmap(all)
bucketLen := nextPrime(int(math.Ceil(float64(maxLen) / 0.75)))
if err = hm.init(all, uint32(bucketLen)); err != nil {
return err
}
if _, s.lruStoreOffset, err = all.alloc(uint64(sizeOfLRUStore)); err != nil {
return err
}
ls := s.lruStore(all)
ls.init(all)
if _, s.freeStoreOffset, err = all.alloc(uint64(sizeOfFreeStore)); err != nil {
return err
}
fs := s.freeStore(all)
if err = fs.init(all); err != nil {
return err
}
if _, s.lockerOffset, err = all.alloc(uint64(sizeOfProcessLocker)); err != nil {
return err
}
s.maxLen = maxLen
return nil
}
func (s *shard) hashmap(all *allocator) *hashmap {
return (*hashmap)(unsafe.Pointer(all.base() + uintptr(s.hashmapOffset)))
}
func (s *shard) lruStore(all *allocator) *lruStore {
return (*lruStore)(unsafe.Pointer(all.base() + uintptr(s.lruStoreOffset)))
}
func (s *shard) freeStore(all *allocator) *freeStore {
return (*freeStore)(unsafe.Pointer(all.base() + uintptr(s.freeStoreOffset)))
}
func (s *shard) locker(all *allocator) Locker {
return (*processLocker)(unsafe.Pointer(all.base() + uintptr(s.lockerOffset)))
}
func (s *shard) Has(all *allocator, hash uint64, key []byte) (uint8, bool) {
locker := s.locker(all)
locker.Lock()
defer locker.Unlock()
hm := s.hashmap(all)
_, node := hm.find(all, hash, key)
if node == nil {
return 0, false
}
return node.count, true
}
func (s *shard) Get(all *allocator, hash uint64, key []byte) ([]byte, uint8, error) {
locker := s.locker(all)
locker.Lock()
defer locker.Unlock()
hm := s.hashmap(all)
_, node := hm.find(all, hash, key)
if node == nil {
return nil, 0, ErrNotFound
}
node.count++
el := nodeTo[hashmapBucketElement](node)
value := el.value()
ls := s.lruStore(all)
ls.moveToFront(all, node.freeIndex, el.lruNode())
return value, node.count, nil
}
func (s *shard) GetWithBuffer(all *allocator, hash uint64, key []byte, buffer io.Writer) (uint8, error) {
locker := s.locker(all)
locker.Lock()
defer locker.Unlock()
hm := s.hashmap(all)
_, node := hm.find(all, hash, key)
if node == nil {
return 0, ErrNotFound
}
node.count++
el := nodeTo[hashmapBucketElement](node)
if err := el.valueWithBuffer(buffer); err != nil {
return 0, err
}
ls := s.lruStore(all)
ls.moveToFront(all, node.freeIndex, el.lruNode())
return node.count, nil
}
func (s *shard) Peek(all *allocator, hash uint64, key []byte) ([]byte, error) {
locker := s.locker(all)
locker.Lock()
defer locker.Unlock()
hm := s.hashmap(all)
_, node := hm.find(all, hash, key)
if node == nil {
return nil, ErrNotFound
}
node.count++
el := nodeTo[hashmapBucketElement](node)
value := el.value()
return value, nil
}
func (s *shard) PeekWithBuffer(all *allocator, hash uint64, key []byte, buffer io.Writer) error {
locker := s.locker(all)
locker.Lock()
defer locker.Unlock()
hm := s.hashmap(all)
_, node := hm.find(all, hash, key)
if node == nil {
return ErrNotFound
}
node.count++
el := nodeTo[hashmapBucketElement](node)
return el.valueWithBuffer(buffer)
}
func (s *shard) Set(all *allocator, hash uint64, key []byte, value []byte) error {
locker := s.locker(all)
locker.Lock()
defer locker.Unlock()
var err error
ls := s.lruStore(all)
hm := s.hashmap(all)
prev, node := hm.find(all, hash, key)
if node == nil {
node, err = s.newElement(all, hash, key, value)
if err != nil {
return err
}
hm.add(all, hash, node)
el := nodeTo[hashmapBucketElement](node)
ls.pushToFront(all, node.freeIndex, el.lruNode())
} else {
elSize := hashmapElementSize(key, value)
index := sizeToIndex(elSize)
if index > node.freeIndex {
// Delete old node and new one to replace
old := node
if node, err = s.newElement(all, hash, key, value); err != nil {
return err
}
if err = s.del(all, hash, prev, old); err != nil {
return err
}
hm.add(all, hash, node)
el := nodeTo[hashmapBucketElement](node)
ls.pushToFront(all, node.freeIndex, el.lruNode())
} else {
el := nodeTo[hashmapBucketElement](node)
el.updateValue(value)
ls.moveToFront(all, node.freeIndex, el.lruNode())
}
}
return nil
}
func (s *shard) Delete(all *allocator, hash uint64, key []byte) error {
locker := s.locker(all)
locker.Lock()
defer locker.Unlock()
hm := s.hashmap(all)
prev, node := hm.find(all, hash, key)
return s.del(all, hash, prev, node)
}
func (s *shard) del(all *allocator, hash uint64, prev *dataNode, node *dataNode) error {
hm := s.hashmap(all)
if err := hm.delete(all, hash, prev, node); err != nil {
return err
}
ls := s.lruStore(all)
el := nodeTo[hashmapBucketElement](node)
ls.remove(all, node.freeIndex, el.lruNode())
fs := s.freeStore(all)
fs.free(all, node)
return nil
}
func (s *shard) newElement(all *allocator, hash uint64, key []byte, value []byte) (node *dataNode, err error) {
fs := s.freeStore(all)
elSize := hashmapElementSize(key, value)
hm := s.hashmap(all)
if hm.len >= s.maxLen {
// 超过长度限制需要淘汰
if err = s.evict(all, elSize); err != nil {
// 有可能第一次在, 这个byte长度范围内进行分配, 就已经整体shard的长度超过了上限, 所以lru list有可能为空
// 直接去free list里面看下有没有可以用的free node
if !errors.Is(err, ErrLRUListIsEmpty) {
return
}
err = nil
}
}
node, err = fs.get(all, elSize)
if err != nil {
if !errors.Is(err, ErrNoSpace) {
return
}
// 空间不足, 就进行淘汰
if err = s.evict(all, elSize); err != nil {
return
}
// 淘汰过后, 一定会有可用空间
node, err = fs.get(all, elSize)
// 如果这个时候还是没有办法得到node, 就说明有错误
if err != nil {
return
}
}
el := nodeTo[hashmapBucketElement](node)
el.reset()
el.hash = hash
el.updateKey(key)
el.updateValue(value)
return node, nil
}
func (s *shard) evict(all *allocator, elSize uint32) error {
hm := s.hashmap(all)
ls := s.lruStore(all)
index := sizeToIndex(elSize)
lruList := ls.get(index)
if lruList.len == 0 {
return ErrLRUListIsEmpty
}
oldest := lruList.Back(all.base())
elPtr := uintptr(unsafe.Pointer(oldest)) - sizeOfHashmapBucketElement
el := (*hashmapBucketElement)(unsafe.Pointer(elPtr))
evictKey := el.key()
hash := xxHashBytes(evictKey)
evictPrev, evictNode := hm.find(all, hash, evictKey)
return s.del(all, hash, evictPrev, evictNode)
}