-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore_pebble.go
More file actions
300 lines (261 loc) · 6.42 KB
/
store_pebble.go
File metadata and controls
300 lines (261 loc) · 6.42 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
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
package cachegrid
import (
"encoding/binary"
"sync"
"sync/atomic"
"time"
"github.com/cockroachdb/pebble"
icache "github.com/skshohagmiah/cachegrid/internal/cache"
)
// PebbleStore is a disk-based Store backed by CockroachDB's Pebble engine.
type PebbleStore struct {
db *pebble.DB
mu sync.Mutex // protects Incr read-modify-write
count atomic.Int64
onEvict func(key string, value []byte)
onExpire func(key string, value []byte)
cbMu sync.RWMutex // protects callback pointers
}
// NewPebbleStore opens or creates a Pebble database at the given path.
func NewPebbleStore(path string) (*PebbleStore, error) {
db, err := pebble.Open(path, &pebble.Options{})
if err != nil {
return nil, err
}
ps := &PebbleStore{db: db}
// Count existing keys
ps.count.Store(ps.countKeys())
return ps, nil
}
// encode packs a value and its expiration into a single byte slice.
// Format: [8 bytes expiresAt unix nano, big-endian][value bytes]
// expiresAt of 0 means no expiration.
func (p *PebbleStore) encode(value []byte, ttl time.Duration) []byte {
var expiresAtNano int64
if ttl > 0 {
expiresAtNano = time.Now().Add(ttl).UnixNano()
}
buf := make([]byte, 8+len(value))
binary.BigEndian.PutUint64(buf[:8], uint64(expiresAtNano))
copy(buf[8:], value)
return buf
}
// decode extracts the value and expiration from a raw byte slice.
func (p *PebbleStore) decode(raw []byte) (value []byte, expiresAtNano int64) {
if len(raw) < 8 {
return nil, 0
}
expiresAtNano = int64(binary.BigEndian.Uint64(raw[:8]))
value = make([]byte, len(raw)-8)
copy(value, raw[8:])
return
}
func (p *PebbleStore) isExpired(expiresAtNano int64) bool {
return expiresAtNano != 0 && time.Now().UnixNano() > expiresAtNano
}
func (p *PebbleStore) Get(key string) ([]byte, bool) {
raw, closer, err := p.db.Get([]byte(key))
if err != nil {
return nil, false
}
// Copy raw before closing — Pebble's slice is only valid until Close.
rawCopy := make([]byte, len(raw))
copy(rawCopy, raw)
closer.Close()
value, expiresAt := p.decode(rawCopy)
if p.isExpired(expiresAt) {
p.db.Delete([]byte(key), pebble.NoSync)
p.count.Add(-1)
return nil, false
}
return value, true
}
func (p *PebbleStore) Set(key string, value []byte, ttl time.Duration) {
// Check if key already exists to keep count accurate
_, closer, err := p.db.Get([]byte(key))
exists := err == nil
if exists {
closer.Close()
}
encoded := p.encode(value, ttl)
if err := p.db.Set([]byte(key), encoded, pebble.NoSync); err != nil {
return
}
if !exists {
p.count.Add(1)
}
}
func (p *PebbleStore) Delete(key string) bool {
_, closer, err := p.db.Get([]byte(key))
if err != nil {
return false
}
closer.Close()
if err := p.db.Delete([]byte(key), pebble.NoSync); err != nil {
return false
}
p.count.Add(-1)
return true
}
func (p *PebbleStore) Exists(key string) bool {
raw, closer, err := p.db.Get([]byte(key))
if err != nil {
return false
}
rawCopy := make([]byte, len(raw))
copy(rawCopy, raw)
closer.Close()
_, expiresAt := p.decode(rawCopy)
if p.isExpired(expiresAt) {
p.db.Delete([]byte(key), pebble.NoSync)
p.count.Add(-1)
return false
}
return true
}
func (p *PebbleStore) TTL(key string) time.Duration {
raw, closer, err := p.db.Get([]byte(key))
if err != nil {
return 0
}
rawCopy := make([]byte, len(raw))
copy(rawCopy, raw)
closer.Close()
_, expiresAtNano := p.decode(rawCopy)
if expiresAtNano == 0 {
return -1
}
remaining := time.Until(time.Unix(0, expiresAtNano))
if remaining <= 0 {
p.db.Delete([]byte(key), pebble.NoSync)
p.count.Add(-1)
return 0
}
return remaining
}
func (p *PebbleStore) Incr(key string, delta int64, defaultTTL time.Duration) (int64, error) {
p.mu.Lock()
defer p.mu.Unlock()
var current int64
var ttl time.Duration
raw, closer, err := p.db.Get([]byte(key))
if err == nil {
rawCopy := make([]byte, len(raw))
copy(rawCopy, raw)
closer.Close()
value, expiresAtNano := p.decode(rawCopy)
if !p.isExpired(expiresAtNano) {
if err := icache.Deserialize(value, ¤t); err != nil {
return 0, err
}
// Preserve original TTL
if expiresAtNano != 0 {
remaining := time.Until(time.Unix(0, expiresAtNano))
if remaining > 0 {
ttl = remaining
}
}
} else {
// Expired — treat as new key
ttl = defaultTTL
p.count.Add(-1) // will be re-added by Set logic below
}
} else {
// New key
ttl = defaultTTL
}
current += delta
newValue, err := icache.Serialize(current)
if err != nil {
return 0, err
}
encoded := p.encode(newValue, ttl)
_, closer2, getErr := p.db.Get([]byte(key))
existed := getErr == nil
if existed {
closer2.Close()
}
if err := p.db.Set([]byte(key), encoded, pebble.NoSync); err != nil {
return 0, err
}
if !existed {
p.count.Add(1)
}
return current, nil
}
func (p *PebbleStore) DeleteExpired() int {
now := time.Now().UnixNano()
type expiredEntry struct {
key string
value []byte
}
var expired []expiredEntry
iter, err := p.db.NewIter(nil)
if err != nil {
return 0
}
defer iter.Close()
for iter.First(); iter.Valid(); iter.Next() {
raw := iter.Value()
if len(raw) < 8 {
continue
}
expiresAtNano := int64(binary.BigEndian.Uint64(raw[:8]))
if expiresAtNano != 0 && now > expiresAtNano {
key := make([]byte, len(iter.Key()))
copy(key, iter.Key())
value := make([]byte, len(raw)-8)
copy(value, raw[8:])
expired = append(expired, expiredEntry{key: string(key), value: value})
}
}
if len(expired) == 0 {
return 0
}
batch := p.db.NewBatch()
for _, e := range expired {
batch.Delete([]byte(e.key), nil)
}
batch.Commit(pebble.NoSync)
batch.Close()
p.count.Add(int64(-len(expired)))
// Fire callbacks outside of any locks
p.cbMu.RLock()
fn := p.onExpire
p.cbMu.RUnlock()
if fn != nil {
for _, e := range expired {
fn(e.key, e.value)
}
}
return len(expired)
}
func (p *PebbleStore) Len() int {
return int(p.count.Load())
}
func (p *PebbleStore) Close() error {
return p.db.Close()
}
func (p *PebbleStore) SetOnEvict(fn func(key string, value []byte)) {
p.cbMu.Lock()
p.onEvict = fn
p.cbMu.Unlock()
}
func (p *PebbleStore) SetOnExpire(fn func(key string, value []byte)) {
p.cbMu.Lock()
p.onExpire = fn
p.cbMu.Unlock()
}
// countKeys iterates through all keys to get initial count.
func (p *PebbleStore) countKeys() int64 {
var count int64
iter, err := p.db.NewIter(nil)
if err != nil {
return 0
}
defer iter.Close()
for iter.First(); iter.Valid(); iter.Next() {
count++
}
return count
}