-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.go
271 lines (219 loc) · 4.95 KB
/
sqlite.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
package kvsqlite
import (
"database/sql"
"encoding/json"
"errors"
"sync"
"time"
_ "github.com/mattn/go-sqlite3"
)
// SQLite is a Key-Value Store in SQLite
type SQLite struct {
sync.RWMutex
Core *sql.DB
Config *SQLiteConfig
}
// SQLiteConfig is the configuration for Redis
type SQLiteConfig struct {
// Path to the SQLite database file.
Path string
// Prefix is the prefix to use for all keys
Prefix string
}
// New returns a new MemoryKV.
func New(cfg *SQLiteConfig) (*SQLite, error) {
if cfg.Path == "" {
return nil, errors.New("sqlite: path is required")
}
if cfg.Prefix == "" {
return nil, errors.New("prefix is required")
}
core, err := sql.Open("sqlite3", cfg.Path)
if err != nil {
return nil, err
}
// Create the table if it doesn't exist
_, err = core.Exec("CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY, value BLOB, expires_at INTEGER)")
if err != nil {
return nil, err
}
return &SQLite{
Core: core,
Config: cfg,
}, nil
}
func (m *SQLite) getKey(key string) string {
return m.Config.Prefix + key
}
func (m *SQLite) encodeValue(value any) (string, error) {
raw, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(raw), nil
}
func (m *SQLite) decodeValue(data []byte, value any) error {
return json.Unmarshal(data, value)
}
func now() int64 {
return time.Now().UnixMilli()
}
// Set sets the value for the given key.
// If maxAge is greater than 0, then the value will be expired after maxAge miliseconds.
func (m *SQLite) Set(key string, value any, maxAge ...time.Duration) error {
m.Lock()
// defer m.Unlock()
var expiresAt int64
if len(maxAge) > 0 {
expiresAt = now() + int64(maxAge[0]/time.Millisecond)
} else {
m.Unlock()
if m.Has(key) {
keyX := m.getKey(key)
stmt, err := m.Core.Prepare("SELECT expires_at FROM kv WHERE key = ?")
if err != nil {
panic(err)
}
res := stmt.QueryRow(keyX)
if res.Err() != nil {
panic(res.Err())
}
// use origin expiresAt
if err := res.Scan(&expiresAt); err != nil {
// panic(err)
m.RUnlock()
return nil
}
}
m.Lock()
}
keyX := m.getKey(key)
stmt, err := m.Core.Prepare("INSERT OR REPLACE INTO kv (key, value, expires_at) VALUES (?, ?, ?)")
if err != nil {
return err
}
valueX, err := m.encodeValue(value)
if err != nil {
return err
}
_, err = stmt.Exec(keyX, valueX, expiresAt)
if err != nil {
return err
}
m.Unlock()
return nil
}
// Get returns the value for the given key.
func (m *SQLite) Get(key string, value any) error {
m.RLock()
keyX := m.getKey(key)
stmt, err := m.Core.Prepare("SELECT value, expires_at FROM kv WHERE key = ?")
if err != nil {
panic(err)
}
res := stmt.QueryRow(keyX)
if res.Err() != nil {
panic(res.Err())
}
var valueX string
var expiresAt int64
if err := res.Scan(&valueX, &expiresAt); err != nil {
// panic(err)
m.RUnlock()
return nil
}
m.RUnlock()
if expiresAt > 0 && expiresAt < now() {
m.Delete(key)
return nil
}
return m.decodeValue([]byte(valueX), value)
}
// Delete deletes the value for the given key.
func (m *SQLite) Delete(key string) error {
m.Lock()
defer m.Unlock()
stmt, err := m.Core.Prepare("DELETE FROM kv WHERE key = ?")
if err != nil {
return err
}
_, err = stmt.Exec(m.getKey(key))
if err != nil {
return err
}
return nil
}
// Has returns true if the given key exists in the kv.
func (m *SQLite) Has(key string) bool {
m.RLock()
defer m.RUnlock()
stmt, err := m.Core.Prepare("SELECT 1 FROM kv WHERE key = ?")
if err != nil {
panic(err)
}
res := stmt.QueryRow(m.getKey(key))
if res.Err() != nil {
panic(res.Err())
}
var value int
if err := res.Scan(&value); err != nil {
return false
}
return value > 0
}
// Keys returns the keys of the kv.
func (m *SQLite) Keys() []string {
m.RLock()
defer m.RUnlock()
rows, err := m.Core.Query("SELECT key FROM kv where key like ?", m.Config.Prefix+"%")
if err != nil {
panic(err)
}
defer rows.Close()
keys := make([]string, 0)
for rows.Next() {
var key string
if err := rows.Scan(&key); err != nil {
panic(err)
}
keys = append(keys, key[len(m.Config.Prefix):])
}
return keys
}
// Size returns the number of elements in the kv.
func (m *SQLite) Size() int {
m.RLock()
defer m.RUnlock()
res, err := m.Core.Query("SELECT count(*) FROM kv where key like ?", m.Config.Prefix+"%")
if err != nil {
panic(err)
}
defer res.Close()
var count int
res.Next()
if err := res.Scan(&count); err != nil {
panic(err)
}
return count
}
// Clear removes all elements from the kv.
func (m *SQLite) Clear() error {
m.Lock()
defer m.Unlock()
_, err := m.Core.Exec("DELETE FROM kv where key like ?", m.Config.Prefix+"%")
return err
}
// ForEach calls the given function for each key-value pair in the kv.
func (m *SQLite) ForEach(f func(string, interface{})) {
m.RLock()
defer m.RUnlock()
keys := m.Keys()
for _, key := range keys {
var value any
if err := m.Get(key, &value); err != nil {
f(key, nil)
} else {
f(key, value)
}
}
}