-
Notifications
You must be signed in to change notification settings - Fork 0
/
thead_safe_store.go
69 lines (59 loc) · 2.32 KB
/
thead_safe_store.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
package sqlcache
import (
"github.com/pkg/errors"
"k8s.io/client-go/tools/cache"
)
// threadSafeStore is a SQLite-backed cache.ThreadSafeStore which builds upon Index
type threadSafeStore struct {
*Indexer
}
// NewThreadSafeStore returns a cache.ThreadSafeStore backed by SQLite for the example type
func NewThreadSafeStore(example any, path string, indexers cache.Indexers) (cache.ThreadSafeStore, error) {
i, err := NewIndexer(example, dummyKeyFunc, path, indexers)
if err != nil {
return nil, err
}
return &threadSafeStore{i}, nil
}
// Add saves an obj with its key, or updates key with obj if it exists in this store
// Note: I/O errors will panic this function, as the interface signature does not allow returning errors
func (t threadSafeStore) Add(key string, obj any) {
err := t.Upsert(key, obj)
if err != nil {
panic(errors.Wrap(err, "Unexpected error in threadSafeStore.Add"))
}
}
// Update delegates to Add
// Note: I/O errors will panic this function, as the interface signature does not allow returning errors
func (t threadSafeStore) Update(key string, obj any) {
t.Add(key, obj)
}
// Delete deletes the object associated with key, if it exists in this store
// Note: I/O errors will panic this function, as the interface signature does not allow returning errors
func (t threadSafeStore) Delete(key string) {
err := t.DeleteByKey(key)
if err != nil {
panic(errors.Wrap(err, "Unexpected error in threadSafeStore.Delete"))
}
}
// Get returns the object associated with the given object's key
// Note: I/O errors will panic this function, as the interface signature does not allow returning errors
func (t threadSafeStore) Get(key string) (any, bool) {
item, exists, err := t.GetByKey(key)
if err != nil {
panic(errors.Wrap(err, "Unexpected error in threadSafeStore.Get"))
}
return item, exists
}
// Replace will delete the contents of the store, using instead the given list
// Note: I/O errors will panic this function, as the interface signature does not allow returning errors
func (t threadSafeStore) Replace(m map[string]any, _ string) {
err := t.ReplaceByKey(m)
if err != nil {
panic(errors.Wrap(err, "Unexpected error in threadSafeStore.Replace"))
}
}
// dummyKeyFunc panics - ThreadSafeStore is designed to work without one
func dummyKeyFunc(obj any) (string, error) {
panic("keyFunc called from ThreadSafeStore")
}