fix file tests
This commit is contained in:
@@ -3,7 +3,6 @@ package file
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -11,10 +10,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/micro/go-micro/v2/store"
|
||||
micro_store "github.com/micro/go-micro/v2/store"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -29,49 +26,213 @@ var (
|
||||
|
||||
// NewStore returns a memory store
|
||||
func NewStore(opts ...store.Option) store.Store {
|
||||
s := &fileStore{
|
||||
options: store.Options{},
|
||||
}
|
||||
s := &fileStore{}
|
||||
s.init(opts...)
|
||||
return s
|
||||
}
|
||||
|
||||
type fileStore struct {
|
||||
options store.Options
|
||||
dir string
|
||||
fileName string
|
||||
fullFilePath string
|
||||
options store.Options
|
||||
dir string
|
||||
fileName string
|
||||
dbPath string
|
||||
// the database handle
|
||||
db *bolt.DB
|
||||
}
|
||||
|
||||
func (m *fileStore) Init(opts ...store.Option) error {
|
||||
return m.init(opts...)
|
||||
// record stored by us
|
||||
type record struct {
|
||||
Key string
|
||||
Value []byte
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (m *fileStore) delete(key string) error {
|
||||
return m.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte(m.options.Table))
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
return b.Delete([]byte(key))
|
||||
})
|
||||
}
|
||||
|
||||
func (m *fileStore) init(opts ...store.Option) error {
|
||||
for _, o := range opts {
|
||||
o(&m.options)
|
||||
}
|
||||
|
||||
if m.options.Database == "" {
|
||||
m.options.Database = DefaultDatabase
|
||||
}
|
||||
|
||||
if m.options.Table == "" {
|
||||
// bbolt requires bucketname to not be empty
|
||||
m.options.Table = DefaultTable
|
||||
}
|
||||
|
||||
// create a directory /tmp/micro
|
||||
dir := filepath.Join(DefaultDir, "micro")
|
||||
// create the database handle
|
||||
fname := m.options.Database + ".db"
|
||||
// Ignoring this as the folder might exist.
|
||||
// Reads/Writes updates will return with sensible error messages
|
||||
// about the dir not existing in case this cannot create the path anyway
|
||||
_ = os.Mkdir(dir, 0700)
|
||||
|
||||
m.dir = dir
|
||||
m.fileName = fname
|
||||
m.fullFilePath = filepath.Join(dir, fname)
|
||||
return nil
|
||||
m.dbPath = filepath.Join(dir, fname)
|
||||
|
||||
// close existing handle
|
||||
if m.db != nil {
|
||||
m.db.Close()
|
||||
}
|
||||
|
||||
// create new db handle
|
||||
db, err := bolt.Open(m.dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// set the new db
|
||||
m.db = db
|
||||
|
||||
// create the table
|
||||
return db.Update(func(tx *bolt.Tx) error {
|
||||
_, err := tx.CreateBucketIfNotExists([]byte(m.options.Table))
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (m *fileStore) String() string {
|
||||
return "local"
|
||||
func (m *fileStore) list(limit, offset uint) []string {
|
||||
var allItems []string
|
||||
|
||||
m.db.View(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte(m.options.Table))
|
||||
// nothing to read
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// @todo very inefficient
|
||||
if err := b.ForEach(func(k, v []byte) error {
|
||||
storedRecord := &record{}
|
||||
|
||||
if err := json.Unmarshal(v, storedRecord); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !storedRecord.ExpiresAt.IsZero() {
|
||||
if storedRecord.ExpiresAt.Before(time.Now()) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
allItems = append(allItems, string(k))
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
allKeys := make([]string, len(allItems))
|
||||
|
||||
for i, k := range allItems {
|
||||
allKeys[i] = k
|
||||
}
|
||||
|
||||
if limit != 0 || offset != 0 {
|
||||
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
||||
min := func(i, j uint) uint {
|
||||
if i < j {
|
||||
return i
|
||||
}
|
||||
return j
|
||||
}
|
||||
return allKeys[offset:min(limit, uint(len(allKeys)))]
|
||||
}
|
||||
|
||||
return allKeys
|
||||
}
|
||||
|
||||
func (m *fileStore) get(k string) (*store.Record, error) {
|
||||
var value []byte
|
||||
|
||||
m.db.View(func(tx *bolt.Tx) error {
|
||||
// @todo this is still very experimental...
|
||||
b := tx.Bucket([]byte(m.options.Table))
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
value = b.Get([]byte(k))
|
||||
return nil
|
||||
})
|
||||
|
||||
if value == nil {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
|
||||
storedRecord := &record{}
|
||||
|
||||
if err := json.Unmarshal(value, storedRecord); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newRecord := &store.Record{}
|
||||
newRecord.Key = storedRecord.Key
|
||||
newRecord.Value = storedRecord.Value
|
||||
|
||||
if !storedRecord.ExpiresAt.IsZero() {
|
||||
if storedRecord.ExpiresAt.Before(time.Now()) {
|
||||
return nil, store.ErrNotFound
|
||||
}
|
||||
newRecord.Expiry = time.Until(storedRecord.ExpiresAt)
|
||||
}
|
||||
|
||||
return newRecord, nil
|
||||
}
|
||||
|
||||
func (m *fileStore) set(r *store.Record) error {
|
||||
// copy the incoming record and then
|
||||
// convert the expiry in to a hard timestamp
|
||||
item := &record{}
|
||||
item.Key = r.Key
|
||||
item.Value = r.Value
|
||||
if r.Expiry != 0 {
|
||||
item.ExpiresAt = time.Now().Add(r.Expiry)
|
||||
}
|
||||
|
||||
// marshal the data
|
||||
data, _ := json.Marshal(item)
|
||||
|
||||
return m.db.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte(m.options.Table))
|
||||
if b == nil {
|
||||
var err error
|
||||
b, err = tx.CreateBucketIfNotExists([]byte(m.options.Table))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return b.Put([]byte(r.Key), data)
|
||||
})
|
||||
}
|
||||
|
||||
func (m *fileStore) Init(opts ...store.Option) error {
|
||||
return m.init(opts...)
|
||||
}
|
||||
|
||||
func (m *fileStore) Delete(key string, opts ...store.DeleteOption) error {
|
||||
deleteOptions := store.DeleteOptions{}
|
||||
for _, o := range opts {
|
||||
o(&deleteOptions)
|
||||
}
|
||||
return m.delete(key)
|
||||
}
|
||||
|
||||
func (m *fileStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
||||
@@ -83,6 +244,7 @@ func (m *fileStore) Read(key string, opts ...store.ReadOption) ([]*store.Record,
|
||||
var keys []string
|
||||
|
||||
// Handle Prefix / suffix
|
||||
// TODO: do range scan here rather than listing all keys
|
||||
if readOpts.Prefix || readOpts.Suffix {
|
||||
var opts []store.ListOption
|
||||
if readOpts.Prefix {
|
||||
@@ -91,18 +253,22 @@ func (m *fileStore) Read(key string, opts ...store.ReadOption) ([]*store.Record,
|
||||
if readOpts.Suffix {
|
||||
opts = append(opts, store.ListSuffix(key))
|
||||
}
|
||||
|
||||
opts = append(opts, store.ListLimit(readOpts.Limit))
|
||||
opts = append(opts, store.ListOffset(readOpts.Offset))
|
||||
|
||||
k, err := m.List(opts...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "FileStore: Read couldn't List()")
|
||||
}
|
||||
|
||||
keys = k
|
||||
} else {
|
||||
keys = []string{key}
|
||||
}
|
||||
|
||||
var results []*store.Record
|
||||
|
||||
for _, k := range keys {
|
||||
r, err := m.get(k)
|
||||
if err != nil {
|
||||
@@ -110,59 +276,10 @@ func (m *fileStore) Read(key string, opts ...store.ReadOption) ([]*store.Record,
|
||||
}
|
||||
results = append(results, r)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (m *fileStore) get(k string) (*store.Record, error) {
|
||||
if len(m.options.Table) > 0 {
|
||||
k = m.options.Table + "/" + k
|
||||
}
|
||||
if len(m.options.Database) > 0 {
|
||||
k = m.options.Database + "/" + k
|
||||
}
|
||||
store, err := bolt.Open(m.fullFilePath, 0700, &bolt.Options{Timeout: 1 * time.Second})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer store.Close()
|
||||
err = store.Update(func(tx *bolt.Tx) error {
|
||||
_, err := tx.CreateBucketIfNotExists([]byte(m.options.Table))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var value []byte
|
||||
store.View(func(tx *bolt.Tx) error {
|
||||
// @todo this is still very experimental...
|
||||
bucket := tx.Bucket([]byte(m.options.Table))
|
||||
value = bucket.Get([]byte(k))
|
||||
return nil
|
||||
})
|
||||
if value == nil {
|
||||
return nil, micro_store.ErrNotFound
|
||||
}
|
||||
storedRecord := &internalRecord{}
|
||||
err = json.Unmarshal(value, storedRecord)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newRecord := µ_store.Record{}
|
||||
newRecord.Key = storedRecord.Key
|
||||
newRecord.Value = storedRecord.Value
|
||||
if !storedRecord.ExpiresAt.IsZero() {
|
||||
if storedRecord.ExpiresAt.Before(time.Now()) {
|
||||
return nil, micro_store.ErrNotFound
|
||||
}
|
||||
newRecord.Expiry = time.Until(storedRecord.ExpiresAt)
|
||||
}
|
||||
|
||||
return newRecord, nil
|
||||
}
|
||||
|
||||
func (m *fileStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
writeOpts := store.WriteOptions{}
|
||||
for _, o := range opts {
|
||||
@@ -182,87 +299,13 @@ func (m *fileStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
||||
if writeOpts.TTL != 0 {
|
||||
newRecord.Expiry = writeOpts.TTL
|
||||
}
|
||||
|
||||
return m.set(&newRecord)
|
||||
}
|
||||
|
||||
return m.set(r)
|
||||
}
|
||||
|
||||
func (m *fileStore) set(r *store.Record) error {
|
||||
key := r.Key
|
||||
if len(m.options.Table) > 0 {
|
||||
key = m.options.Table + "/" + key
|
||||
}
|
||||
if len(m.options.Database) > 0 {
|
||||
key = m.options.Database + "/" + key
|
||||
}
|
||||
|
||||
// copy the incoming record and then
|
||||
// convert the expiry in to a hard timestamp
|
||||
i := &internalRecord{}
|
||||
i.Key = r.Key
|
||||
i.Value = r.Value
|
||||
if r.Expiry != 0 {
|
||||
i.ExpiresAt = time.Now().Add(r.Expiry)
|
||||
}
|
||||
|
||||
iJSON, _ := json.Marshal(i)
|
||||
|
||||
store, err := bolt.Open(m.fullFilePath, 0700, &bolt.Options{Timeout: 1 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
return store.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte(m.options.Table))
|
||||
if b == nil {
|
||||
var err error
|
||||
b, err = tx.CreateBucketIfNotExists([]byte(m.options.Table))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return b.Put([]byte(key), iJSON)
|
||||
})
|
||||
}
|
||||
|
||||
func (m *fileStore) Delete(key string, opts ...store.DeleteOption) error {
|
||||
deleteOptions := store.DeleteOptions{}
|
||||
for _, o := range opts {
|
||||
o(&deleteOptions)
|
||||
}
|
||||
return m.delete(key)
|
||||
}
|
||||
|
||||
func (m *fileStore) delete(key string) error {
|
||||
if len(m.options.Table) > 0 {
|
||||
key = m.options.Table + "/" + key
|
||||
}
|
||||
if len(m.options.Database) > 0 {
|
||||
key = m.options.Database + "/" + key
|
||||
}
|
||||
store, err := bolt.Open(m.fullFilePath, 0700, &bolt.Options{Timeout: 1 * time.Second})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
return store.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte(m.options.Table))
|
||||
if b == nil {
|
||||
var err error
|
||||
b, err = tx.CreateBucketIfNotExists([]byte(m.options.Table))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err := b.Delete([]byte(key))
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (m *fileStore) deleteAll() error {
|
||||
return os.Remove(m.fullFilePath)
|
||||
}
|
||||
|
||||
func (m *fileStore) Options() store.Options {
|
||||
return m.options
|
||||
}
|
||||
@@ -273,6 +316,8 @@ func (m *fileStore) List(opts ...store.ListOption) ([]string, error) {
|
||||
for _, o := range opts {
|
||||
o(&listOptions)
|
||||
}
|
||||
|
||||
// TODO apply prefix/suffix in range query
|
||||
allKeys := m.list(listOptions.Limit, listOptions.Offset)
|
||||
|
||||
if len(listOptions.Prefix) > 0 {
|
||||
@@ -284,6 +329,7 @@ func (m *fileStore) List(opts ...store.ListOption) ([]string, error) {
|
||||
}
|
||||
allKeys = prefixKeys
|
||||
}
|
||||
|
||||
if len(listOptions.Suffix) > 0 {
|
||||
var suffixKeys []string
|
||||
for _, k := range allKeys {
|
||||
@@ -297,69 +343,6 @@ func (m *fileStore) List(opts ...store.ListOption) ([]string, error) {
|
||||
return allKeys, nil
|
||||
}
|
||||
|
||||
func (m *fileStore) list(limit, offset uint) []string {
|
||||
allItems := []string{}
|
||||
store, err := bolt.Open(m.fullFilePath, 0700, &bolt.Options{Timeout: 1 * time.Second})
|
||||
if err != nil {
|
||||
fmt.Println("Error creating file:", err)
|
||||
}
|
||||
defer store.Close()
|
||||
store.Update(func(tx *bolt.Tx) error {
|
||||
b := tx.Bucket([]byte(m.options.Table))
|
||||
if b == nil {
|
||||
var err error
|
||||
b, err = tx.CreateBucketIfNotExists([]byte(m.options.Table))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// @todo very inefficient
|
||||
if err := b.ForEach(func(k, v []byte) error {
|
||||
storedRecord := &internalRecord{}
|
||||
err := json.Unmarshal(v, storedRecord)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !storedRecord.ExpiresAt.IsZero() {
|
||||
if storedRecord.ExpiresAt.Before(time.Now()) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
allItems = append(allItems, string(k))
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
allKeys := make([]string, len(allItems))
|
||||
i := 0
|
||||
for _, k := range allItems {
|
||||
if len(m.options.Database) > 0 {
|
||||
k = strings.TrimPrefix(k, m.options.Database+"/")
|
||||
}
|
||||
if len(m.options.Table) > 0 {
|
||||
k = strings.TrimPrefix(k, m.options.Table+"/")
|
||||
}
|
||||
allKeys[i] = k
|
||||
i++
|
||||
}
|
||||
if limit != 0 || offset != 0 {
|
||||
sort.Slice(allKeys, func(i, j int) bool { return allKeys[i] < allKeys[j] })
|
||||
min := func(i, j uint) uint {
|
||||
if i < j {
|
||||
return i
|
||||
}
|
||||
return j
|
||||
}
|
||||
return allKeys[offset:min(limit, uint(len(allKeys)))]
|
||||
}
|
||||
return allKeys
|
||||
}
|
||||
|
||||
type internalRecord struct {
|
||||
Key string
|
||||
Value []byte
|
||||
ExpiresAt time.Time
|
||||
func (m *fileStore) String() string {
|
||||
return "file"
|
||||
}
|
||||
|
Reference in New Issue
Block a user