-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
381 lines (301 loc) · 7.53 KB
/
db.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package inventory
import (
"maps"
"slices"
"sync"
)
// DB maintains items under keys indexed also by tags in order to be able to
// evict all items under a specific tag
type DB interface {
DBViewer
DBWriter
// View provides atomic read-only access to the db for the scope of the
// provided callback, isolated from other operations on the db.
View(func(viewer DBViewer) error) error
// Update provides atomic read-write access to the db for the scope of
// the provided callback.
Update(func(writer DBWriter) error) error
// GetOrFill is a nice utility that wraps get, put if not exist and return
// the value,
GetOrFill(key string, fill func() (any, error), tags ...string) (val any, err error)
// Invalidate deletes all keys related to the provided tags
Invalidate(tags ...string) (deleted []string)
}
// DBViewer represents isolated read access handle to the db
type DBViewer interface {
// Get safely retrieves a val identified by the provided key
Get(key string) (val any, ok bool)
// Iter retrieves all the keys under a tag and let you access each item in each key
Iter(tag string, fn func(key string, getVal func() (any, bool)) (proceed bool))
}
// DBWriter represents isolated write access handle to the db
type DBWriter interface {
DBViewer
// Put safely sets the provided val under the provided key indexed by the
// provided tags
Put(key string, val any)
// Tag simply adds tag on a key
Tag(key string, tags ...string)
// Invalidate deletes all keys related to the provided tags
Invalidate(tags ...string) (deleted []string)
}
func NewDB() DB {
return &db{
storage: storage{
map[string]map[string]struct{}{},
map[string]map[string]struct{}{},
map[string]any{},
},
}
}
type storage struct {
tagToKeys map[string]map[string]struct{}
keyToTags map[string]map[string]struct{}
items map[string]any
}
type db struct {
storage
t transaction
muR sync.RWMutex
muW sync.Mutex
}
func (c *db) Invalidate(tags ...string) (deleted []string) {
_ = c.Update(func(writer DBWriter) error {
deleted = writer.Invalidate(tags...)
return nil
})
return
}
func (c *db) Tag(key string, tags ...string) {
_ = c.Update(func(writer DBWriter) error {
writer.Tag(key, tags...)
return nil
})
}
func (c *db) View(viewFn func(DBViewer) error) (err error) {
c.muR.RLock()
err = viewFn(cacheView(c.storage))
c.muR.RUnlock()
return
}
func (c *db) Update(updateFn func(DBWriter) error) (err error) {
c.muW.Lock()
c.t.origin = &c.storage
if c.t.additions.items == nil || c.t.additions.keyToTags == nil || c.t.additions.tagToKeys == nil {
c.t.additions = storage{
map[string]map[string]struct{}{},
map[string]map[string]struct{}{},
map[string]any{},
}
}
if c.t.deletions.items == nil || c.t.deletions.keyToTags == nil || c.t.deletions.tagToKeys == nil {
c.t.deletions = storage{
map[string]map[string]struct{}{},
map[string]map[string]struct{}{},
map[string]any{},
}
}
err = updateFn(&c.t)
if err == nil {
c.muR.Lock()
for k := range c.t.deletions.items {
delete(c.storage.items, k)
}
for k := range c.t.deletions.keyToTags {
delete(c.storage.keyToTags, k)
}
for k := range c.t.deletions.tagToKeys {
delete(c.storage.tagToKeys, k)
}
for k, v := range c.t.additions.items {
c.storage.items[k] = v
}
for k, v := range c.t.additions.keyToTags {
c.storage.keyToTags[k] = v
}
for k, v := range c.t.additions.tagToKeys {
c.storage.tagToKeys[k] = v
}
c.muR.Unlock()
}
clear(c.t.additions.items)
clear(c.t.additions.keyToTags)
clear(c.t.additions.tagToKeys)
clear(c.t.deletions.items)
clear(c.t.deletions.keyToTags)
clear(c.t.deletions.tagToKeys)
c.muW.Unlock()
return
}
func (c *db) Get(key string) (val any, ok bool) {
c.muR.RLock()
val, ok = cacheView(c.storage).Get(key)
c.muR.RUnlock()
return
}
func (c *db) Iter(tag string, fn func(key string, val func() (any, bool)) (proceed bool)) {
c.muR.RLock()
cacheView(c.storage).Iter(tag, fn)
c.muR.RUnlock()
return
}
func (c *db) Put(key string, val any) {
_ = c.Update(func(writer DBWriter) error {
writer.Put(key, val)
return nil
})
return
}
func (c *db) GetOrFill(key string, fill func() (any, error), tags ...string) (val any, err error) {
val, ok := c.Get(key)
if ok {
return
}
err = c.Update(func(writer DBWriter) error {
val, ok = writer.Get(key)
if ok {
return nil
}
val, err = fill()
if err != nil {
return err
}
writer.Put(key, val)
writer.Tag(key, tags...)
return nil
})
return
}
type cacheView storage
func (c cacheView) Get(key string) (val any, ok bool) {
val, ok = c.items[key]
return
}
func (c cacheView) Iter(tag string, fn func(key string, val func() (any, bool)) bool) {
keys, ok := c.tagToKeys[tag]
if !ok {
return
}
for k := range keys {
if !fn(k, func() (any, bool) {
return c.Get(k)
}) {
break
}
}
return
}
type transaction struct {
origin *storage
additions storage
deletions storage
}
func (c *transaction) Tag(key string, tags ...string) {
if _, ok := c.additions.keyToTags[key]; !ok {
_, ok = c.origin.keyToTags[key]
if ok {
c.additions.keyToTags[key] = make(map[string]struct{}, len(c.origin.keyToTags[key]))
maps.Copy(c.additions.keyToTags[key], c.origin.keyToTags[key])
} else {
c.additions.keyToTags[key] = make(map[string]struct{})
}
}
for _, tag := range tags {
if _, ok := c.additions.tagToKeys[tag]; !ok {
c.additions.tagToKeys[tag], ok = c.origin.tagToKeys[tag]
if ok {
c.additions.tagToKeys[tag] = make(map[string]struct{}, len(c.origin.tagToKeys[tag]))
maps.Copy(c.additions.tagToKeys[tag], c.origin.tagToKeys[tag])
} else {
c.additions.tagToKeys[tag] = make(map[string]struct{})
}
}
c.additions.tagToKeys[tag][key] = struct{}{}
c.additions.keyToTags[key][tag] = struct{}{}
}
}
func (c *transaction) Get(key string) (val any, ok bool) {
val, ok = c.additions.items[key]
if ok {
return
}
originVal, ok := c.origin.items[key]
if !ok {
return
}
if _, ok = c.deletions.items[key]; !ok {
return originVal, true
}
return
}
func (c *transaction) Iter(tag string, fn func(key string, val func() (any, bool)) bool) {
_, deleted := c.deletions.tagToKeys[tag]
if deleted {
return
}
var keys map[string]struct{}
maps.Copy(keys, c.origin.tagToKeys[tag])
addedKeys, ok := c.additions.tagToKeys[tag]
if ok {
maps.Copy(keys, addedKeys)
}
for k := range c.additions.tagToKeys[tag] {
if _, ok = c.deletions.items[k]; ok {
continue
}
proceed := fn(k, func() (any, bool) {
return c.Get(k)
})
if !proceed {
break
}
}
return
}
func (c *transaction) Put(key string, val any) {
c.additions.items[key] = val
return
}
func (c *transaction) Invalidate(tags ...string) (deleted []string) {
for _, tag := range tags {
_, ok := c.deletions.tagToKeys[tag]
if ok {
continue
}
keys, ok := c.additions.tagToKeys[tag]
if !ok {
keys, ok = c.origin.tagToKeys[tag]
}
if !ok {
continue
}
delete(c.additions.tagToKeys, tag)
c.deletions.tagToKeys[tag] = nil
for k := range keys {
c.deleteKey(k)
if !slices.Contains(deleted, k) {
deleted = append(deleted, k)
}
}
c.deleteKey(tag)
if !slices.Contains(deleted, tag) {
deleted = append(deleted, tag)
}
}
return
}
func (c *transaction) deleteKey(key string) {
delete(c.additions.items, key)
c.deletions.items[key] = nil
tags, ok := c.additions.keyToTags[key]
if !ok {
tags, _ = c.origin.keyToTags[key]
}
for tag := range tags {
if _, ok = c.deletions.tagToKeys[tag]; ok {
continue
}
delete(c.additions.tagToKeys[tag], key)
c.deletions.tagToKeys[tag] = nil
}
}