-
Notifications
You must be signed in to change notification settings - Fork 13
/
kv.go
159 lines (117 loc) · 2.63 KB
/
kv.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
package kv
import (
"sync"
"github.com/pkg/errors"
"github.com/streamdal/streamdal/sdks/go/logger"
)
var (
ErrNilConfig = errors.New("config cannot be nil")
)
type KV struct {
cfg *Config
kvs map[string]string
kvsMtx *sync.RWMutex
log logger.Logger
}
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . IKV
type IKV interface {
// Get gets a value from the KV store; bool indicates if value exists
Get(key string) (string, bool)
// Set sets a key/value pair in the KV store; return bool indicates if key
// was overwritten
Set(key string, value string) bool
// Delete will delete a key from the KV store; return bool indicates if
// key existed before deletion
Delete(key string) bool
// Exists checks if a key exists in the KV store
Exists(key string) bool
// Purge removes all keys from the KV store; return int indicates how many
// keys were removed.
Purge() int64
// Keys returns a slice of all keys in the KV store
Keys() []string
// Items returns the number of keys in the KV store
Items() int64
}
type Config struct {
Logger logger.Logger
}
func New(cfg *Config) (*KV, error) {
if err := validateConfig(cfg); err != nil {
return nil, errors.Wrap(err, "unable to validate config")
}
return &KV{
cfg: cfg,
kvs: make(map[string]string),
kvsMtx: &sync.RWMutex{},
log: cfg.Logger,
}, nil
}
func (k *KV) Get(key string) (string, bool) {
k.kvsMtx.RLock()
defer k.kvsMtx.RUnlock()
if val, ok := k.kvs[key]; ok {
return val, true
}
return "", false
}
func (k *KV) Set(key string, value string) bool {
k.kvsMtx.Lock()
defer k.kvsMtx.Unlock()
if _, ok := k.kvs[key]; ok {
k.kvs[key] = value
return true
}
k.kvs[key] = value
return false
}
func (k *KV) Delete(key string) bool {
k.kvsMtx.Lock()
defer k.kvsMtx.Unlock()
exists := false
if _, ok := k.kvs[key]; ok {
exists = true
}
delete(k.kvs, key)
return exists
}
func (k *KV) Exists(key string) bool {
k.kvsMtx.RLock()
defer k.kvsMtx.RUnlock()
if _, ok := k.kvs[key]; ok {
return true
}
return false
}
func (k *KV) Purge() int64 {
k.kvsMtx.Lock()
defer k.kvsMtx.Unlock()
purged := int64(len(k.kvs))
k.kvs = make(map[string]string)
return purged
}
func (k *KV) Keys() []string {
k.kvsMtx.RLock()
defer k.kvsMtx.RUnlock()
keys := make([]string, len(k.kvs))
i := 0
for key := range k.kvs {
keys[i] = key
i++
}
return keys
}
func (k *KV) Items() int64 {
k.kvsMtx.RLock()
defer k.kvsMtx.RUnlock()
return int64(len(k.kvs))
}
func validateConfig(cfg *Config) error {
if cfg == nil {
return ErrNilConfig
}
if cfg.Logger == nil {
cfg.Logger = &logger.TinyLogger{}
}
return nil
}