-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
234 lines (195 loc) · 5.18 KB
/
storage.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
package certmagicsqlite3
import (
"context"
"database/sql"
"errors"
"io/fs"
"time"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/certmagic"
_ "modernc.org/sqlite"
)
type Storage struct {
DataSourceName string `json:"datasourcename,omitempty"`
db *sql.DB
lockExpirationTimeOut time.Duration
}
func OpenSQLiteStorage(dataSourceName string) (Storage, error) {
if dataSourceName == "" {
return Storage{}, errors.New("data source cannot be empty")
}
db, err := sql.Open("sqlite", dataSourceName)
if err != nil {
return Storage{}, err
}
for _, stmt := range []string{pragmaWALEnabled, pragma500BusyTimeout, pragmaCaseSenstive} {
_, err = db.Exec(stmt, nil)
if err != nil {
return Storage{}, err
}
}
_, err = db.Exec(createTable)
if err != nil {
return Storage{}, err
}
s := Storage{
db: db,
lockExpirationTimeOut: defaultLockTimeOut,
}
return s, nil
}
func (s *Storage) Store(ctx context.Context, key string, value []byte) error {
if key == "" {
return errors.New("key cannot be empty")
}
storeContext, cancel := context.WithCancel(ctx)
defer cancel()
if len(value) == 0 {
return errors.New("value cannot be empty")
}
stmt, err := s.db.Prepare(insertKey)
if err != nil {
return err
}
t := time.Now()
_, err = stmt.ExecContext(storeContext, key, value, t.Format(time.Layout), len(value))
if err != nil {
return err
}
return nil
}
func (s *Storage) Load(ctx context.Context, key string) ([]byte, error) {
loadContext, cancel := context.WithCancel(ctx)
defer cancel()
value := []byte{}
if err := s.db.QueryRowContext(loadContext, getKey, key).Scan(&value); err != nil {
if err == sql.ErrNoRows {
return nil, fs.ErrNotExist
}
return nil, err
}
return value, nil
}
func (s *Storage) Exists(ctx context.Context, key string) bool {
existsContext, cancel := context.WithCancel(ctx)
defer cancel()
var value []byte
if err := s.db.QueryRowContext(existsContext, getKey, key).Scan(&value); err != nil {
return false
}
return true
}
func (s *Storage) Delete(ctx context.Context, key string) error {
deleteContext, cancel := context.WithCancel(ctx)
defer cancel()
if key == "" {
return errors.New("key cannot be empty")
}
stmt, err := s.db.Prepare(deleteKey)
if err != nil {
return err
}
result, err := stmt.ExecContext(deleteContext, key)
if err != nil {
return err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return fs.ErrNotExist
}
return nil
}
func (s *Storage) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
listContext, cancel := context.WithCancel(ctx)
defer cancel()
if recursive {
return nil, errors.New("recursive not supported")
}
rows, err := s.db.QueryContext(listContext, listKey, prefix)
if err != nil {
return nil, err
}
defer rows.Close()
keyList := make([]string, 0)
var key string
if rows.Next() {
err = rows.Scan(&key)
if err != nil {
return keyList, err
}
keyList = append(keyList, key)
for rows.Next() {
err = rows.Scan(&key)
if err != nil {
return keyList, err
}
keyList = append(keyList, key)
}
} else {
return keyList, fs.ErrNotExist
}
if err = rows.Err(); err != nil {
return keyList, err
}
return keyList, nil
}
func (s *Storage) Stat(ctx context.Context, key string) (certmagic.KeyInfo, error) {
statContext, cancel := context.WithCancel(ctx)
defer cancel()
var modifiedString string
var size int64
if err := s.db.QueryRowContext(statContext, statKey, key).Scan(&modifiedString, &size); err != nil {
if err == sql.ErrNoRows {
return certmagic.KeyInfo{}, fs.ErrNotExist
}
return certmagic.KeyInfo{}, err
}
modified, err := time.Parse(time.Layout, modifiedString)
if err != nil {
return certmagic.KeyInfo{}, err
}
keyInfo := certmagic.KeyInfo{
Key: key,
Modified: modified,
Size: size,
}
return keyInfo, nil
}
// Lock unimplemented TODO
func (s *Storage) Lock(ctx context.Context, name string) error {
return errors.New("unimplemented")
}
// UnLock unimplemented TODO
func (s *Storage) Unlock(ctx context.Context, name string) error {
return errors.New("unimplemented")
}
func (s *Storage) SetLockTimeOut(timeout time.Duration) {
s.lockExpirationTimeOut = timeout
}
func (s *Storage) CertMagicStorage() (certmagic.Storage, error) {
return s, nil
}
const createTable = `
CREATE TABLE IF NOT EXISTS certmagic(
key TEXT NOT NULL PRIMARY KEY,
value BLOB NOT NULL,
modified TEXT NOT NULL,
size INTEGER NOT NULL
);`
const pragmaWALEnabled = `PRAGMA journal_mode = WAL;`
const pragma500BusyTimeout = `PRAGMA busy_timeout = 5000;`
const pragmaCaseSenstive = `PRAGMA case_sensitive_like = true;`
const insertKey = `INSERT OR REPLACE INTO certmagic(key, value, modified, size) VALUES (?, ?, ?, ?);`
const getKey = `SELECT value from certmagic WHERE key = ?`
const listKey = `SELECT key from certmagic WHERE key LIKE ? || '%' `
const statKey = `SELECT modified,size from certmagic WHERE key = ?`
const deleteKey = `DELETE FROM certmagic WHERE key=?`
const defaultLockTimeOut = 500 * time.Millisecond
// interface guards
var (
_ certmagic.Storage = (*Storage)(nil)
_ caddy.StorageConverter = (*Storage)(nil)
)