Skip to content

Commit 97275d3

Browse files
authored
WIP: move file store (do not merge) (#2766)
* move file store * fix build
1 parent e29159e commit 97275d3

File tree

3 files changed

+64
-62
lines changed

3 files changed

+64
-62
lines changed

store/file.go renamed to store/file/file.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package store
1+
package file
22

33
import (
44
"context"
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111
"time"
1212

13+
"go-micro.dev/v5/store"
1314
bolt "go.etcd.io/bbolt"
1415
)
1516

@@ -26,7 +27,7 @@ var (
2627
dataBucket = "data"
2728
)
2829

29-
func NewFileStore(opts ...Option) Store {
30+
func NewStore(opts ...store.Option) store.Store {
3031
s := &fileStore{
3132
handles: make(map[string]*fileHandle),
3233
}
@@ -35,7 +36,7 @@ func NewFileStore(opts ...Option) Store {
3536
}
3637

3738
type fileStore struct {
38-
options Options
39+
options store.Options
3940
dir string
4041

4142
// the database handle
@@ -70,7 +71,7 @@ func (m *fileStore) delete(fd *fileHandle, key string) error {
7071
})
7172
}
7273

73-
func (m *fileStore) init(opts ...Option) error {
74+
func (m *fileStore) init(opts ...store.Option) error {
7475
for _, o := range opts {
7576
o(&m.options)
7677
}
@@ -206,7 +207,7 @@ func (m *fileStore) list(fd *fileHandle, limit, offset uint) []string {
206207
return allKeys
207208
}
208209

209-
func (m *fileStore) get(fd *fileHandle, k string) (*Record, error) {
210+
func (m *fileStore) get(fd *fileHandle, k string) (*store.Record, error) {
210211
var value []byte
211212

212213
fd.db.View(func(tx *bolt.Tx) error {
@@ -221,7 +222,7 @@ func (m *fileStore) get(fd *fileHandle, k string) (*Record, error) {
221222
})
222223

223224
if value == nil {
224-
return nil, ErrNotFound
225+
return nil, store.ErrNotFound
225226
}
226227

227228
storedRecord := &record{}
@@ -230,7 +231,7 @@ func (m *fileStore) get(fd *fileHandle, k string) (*Record, error) {
230231
return nil, err
231232
}
232233

233-
newRecord := &Record{}
234+
newRecord := &store.Record{}
234235
newRecord.Key = storedRecord.Key
235236
newRecord.Value = storedRecord.Value
236237
newRecord.Metadata = make(map[string]interface{})
@@ -241,15 +242,15 @@ func (m *fileStore) get(fd *fileHandle, k string) (*Record, error) {
241242

242243
if !storedRecord.ExpiresAt.IsZero() {
243244
if storedRecord.ExpiresAt.Before(time.Now()) {
244-
return nil, ErrNotFound
245+
return nil, store.ErrNotFound
245246
}
246247
newRecord.Expiry = time.Until(storedRecord.ExpiresAt)
247248
}
248249

249250
return newRecord, nil
250251
}
251252

252-
func (m *fileStore) set(fd *fileHandle, r *Record) error {
253+
func (m *fileStore) set(fd *fileHandle, r *store.Record) error {
253254
// copy the incoming record and then
254255
// convert the expiry in to a hard timestamp
255256
item := &record{}
@@ -291,12 +292,12 @@ func (f *fileStore) Close() error {
291292
return nil
292293
}
293294

294-
func (f *fileStore) Init(opts ...Option) error {
295+
func (f *fileStore) Init(opts ...store.Option) error {
295296
return f.init(opts...)
296297
}
297298

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
300301
for _, o := range opts {
301302
o(&deleteOptions)
302303
}
@@ -309,8 +310,8 @@ func (m *fileStore) Delete(key string, opts ...DeleteOption) error {
309310
return m.delete(fd, key)
310311
}
311312

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
314315
for _, o := range opts {
315316
o(&readOpts)
316317
}
@@ -342,7 +343,7 @@ func (m *fileStore) Read(key string, opts ...ReadOption) ([]*Record, error) {
342343
keys = []string{key}
343344
}
344345

345-
var results []*Record
346+
var results []*store.Record
346347

347348
for _, k := range keys {
348349
r, err := m.get(fd, k)
@@ -355,8 +356,8 @@ func (m *fileStore) Read(key string, opts ...ReadOption) ([]*Record, error) {
355356
return results, nil
356357
}
357358

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
360361
for _, o := range opts {
361362
o(&writeOpts)
362363
}
@@ -368,7 +369,7 @@ func (m *fileStore) Write(r *Record, opts ...WriteOption) error {
368369

369370
if len(opts) > 0 {
370371
// Copy the record before applying options, or the incoming record will be mutated
371-
newRecord := Record{}
372+
newRecord := store.Record{}
372373
newRecord.Key = r.Key
373374
newRecord.Value = r.Value
374375
newRecord.Metadata = make(map[string]interface{})
@@ -391,12 +392,12 @@ func (m *fileStore) Write(r *Record, opts ...WriteOption) error {
391392
return m.set(fd, r)
392393
}
393394

394-
func (m *fileStore) Options() Options {
395+
func (m *fileStore) Options() store.Options {
395396
return m.options
396397
}
397398

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
400401

401402
for _, o := range opts {
402403
o(&listOptions)
@@ -439,9 +440,9 @@ func (m *fileStore) String() string {
439440

440441
type dirOptionKey struct{}
441442

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) {
445446
if o.Context == nil {
446447
o.Context = context.Background()
447448
}

0 commit comments

Comments
 (0)