-
Notifications
You must be signed in to change notification settings - Fork 3
/
store.go
359 lines (292 loc) · 8.11 KB
/
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
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
package main
import (
"bufio"
"crypto/rand"
"errors"
"fmt"
"io"
"log/slog"
"math/big"
"os"
"path/filepath"
"strings"
"time"
"github.com/akamensky/base58"
"github.com/timshannon/badgerhold/v4"
)
const (
DirDatabase = "db"
DirStorage = "data"
)
// ErrNotFound is returned by the `Store.Get` method if there is no Item for
// the requested ID.
var ErrNotFound = errors.New("No Item found for this ID")
// BadgerLogWapper implements badger.Logger to forward logs to log/slog.
type BadgerLogWapper struct {
*slog.Logger
}
func (logger *BadgerLogWapper) Errorf(f string, args ...interface{}) {
logger.Logger.Error(fmt.Sprintf(f, args...), slog.String("producer", "badger"))
}
func (logger *BadgerLogWapper) Warningf(f string, args ...interface{}) {
logger.Logger.Warn(fmt.Sprintf(f, args...), slog.String("producer", "badger"))
}
func (logger *BadgerLogWapper) Infof(f string, args ...interface{}) {
logger.Logger.Info(fmt.Sprintf(f, args...), slog.String("producer", "badger"))
}
func (logger *BadgerLogWapper) Debugf(f string, args ...interface{}) {
logger.Logger.Debug(fmt.Sprintf(f, args...), slog.String("producer", "badger"))
}
// randomIdGenerator returns an ID generator for the "random" type.
func randomIdGenerator(length int) func() (string, error) {
return func() (string, error) {
// n bytes or randomness, which would be for n = 4:
// 4*8 = 32 Bits of randomness; 2^32 = 4 294 967 296 possible combinations
idBuff := make([]byte, length)
_, err := rand.Read(idBuff)
if err != nil {
return "", err
}
return string(base58.Encode(idBuff)), nil
}
}
// wordlistIdGenerator returns an ID generator for the "wordlist" type.
func wordlistIdGenerator(sourceFile string, length int) (func() (string, error), error) {
f, err := os.Open(sourceFile)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()
scanner := bufio.NewScanner(f)
words := make([]string, 0, 1024)
for scanner.Scan() {
words = append(words, scanner.Text())
}
err = scanner.Err()
if err != nil {
return nil, err
}
return func() (string, error) {
parts := make([]string, length)
for i := 0; i < length; i++ {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(words))))
if err != nil {
return "", err
}
parts[i] = words[int(n.Int64())]
}
return strings.Join(parts, "-"), nil
}, nil
}
// Store stores an index of all Items as well as the pure files.
type Store struct {
baseDir string
bh *badgerhold.Store
idGenerator func() (string, error)
cleanup bool
stopSyn chan struct{}
stopAck chan struct{}
}
// NewStore opens or initializes a Store in the given directory.
//
// autoCleanup specifies if both a background cleanup job will be launched as
// well as deleting expired Items after being retrieved.
func NewStore(
baseDir string,
idGenerator func() (string, error),
autoCleanup bool,
) (s *Store, err error) {
s = &Store{
baseDir: baseDir,
idGenerator: idGenerator,
cleanup: autoCleanup,
}
slog.Info("Opening Store", slog.String("directory", baseDir))
for _, dir := range []string{baseDir, s.databaseDir(), s.storageDir()} {
_, stat := os.Stat(dir)
if !os.IsNotExist(stat) {
continue
}
err = os.Mkdir(dir, 0700)
if err != nil {
slog.Error("Cannot create directory", slog.String("directory", dir), slog.Any("error", err))
return
}
}
opts := badgerhold.DefaultOptions
opts.Dir = s.databaseDir()
opts.ValueDir = opts.Dir
opts.Logger = &BadgerLogWapper{slog.Default()}
opts.Options.BaseLevelSize = 1 << 21 // 2MiB
opts.Options.ValueLogFileSize = 1 << 24 // 16MiB
opts.Options.BaseTableSize = 1 << 20 // 1MiB
s.bh, err = badgerhold.Open(opts)
if err != nil {
return
}
if s.cleanup {
s.stopSyn = make(chan struct{})
s.stopAck = make(chan struct{})
go s.cleanupExired()
}
return
}
// databaseDir returns the database subdirectory.
func (s Store) databaseDir() string {
return filepath.Join(s.baseDir, DirDatabase)
}
// storageDir returns the file storage subdirectory.
func (s Store) storageDir() string {
return filepath.Join(s.baseDir, DirStorage)
}
// cleanupExired runs in a background goroutine to clean up expired Items.
func (s *Store) cleanupExired() {
var ticker = time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-s.stopSyn:
close(s.stopAck)
return
case <-ticker.C:
if err := s.deleteExpired(); err != nil {
slog.Error("Deletion of expired Items failed", slog.Any("error", err))
}
}
}
}
// createID creates an ID for a new Item based on the Store.idGenerator.
func (s *Store) createID() (string, error) {
for i := 0; i < 32; i++ {
id, err := s.idGenerator()
if err != nil {
return "", err
}
err = s.bh.Get(id, Item{})
switch err {
case nil:
// Continue if this ID is already in use
continue
case badgerhold.ErrNotFound:
// Use this ID if there is no such entry
return id, nil
default:
// Otherwise, pass error along
return "", err
}
}
return "", errors.New("failed to calculate a free ID")
}
// Close the Store and its database.
func (s *Store) Close() error {
slog.Info("Closing Store")
if s.cleanup {
close(s.stopSyn)
<-s.stopAck
}
return s.bh.Close()
}
// Get an Item by its ID. The Item's file can be accessed with GetFile.
func (s *Store) Get(id string) (i Item, err error) {
slog.Debug("Requested Item from Store", slog.String("id", id))
err = s.bh.Get(id, &i)
if err == badgerhold.ErrNotFound {
slog.Debug("Requested Item was not found", slog.String("id", id))
err = ErrNotFound
return
} else if err != nil {
slog.Error("Requesting Item failed", slog.String("id", id))
return
}
if s.cleanup && i.Expires.Before(time.Now()) {
slog.Info("Requested Item is expired, will be deleted",
slog.String("id", id), slog.Any("expires", i.Expires))
err = s.Delete(i.ID)
if err != nil {
slog.Error("Failed to delete expired Item", slog.String("id", id), slog.Any("error", err))
return
}
err = ErrNotFound
}
return
}
// GetFile creates a ReadCloser for a stored Item file by this ID.
func (s *Store) GetFile(id string) (*os.File, error) {
return os.Open(filepath.Join(s.storageDir(), id))
}
// Put a new Item inside the Store.
//
// Both a database entry and a file will be created. The given file will be
// read into the storage and closed afterwards.
func (s *Store) Put(i Item, file io.ReadCloser) (id string, err error) {
slog.Debug("Requested insertion of Item into the Store")
id, err = s.createID()
if err != nil {
slog.Error("Failed to create an ID for a new Item", slog.Any("error", err))
return
}
i.ID = id
slog.Debug("Insert Item with assigned ID", slog.String("id", i.ID))
err = s.bh.Insert(i.ID, i)
if err != nil {
slog.Error("Failed to insert Item into database",
slog.String("id", i.ID), slog.Any("error", err))
return
}
f, err := os.Create(filepath.Join(s.storageDir(), i.ID))
if err != nil {
slog.Error("Failed to create file",
slog.String("id", i.ID), slog.Any("error", err))
return
}
_, err = io.Copy(f, file)
if err != nil {
return
}
err = file.Close()
if err != nil {
return
}
err = f.Close()
if err != nil {
return
}
return
}
// deleteExpired checks the Store for expired Items and deletes them.
func (s *Store) deleteExpired() error {
var items []Item
err := s.bh.Find(&items, badgerhold.Where("Expires").Lt(time.Now()))
if err != nil {
return err
}
for _, i := range items {
slog.Debug("Delete expired Item", slog.String("id", i.ID))
err := s.Delete(i.ID)
if err != nil {
return err
}
}
return nil
}
// Delte an Item. Both the database entry and the file will be removed.
func (s *Store) Delete(id string) (err error) {
slog.Debug("Requested deletion of Item", slog.String("id", id))
err = s.bh.Delete(&id, Item{})
if err != nil {
slog.Error("Failed to delete Item from database",
slog.String("id", id), slog.Any("error", err))
return
}
err = os.Remove(filepath.Join(s.storageDir(), id))
if err != nil {
slog.Error("Failed to delete Item's file",
slog.String("id", id), slog.Any("error", err))
return
}
return
}
// BadgerHold returns a reference to the underlying BadgerHold instance.
func (s *Store) BadgerHold() *badgerhold.Store {
return s.bh
}