1
- package store
1
+ package file
2
2
3
3
import (
4
4
"context"
@@ -10,6 +10,7 @@ import (
10
10
"sync"
11
11
"time"
12
12
13
+ "go-micro.dev/v5/store"
13
14
bolt "go.etcd.io/bbolt"
14
15
)
15
16
26
27
dataBucket = "data"
27
28
)
28
29
29
- func NewFileStore (opts ... Option ) Store {
30
+ func NewStore (opts ... store. Option ) store. Store {
30
31
s := & fileStore {
31
32
handles : make (map [string ]* fileHandle ),
32
33
}
@@ -35,7 +36,7 @@ func NewFileStore(opts ...Option) Store {
35
36
}
36
37
37
38
type fileStore struct {
38
- options Options
39
+ options store. Options
39
40
dir string
40
41
41
42
// the database handle
@@ -70,7 +71,7 @@ func (m *fileStore) delete(fd *fileHandle, key string) error {
70
71
})
71
72
}
72
73
73
- func (m * fileStore ) init (opts ... Option ) error {
74
+ func (m * fileStore ) init (opts ... store. Option ) error {
74
75
for _ , o := range opts {
75
76
o (& m .options )
76
77
}
@@ -206,7 +207,7 @@ func (m *fileStore) list(fd *fileHandle, limit, offset uint) []string {
206
207
return allKeys
207
208
}
208
209
209
- func (m * fileStore ) get (fd * fileHandle , k string ) (* Record , error ) {
210
+ func (m * fileStore ) get (fd * fileHandle , k string ) (* store. Record , error ) {
210
211
var value []byte
211
212
212
213
fd .db .View (func (tx * bolt.Tx ) error {
@@ -221,7 +222,7 @@ func (m *fileStore) get(fd *fileHandle, k string) (*Record, error) {
221
222
})
222
223
223
224
if value == nil {
224
- return nil , ErrNotFound
225
+ return nil , store . ErrNotFound
225
226
}
226
227
227
228
storedRecord := & record {}
@@ -230,7 +231,7 @@ func (m *fileStore) get(fd *fileHandle, k string) (*Record, error) {
230
231
return nil , err
231
232
}
232
233
233
- newRecord := & Record {}
234
+ newRecord := & store. Record {}
234
235
newRecord .Key = storedRecord .Key
235
236
newRecord .Value = storedRecord .Value
236
237
newRecord .Metadata = make (map [string ]interface {})
@@ -241,15 +242,15 @@ func (m *fileStore) get(fd *fileHandle, k string) (*Record, error) {
241
242
242
243
if ! storedRecord .ExpiresAt .IsZero () {
243
244
if storedRecord .ExpiresAt .Before (time .Now ()) {
244
- return nil , ErrNotFound
245
+ return nil , store . ErrNotFound
245
246
}
246
247
newRecord .Expiry = time .Until (storedRecord .ExpiresAt )
247
248
}
248
249
249
250
return newRecord , nil
250
251
}
251
252
252
- func (m * fileStore ) set (fd * fileHandle , r * Record ) error {
253
+ func (m * fileStore ) set (fd * fileHandle , r * store. Record ) error {
253
254
// copy the incoming record and then
254
255
// convert the expiry in to a hard timestamp
255
256
item := & record {}
@@ -291,12 +292,12 @@ func (f *fileStore) Close() error {
291
292
return nil
292
293
}
293
294
294
- func (f * fileStore ) Init (opts ... Option ) error {
295
+ func (f * fileStore ) Init (opts ... store. Option ) error {
295
296
return f .init (opts ... )
296
297
}
297
298
298
- func (m * fileStore ) Delete (key string , opts ... DeleteOption ) error {
299
- var deleteOptions DeleteOptions
299
+ func (m * fileStore ) Delete (key string , opts ... store. DeleteOption ) error {
300
+ var deleteOptions store. DeleteOptions
300
301
for _ , o := range opts {
301
302
o (& deleteOptions )
302
303
}
@@ -309,8 +310,8 @@ func (m *fileStore) Delete(key string, opts ...DeleteOption) error {
309
310
return m .delete (fd , key )
310
311
}
311
312
312
- func (m * fileStore ) Read (key string , opts ... ReadOption ) ([]* Record , error ) {
313
- var readOpts ReadOptions
313
+ func (m * fileStore ) Read (key string , opts ... store. ReadOption ) ([]* store. Record , error ) {
314
+ var readOpts store. ReadOptions
314
315
for _ , o := range opts {
315
316
o (& readOpts )
316
317
}
@@ -342,7 +343,7 @@ func (m *fileStore) Read(key string, opts ...ReadOption) ([]*Record, error) {
342
343
keys = []string {key }
343
344
}
344
345
345
- var results []* Record
346
+ var results []* store. Record
346
347
347
348
for _ , k := range keys {
348
349
r , err := m .get (fd , k )
@@ -355,8 +356,8 @@ func (m *fileStore) Read(key string, opts ...ReadOption) ([]*Record, error) {
355
356
return results , nil
356
357
}
357
358
358
- func (m * fileStore ) Write (r * Record , opts ... WriteOption ) error {
359
- var writeOpts WriteOptions
359
+ func (m * fileStore ) Write (r * store. Record , opts ... store .WriteOption ) error {
360
+ var writeOpts store. WriteOptions
360
361
for _ , o := range opts {
361
362
o (& writeOpts )
362
363
}
@@ -368,7 +369,7 @@ func (m *fileStore) Write(r *Record, opts ...WriteOption) error {
368
369
369
370
if len (opts ) > 0 {
370
371
// Copy the record before applying options, or the incoming record will be mutated
371
- newRecord := Record {}
372
+ newRecord := store. Record {}
372
373
newRecord .Key = r .Key
373
374
newRecord .Value = r .Value
374
375
newRecord .Metadata = make (map [string ]interface {})
@@ -391,12 +392,12 @@ func (m *fileStore) Write(r *Record, opts ...WriteOption) error {
391
392
return m .set (fd , r )
392
393
}
393
394
394
- func (m * fileStore ) Options () Options {
395
+ func (m * fileStore ) Options () store. Options {
395
396
return m .options
396
397
}
397
398
398
- func (m * fileStore ) List (opts ... ListOption ) ([]string , error ) {
399
- var listOptions ListOptions
399
+ func (m * fileStore ) List (opts ... store. ListOption ) ([]string , error ) {
400
+ var listOptions store. ListOptions
400
401
401
402
for _ , o := range opts {
402
403
o (& listOptions )
@@ -439,9 +440,9 @@ func (m *fileStore) String() string {
439
440
440
441
type dirOptionKey struct {}
441
442
442
- // DirOption is a file store Option to set the directory for the file
443
- func DirOption (dir string ) Option {
444
- return func (o * Options ) {
443
+ // DirOption is a file store store. Option to set the directory for the file
444
+ func DirOption (dir string ) store. Option {
445
+ return func (o * store. Options ) {
445
446
if o .Context == nil {
446
447
o .Context = context .Background ()
447
448
}
0 commit comments