-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblock_cache.go
More file actions
236 lines (188 loc) · 4.44 KB
/
block_cache.go
File metadata and controls
236 lines (188 loc) · 4.44 KB
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
package bitcask
import (
"errors"
"sync"
"time"
)
const (
// fid use 5 bytes
BlockCacheFidBits = 40
BlockCacheFidMask = (1 << BlockCacheFidBits) - 1
BlockCacheFidShift = (64 - 40) // high 40-bits
// each data block is 32KB, and 22 bits can locate 128 GB
BlockCacheIdxBits = 22
BlockCacheIdxMask = (1 << BlockCacheIdxBits) - 1
BlockCacheIdxShift = 0 // low 22-bits
// the extra 2 bits reserved
)
var (
ErrBlockCacheMiss = errors.New("block cache miss")
ErrBlockCacheFidOutOfRange = errors.New("block cache fid out of range")
ErrBlockCacheIdxOutOfRange = errors.New("block cache idx out of range")
)
func BlockCacheKey(fid, blkIdx uint64) (uint64, error) {
if fid > BlockCacheFidMask {
return 0, ErrBlockCacheFidOutOfRange
}
if blkIdx > BlockCacheIdxMask {
return 0, ErrBlockCacheIdxOutOfRange
}
return (fid << BlockCacheFidShift) | blkIdx, nil
}
type BlockCacheOperator struct {
helper MapOperatorBase
}
func (optr *BlockCacheOperator) Hash(key *uint64) uint64 {
return *key
}
func (optr *BlockCacheOperator) Equals(lhs, rhs *uint64) bool {
return *lhs == *rhs
}
func (optr *BlockCacheOperator) Rand(upper uint64) uint64 {
return optr.helper.Rand(upper)
}
func (optr *BlockCacheOperator) WallTime() time.Time {
return optr.helper.WallTime()
}
type BlockCache struct {
maps *ShardMap[uint64, []byte]
blkPool *sync.Pool
}
type BlockCacheOptions struct {
Capacity uint64
Limited uint64
EvictionPoolCapacity uint64
SampleKeys uint64
Helper MapOperatorBase
}
func NewBlockCache(opts *BlockCacheOptions) (*BlockCache, error) {
blkPool := &sync.Pool{
New: func() any {
b := make([]byte, BlockSize)
return &b
},
}
if opts == nil || opts.Capacity == 0 {
return &BlockCache{
maps: nil,
blkPool: blkPool,
}, nil
}
mapOpts := &MapOptions{
Capacity: opts.Capacity,
Limited: opts.Limited,
EvictionPoolCapacity: opts.EvictionPoolCapacity,
SampleKeys: opts.SampleKeys,
}
optr := &BlockCacheOperator{
helper: opts.Helper,
}
maps, err := NewShardMap[uint64, []byte](optr, mapOpts)
if err != nil {
return nil, err
}
return &BlockCache{
maps: maps,
blkPool: blkPool,
}, nil
}
func (c *BlockCache) key(fid, blkIdx uint64) (uint64, error) {
if fid > BlockCacheFidMask {
return 0, ErrBlockCacheFidOutOfRange
}
if blkIdx > BlockCacheIdxMask {
return 0, ErrBlockCacheIdxOutOfRange
}
return (fid << BlockCacheFidShift) | blkIdx, nil
}
func (c *BlockCache) Get(fid, blkIdx uint64) ([]byte, error) {
if c.maps == nil {
return nil, ErrBlockCacheMiss
}
key, err := c.key(fid, blkIdx)
if err != nil {
return nil, err
}
if blkPtr, err := c.maps.Get(&key); err == nil {
return *blkPtr, nil
}
return nil, ErrBlockCacheMiss
}
func (c *BlockCache) BatchGet(fid, blkStartIdx, blkNum uint64) ([][]byte, error) {
if c.maps == nil {
return nil, ErrBlockCacheMiss
}
blks := make([][]byte, blkNum)
for i := uint64(0); i < blkNum; i++ {
key, err := c.key(fid, blkStartIdx+i)
if err != nil {
return nil, err
}
blkPtr, err := c.maps.Get(&key)
if err != nil {
return nil, err
}
blks[i] = *blkPtr
}
return blks, nil
}
func (c *BlockCache) Put(fid, blkIdx, length uint64, blk []byte) error {
if c.maps == nil {
c.blkPool.Put(&blk)
return nil
}
// the actual length is less than len(blk), and we should not put it into cache
if int(length) != len(blk) {
c.blkPool.Put(&blk)
return nil
}
key, err := c.key(fid, blkIdx)
if err != nil {
c.blkPool.Put(&blk)
return err
}
oldBlkPtr, err := c.maps.Set(&key, &blk)
if err != nil {
c.blkPool.Put(&blk)
return err
}
// maybe no eviction
if oldBlkPtr != nil {
c.blkPool.Put(oldBlkPtr)
}
return nil
}
func (c *BlockCache) BatchPut(fid, blkIdx, length uint64, blks [][]byte) error {
if c.maps == nil {
for idx := range blks {
c.blkPool.Put(&blks[idx])
}
return nil
}
// blks must include at least one elements
blkNum := uint64(len(blks))
if length != blkNum*uint64(len(blks[0])) {
blkNum--
c.blkPool.Put(&blks[len(blks)-1])
}
for i := uint64(0); i < blkNum; i++ {
key, err := c.key(fid, blkIdx+i)
if err != nil {
c.blkPool.Put(&blks[i])
continue
}
oldBlkPtr, err := c.maps.Set(&key, &blks[i])
if err != nil {
c.blkPool.Put(&blks[i])
continue
}
if oldBlkPtr != nil {
c.blkPool.Put(oldBlkPtr)
}
}
return nil
}
func (c *BlockCache) NewBlock() []byte {
blkPtr := c.blkPool.Get().(*[]byte)
return *blkPtr
}