-
Notifications
You must be signed in to change notification settings - Fork 1
/
segment.go
133 lines (120 loc) · 3.24 KB
/
segment.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
package scache
import (
"encoding/binary"
"sync/atomic"
)
/*
Idea taken from
https://dev.to/douglasmakey/how-bigcache-avoids-expensive-gc-cycles-and-speeds-up-concurrent-access-in-go-12bb
*/
const (
headerSize = 5
controlByte = 0x9A
)
type segment struct {
*shardedMap
config *Config
index uint32
data []byte
dataSize uint64
tail uint64
keys uint32
mmap *mmap
}
func (s *segment) close() error {
if s.mmap != nil {
return s.mmap.close()
}
return nil
}
func (s *segment) reset() {
for i := range s.maps {
s.shardedMap.lock[i].Lock()
clearSwissMap(s.shardedMap.maps[i], keys, values)
s.shardedMap.lock[i].Unlock()
}
atomic.StoreUint64(&s.tail, 32)
atomic.StoreUint32(&s.keys, 0)
}
func (s *segment) get(key string) ([]byte, bool) {
shardedMap := s.getShardedMap()
headerAddress := shardedMap.getAddress(key)
if headerAddress == 0 {
return nil, false
}
headerAddressEnd := headerAddress + headerSize
if headerAddressEnd > s.dataSize {
return nil, false
}
entrySize := binary.LittleEndian.Uint32(s.data[headerAddress+1 : headerAddressEnd])
if headerAddressEnd > atomic.LoadUint64(&s.tail) {
return nil, false
}
dataAddress := headerAddress + headerSize
dataAddressEnd := dataAddress + uint64(entrySize)
if dataAddressEnd > s.dataSize {
return nil, false
}
result := s.data[dataAddress:dataAddressEnd]
if s.data[headerAddress] != controlByte {
return nil, false
}
return result, true
}
func (s *segment) delete(key string) {
shardedMap := s.getShardedMap()
if shardedMap.delete(key) {
updateKeys:
if keys := atomic.LoadUint32(&s.keys); keys > 1 {
if !atomic.CompareAndSwapUint32(&s.keys, keys, keys-1) {
goto updateKeys
}
}
}
}
func (s *segment) getShardedMap() *shardedMap {
result := s.shardedMap
return result
}
func (s *segment) set(key string, value []byte) ([]byte, bool) {
if maxEntries := s.config.MaxEntries; maxEntries > 0 && 1+int(atomic.LoadUint32(&s.keys)) > maxEntries {
return nil, false
}
shardedMap := s.getShardedMap()
blobSize := len(value) + headerSize
alignBlobSize := ((blobSize >> 5) + 1) << 5
nextAddress := int(atomic.AddUint64(&s.tail, uint64(alignBlobSize)))
if nextAddress >= len(s.data) { //out of memory,
atomic.SwapUint64(&s.tail, s.dataSize-1)
return nil, false
}
headerAddress := nextAddress - alignBlobSize
s.data[headerAddress] = controlByte
binary.LittleEndian.PutUint32(s.data[headerAddress+1:headerAddress+headerSize], uint32(len(value)))
entryAddress := headerAddress + headerSize
entryAddressOffset := entryAddress + len(value)
copy(s.data[entryAddress:entryAddressOffset], value)
if hadKey := shardedMap.put(key, uint32(headerAddress>>5)); !hadKey {
atomic.AddUint32(&s.keys, 1)
}
return s.data[entryAddress:entryAddressOffset], true
}
func (s *segment) allocate(idx int) error {
s.index = uint32(idx)
s.tail = 32
segmentDataSize := s.config.SegmentDataSize()
if s.config.Location == "" {
s.data = make([]byte, segmentDataSize)
s.dataSize = uint64(segmentDataSize)
return nil
}
s.mmap = newMmap(s.config.Location, s.config.SizeMb*mb)
err := s.mmap.open()
if err == nil {
s.mmap.size = segmentDataSize
offset := int64(idx * segmentDataSize)
err = s.mmap.assign(offset, &s.data)
s.dataSize = uint64(len(s.data))
}
return err
}