move file store
This commit is contained in:
1
go.mod
1
go.mod
@@ -34,7 +34,6 @@ require (
|
|||||||
github.com/stretchr/testify v1.6.1
|
github.com/stretchr/testify v1.6.1
|
||||||
github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf
|
github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf
|
||||||
github.com/xanzy/go-gitlab v0.35.1
|
github.com/xanzy/go-gitlab v0.35.1
|
||||||
go.etcd.io/bbolt v1.3.5
|
|
||||||
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
|
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
|
||||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381
|
golang.org/x/net v0.0.0-20200707034311-ab3426394381
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
|
||||||
|
3
go.sum
3
go.sum
@@ -353,8 +353,6 @@ github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2
|
|||||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
||||||
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
|
github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
|
||||||
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
|
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
|
||||||
go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0=
|
|
||||||
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
|
||||||
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
||||||
go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
|
||||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||||
@@ -452,7 +450,6 @@ golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
20
store/cache/cache_test.go
vendored
20
store/cache/cache_test.go
vendored
@@ -6,21 +6,14 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/go-micro/v3/store"
|
"github.com/micro/go-micro/v3/store"
|
||||||
"github.com/micro/go-micro/v3/store/file"
|
"github.com/micro/go-micro/v3/store/memory"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func cleanup(db string, s store.Store) {
|
|
||||||
s.Close()
|
|
||||||
dir := filepath.Join(file.DefaultDir, db+"/")
|
|
||||||
os.RemoveAll(dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRead(t *testing.T) {
|
func TestRead(t *testing.T) {
|
||||||
cf := NewStore(file.NewStore())
|
cf := NewStore(memory.NewStore())
|
||||||
cf.Init()
|
cf.Init()
|
||||||
cfInt := cf.(*cache)
|
cfInt := cf.(*cache)
|
||||||
defer cleanup(file.DefaultDatabase, cf)
|
|
||||||
|
|
||||||
_, err := cf.Read("key1")
|
_, err := cf.Read("key1")
|
||||||
assert.Error(t, err, "Unexpected record")
|
assert.Error(t, err, "Unexpected record")
|
||||||
@@ -38,10 +31,9 @@ func TestRead(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWrite(t *testing.T) {
|
func TestWrite(t *testing.T) {
|
||||||
cf := NewStore(file.NewStore())
|
cf := NewStore(memory.NewStore())
|
||||||
cf.Init()
|
cf.Init()
|
||||||
cfInt := cf.(*cache)
|
cfInt := cf.(*cache)
|
||||||
defer cleanup(file.DefaultDatabase, cf)
|
|
||||||
|
|
||||||
cf.Write(&store.Record{
|
cf.Write(&store.Record{
|
||||||
Key: "key1",
|
Key: "key1",
|
||||||
@@ -55,10 +47,9 @@ func TestWrite(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
cf := NewStore(file.NewStore())
|
cf := NewStore(memory.NewStore())
|
||||||
cf.Init()
|
cf.Init()
|
||||||
cfInt := cf.(*cache)
|
cfInt := cf.(*cache)
|
||||||
defer cleanup(file.DefaultDatabase, cf)
|
|
||||||
|
|
||||||
cf.Write(&store.Record{
|
cf.Write(&store.Record{
|
||||||
Key: "key1",
|
Key: "key1",
|
||||||
@@ -78,10 +69,9 @@ func TestDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestList(t *testing.T) {
|
func TestList(t *testing.T) {
|
||||||
cf := NewStore(file.NewStore())
|
cf := NewStore(memory.NewStore())
|
||||||
cf.Init()
|
cf.Init()
|
||||||
cfInt := cf.(*cache)
|
cfInt := cf.(*cache)
|
||||||
defer cleanup(file.DefaultDatabase, cf)
|
|
||||||
|
|
||||||
keys, err := cf.List()
|
keys, err := cf.List()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
@@ -1,172 +0,0 @@
|
|||||||
package file
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/v3/store"
|
|
||||||
bolt "go.etcd.io/bbolt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewBlobStore returns a blob file store
|
|
||||||
func NewBlobStore(opts ...store.Option) (store.BlobStore, error) {
|
|
||||||
// parse the options
|
|
||||||
var options store.Options
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
|
|
||||||
var dir string
|
|
||||||
if options.Context != nil {
|
|
||||||
if d, ok := options.Context.Value(dirKey{}).(string); ok {
|
|
||||||
dir = d
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(dir) == 0 {
|
|
||||||
dir = DefaultDir
|
|
||||||
}
|
|
||||||
|
|
||||||
// ensure the parent directory exists
|
|
||||||
os.MkdirAll(dir, 0700)
|
|
||||||
|
|
||||||
return &blobStore{dir}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type blobStore struct {
|
|
||||||
dir string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *blobStore) db() (*bolt.DB, error) {
|
|
||||||
dbPath := filepath.Join(b.dir, "blob.db")
|
|
||||||
return bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *blobStore) Read(key string, opts ...store.BlobOption) (io.Reader, error) {
|
|
||||||
// validate the key
|
|
||||||
if len(key) == 0 {
|
|
||||||
return nil, store.ErrMissingKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse the options
|
|
||||||
var options store.BlobOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = "micro"
|
|
||||||
}
|
|
||||||
|
|
||||||
// open a connection to the database
|
|
||||||
db, err := b.db()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// execute the transaction
|
|
||||||
var value []byte
|
|
||||||
readValue := func(tx *bolt.Tx) error {
|
|
||||||
// check for the namespaces bucket
|
|
||||||
bucket := tx.Bucket([]byte(options.Namespace))
|
|
||||||
if bucket == nil {
|
|
||||||
return store.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
// look for the blob within the bucket
|
|
||||||
res := bucket.Get([]byte(key))
|
|
||||||
if res == nil {
|
|
||||||
return store.ErrNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
// the res object is only valid for the duration of the blot transaction, see:
|
|
||||||
// https://github.com/golang/go/issues/33047
|
|
||||||
value = make([]byte, len(res))
|
|
||||||
copy(value, res)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if err := db.View(readValue); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// return the blob
|
|
||||||
return bytes.NewBuffer(value), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *blobStore) Write(key string, blob io.Reader, opts ...store.BlobOption) error {
|
|
||||||
// validate the key
|
|
||||||
if len(key) == 0 {
|
|
||||||
return store.ErrMissingKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse the options
|
|
||||||
var options store.BlobOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = "micro"
|
|
||||||
}
|
|
||||||
|
|
||||||
// open a connection to the database
|
|
||||||
db, err := b.db()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// execute the transaction
|
|
||||||
return db.Update(func(tx *bolt.Tx) error {
|
|
||||||
// create the bucket
|
|
||||||
bucket, err := tx.CreateBucketIfNotExists([]byte(options.Namespace))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// write to the bucket
|
|
||||||
value, err := ioutil.ReadAll(blob)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return bucket.Put([]byte(key), value)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *blobStore) Delete(key string, opts ...store.BlobOption) error {
|
|
||||||
// validate the key
|
|
||||||
if len(key) == 0 {
|
|
||||||
return store.ErrMissingKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse the options
|
|
||||||
var options store.BlobOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&options)
|
|
||||||
}
|
|
||||||
if len(options.Namespace) == 0 {
|
|
||||||
options.Namespace = "micro"
|
|
||||||
}
|
|
||||||
|
|
||||||
// open a connection to the database
|
|
||||||
db, err := b.db()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
// execute the transaction
|
|
||||||
return db.Update(func(tx *bolt.Tx) error {
|
|
||||||
// check for the namespaces bucket
|
|
||||||
bucket := tx.Bucket([]byte(options.Namespace))
|
|
||||||
if bucket == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return bucket.Delete([]byte(key))
|
|
||||||
})
|
|
||||||
}
|
|
@@ -1,81 +0,0 @@
|
|||||||
package file
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io/ioutil"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/v3/store"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestBlobStore(t *testing.T) {
|
|
||||||
blob, err := NewBlobStore()
|
|
||||||
assert.NotNilf(t, blob, "Blob should not be nil")
|
|
||||||
assert.Nilf(t, err, "Error should be nil")
|
|
||||||
|
|
||||||
t.Run("ReadMissingKey", func(t *testing.T) {
|
|
||||||
res, err := blob.Read("")
|
|
||||||
assert.Equal(t, store.ErrMissingKey, err, "Error should be missing key")
|
|
||||||
assert.Nil(t, res, "Result should be nil")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ReadNotFound", func(t *testing.T) {
|
|
||||||
res, err := blob.Read("foo")
|
|
||||||
assert.Equal(t, store.ErrNotFound, err, "Error should be not found")
|
|
||||||
assert.Nil(t, res, "Result should be nil")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("WriteMissingKey", func(t *testing.T) {
|
|
||||||
buf := bytes.NewBuffer([]byte("HelloWorld"))
|
|
||||||
err := blob.Write("", buf)
|
|
||||||
assert.Equal(t, store.ErrMissingKey, err, "Error should be missing key")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("WriteValid", func(t *testing.T) {
|
|
||||||
buf := bytes.NewBuffer([]byte("world"))
|
|
||||||
err := blob.Write("hello", buf)
|
|
||||||
assert.Nilf(t, err, "Error should be nil")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ReadValid", func(t *testing.T) {
|
|
||||||
val, err := blob.Read("hello")
|
|
||||||
bytes, _ := ioutil.ReadAll(val)
|
|
||||||
assert.Nilf(t, err, "Error should be nil")
|
|
||||||
assert.Equal(t, string(bytes), "world", "Value should be world")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ReadIncorrectNamespace", func(t *testing.T) {
|
|
||||||
val, err := blob.Read("hello", store.BlobNamespace("bar"))
|
|
||||||
assert.Equal(t, store.ErrNotFound, err, "Error should be not found")
|
|
||||||
assert.Nil(t, val, "Value should be nil")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ReadCorrectNamespace", func(t *testing.T) {
|
|
||||||
val, err := blob.Read("hello", store.BlobNamespace("micro"))
|
|
||||||
bytes, _ := ioutil.ReadAll(val)
|
|
||||||
assert.Nil(t, err, "Error should be nil")
|
|
||||||
assert.Equal(t, string(bytes), "world", "Value should be world")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("DeleteIncorrectNamespace", func(t *testing.T) {
|
|
||||||
err := blob.Delete("hello", store.BlobNamespace("bar"))
|
|
||||||
assert.Nil(t, err, "Error should be nil")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("DeleteCorrectNamespaceIncorrectKey", func(t *testing.T) {
|
|
||||||
err := blob.Delete("world", store.BlobNamespace("micro"))
|
|
||||||
assert.Nil(t, err, "Error should be nil")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("DeleteCorrectNamespace", func(t *testing.T) {
|
|
||||||
err := blob.Delete("hello", store.BlobNamespace("micro"))
|
|
||||||
assert.Nil(t, err, "Error should be nil")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("ReadDeletedKey", func(t *testing.T) {
|
|
||||||
res, err := blob.Read("hello", store.BlobNamespace("micro"))
|
|
||||||
assert.Equal(t, store.ErrNotFound, err, "Error should be not found")
|
|
||||||
assert.Nil(t, res, "Result should be nil")
|
|
||||||
})
|
|
||||||
}
|
|
@@ -1,383 +0,0 @@
|
|||||||
// Package local is a file system backed store
|
|
||||||
package file
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/v3/store"
|
|
||||||
bolt "go.etcd.io/bbolt"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
// DefaultDatabase is the namespace that the bbolt store
|
|
||||||
// will use if no namespace is provided.
|
|
||||||
DefaultDatabase = "micro"
|
|
||||||
// DefaultTable when none is specified
|
|
||||||
DefaultTable = "micro"
|
|
||||||
// DefaultDir is the default directory for bbolt files
|
|
||||||
DefaultDir = filepath.Join(os.TempDir(), "micro", "store")
|
|
||||||
|
|
||||||
// bucket used for data storage
|
|
||||||
dataBucket = "data"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewStore returns a file store
|
|
||||||
func NewStore(opts ...store.Option) store.Store {
|
|
||||||
s := &fileStore{}
|
|
||||||
s.init(opts...)
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
type fileStore struct {
|
|
||||||
options store.Options
|
|
||||||
dir string
|
|
||||||
}
|
|
||||||
|
|
||||||
type fileHandle struct {
|
|
||||||
key string
|
|
||||||
db *bolt.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
// record stored by us
|
|
||||||
type record struct {
|
|
||||||
Key string
|
|
||||||
Value []byte
|
|
||||||
Metadata map[string]interface{}
|
|
||||||
ExpiresAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func key(database, table string) string {
|
|
||||||
return database + ":" + table
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) delete(db *bolt.DB, key string) error {
|
|
||||||
return db.Update(func(tx *bolt.Tx) error {
|
|
||||||
b := tx.Bucket([]byte(dataBucket))
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
dir := m.getDir(m.options.Database)
|
|
||||||
os.MkdirAll(dir, 0700)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// getDir returns the directory which should contain the files for a databases
|
|
||||||
func (m *fileStore) getDir(db string) string {
|
|
||||||
// get the directory option from the context
|
|
||||||
var directory string
|
|
||||||
if m.options.Context != nil {
|
|
||||||
fd, ok := m.options.Context.Value(dirKey{}).(string)
|
|
||||||
if ok {
|
|
||||||
directory = fd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(directory) == 0 {
|
|
||||||
directory = DefaultDir
|
|
||||||
}
|
|
||||||
|
|
||||||
// construct the directory, e.g. /tmp/micro
|
|
||||||
return filepath.Join(directory, db)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileStore) getDB(database, table string) (*bolt.DB, error) {
|
|
||||||
if len(database) == 0 {
|
|
||||||
database = f.options.Database
|
|
||||||
}
|
|
||||||
if len(table) == 0 {
|
|
||||||
table = f.options.Table
|
|
||||||
}
|
|
||||||
|
|
||||||
// create a directory /tmp/micro
|
|
||||||
dir := f.getDir(database)
|
|
||||||
// create the database handle
|
|
||||||
fname := table + ".db"
|
|
||||||
// make the dir
|
|
||||||
os.MkdirAll(dir, 0700)
|
|
||||||
// database path
|
|
||||||
dbPath := filepath.Join(dir, fname)
|
|
||||||
|
|
||||||
// create new db handle
|
|
||||||
// Bolt DB only allows one process to open the file R/W so make sure we're doing this under a lock
|
|
||||||
return bolt.Open(dbPath, 0700, &bolt.Options{Timeout: 5 * time.Second})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) list(db *bolt.DB, limit, offset uint, prefix, suffix string) []string {
|
|
||||||
|
|
||||||
var keys []string
|
|
||||||
|
|
||||||
db.View(func(tx *bolt.Tx) error {
|
|
||||||
b := tx.Bucket([]byte(dataBucket))
|
|
||||||
// nothing to read
|
|
||||||
if b == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
c := b.Cursor()
|
|
||||||
var k, v []byte
|
|
||||||
var cont func(k []byte) bool
|
|
||||||
|
|
||||||
if prefix != "" {
|
|
||||||
// for prefix we can speed up the search, not for suffix though :(
|
|
||||||
k, v = c.Seek([]byte(prefix))
|
|
||||||
cont = func(k []byte) bool {
|
|
||||||
return bytes.HasPrefix(k, []byte(prefix))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
k, v = c.First()
|
|
||||||
cont = func(k []byte) bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for ; k != nil && cont(k); k, v = c.Next() {
|
|
||||||
storedRecord := &record{}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(v, storedRecord); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !storedRecord.ExpiresAt.IsZero() {
|
|
||||||
if storedRecord.ExpiresAt.Before(time.Now()) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if suffix != "" && !bytes.HasSuffix(k, []byte(suffix)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if offset > 0 {
|
|
||||||
offset--
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
keys = append(keys, string(k))
|
|
||||||
// this check still works if no limit was passed to begin with, you'll just end up with large -ve value
|
|
||||||
if limit == 1 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
limit--
|
|
||||||
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) get(db *bolt.DB, k string) (*store.Record, error) {
|
|
||||||
var value []byte
|
|
||||||
|
|
||||||
db.View(func(tx *bolt.Tx) error {
|
|
||||||
// @todo this is still very experimental...
|
|
||||||
b := tx.Bucket([]byte(dataBucket))
|
|
||||||
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
|
|
||||||
newRecord.Metadata = make(map[string]interface{})
|
|
||||||
|
|
||||||
for k, v := range storedRecord.Metadata {
|
|
||||||
newRecord.Metadata[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
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(db *bolt.DB, 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
|
|
||||||
item.Metadata = make(map[string]interface{})
|
|
||||||
|
|
||||||
if r.Expiry != 0 {
|
|
||||||
item.ExpiresAt = time.Now().Add(r.Expiry)
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range r.Metadata {
|
|
||||||
item.Metadata[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
// marshal the data
|
|
||||||
data, _ := json.Marshal(item)
|
|
||||||
|
|
||||||
return db.Update(func(tx *bolt.Tx) error {
|
|
||||||
b := tx.Bucket([]byte(dataBucket))
|
|
||||||
if b == nil {
|
|
||||||
var err error
|
|
||||||
b, err = tx.CreateBucketIfNotExists([]byte(dataBucket))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return b.Put([]byte(r.Key), data)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileStore) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fileStore) Init(opts ...store.Option) error {
|
|
||||||
return f.init(opts...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) Delete(key string, opts ...store.DeleteOption) error {
|
|
||||||
var deleteOptions store.DeleteOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&deleteOptions)
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := m.getDB(deleteOptions.Database, deleteOptions.Table)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
return m.delete(db, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
|
||||||
var readOpts store.ReadOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&readOpts)
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := m.getDB(readOpts.Database, readOpts.Table)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
var keys []string
|
|
||||||
|
|
||||||
// Handle Prefix / suffix
|
|
||||||
if readOpts.Prefix || readOpts.Suffix {
|
|
||||||
prefix := ""
|
|
||||||
if readOpts.Prefix {
|
|
||||||
prefix = key
|
|
||||||
}
|
|
||||||
suffix := ""
|
|
||||||
if readOpts.Suffix {
|
|
||||||
suffix = key
|
|
||||||
}
|
|
||||||
// list the keys
|
|
||||||
keys = m.list(db, readOpts.Limit, readOpts.Offset, prefix, suffix)
|
|
||||||
} else {
|
|
||||||
keys = []string{key}
|
|
||||||
}
|
|
||||||
|
|
||||||
var results []*store.Record
|
|
||||||
|
|
||||||
for _, k := range keys {
|
|
||||||
r, err := m.get(db, k)
|
|
||||||
if err != nil {
|
|
||||||
return results, err
|
|
||||||
}
|
|
||||||
results = append(results, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
return results, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) Write(r *store.Record, opts ...store.WriteOption) error {
|
|
||||||
var writeOpts store.WriteOptions
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&writeOpts)
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := m.getDB(writeOpts.Database, writeOpts.Table)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
if len(opts) > 0 {
|
|
||||||
// Copy the record before applying options, or the incoming record will be mutated
|
|
||||||
newRecord := store.Record{}
|
|
||||||
newRecord.Key = r.Key
|
|
||||||
newRecord.Value = r.Value
|
|
||||||
newRecord.Metadata = make(map[string]interface{})
|
|
||||||
newRecord.Expiry = r.Expiry
|
|
||||||
|
|
||||||
for k, v := range r.Metadata {
|
|
||||||
newRecord.Metadata[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
return m.set(db, &newRecord)
|
|
||||||
}
|
|
||||||
|
|
||||||
return m.set(db, r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) Options() store.Options {
|
|
||||||
return m.options
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) List(opts ...store.ListOption) ([]string, error) {
|
|
||||||
var listOptions store.ListOptions
|
|
||||||
|
|
||||||
for _, o := range opts {
|
|
||||||
o(&listOptions)
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := m.getDB(listOptions.Database, listOptions.Table)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
allKeys := m.list(db, listOptions.Limit, listOptions.Offset, listOptions.Prefix, listOptions.Suffix)
|
|
||||||
|
|
||||||
return allKeys, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *fileStore) String() string {
|
|
||||||
return "file"
|
|
||||||
}
|
|
@@ -1,16 +0,0 @@
|
|||||||
package file
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/v3/store"
|
|
||||||
)
|
|
||||||
|
|
||||||
type dirKey struct{}
|
|
||||||
|
|
||||||
// WithDir sets the directory to store the files in
|
|
||||||
func WithDir(dir string) store.Option {
|
|
||||||
return func(o *store.Options) {
|
|
||||||
o.Context = context.WithValue(o.Context, dirKey{}, dir)
|
|
||||||
}
|
|
||||||
}
|
|
@@ -14,24 +14,9 @@ import (
|
|||||||
"github.com/kr/pretty"
|
"github.com/kr/pretty"
|
||||||
"github.com/micro/go-micro/v3/store"
|
"github.com/micro/go-micro/v3/store"
|
||||||
"github.com/micro/go-micro/v3/store/cache"
|
"github.com/micro/go-micro/v3/store/cache"
|
||||||
"github.com/micro/go-micro/v3/store/file"
|
|
||||||
"github.com/micro/go-micro/v3/store/memory"
|
"github.com/micro/go-micro/v3/store/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
func fileStoreCleanup(db string, s store.Store) {
|
|
||||||
s.Close()
|
|
||||||
dir := filepath.Join(file.DefaultDir, db+"/")
|
|
||||||
os.RemoveAll(dir)
|
|
||||||
}
|
|
||||||
|
|
||||||
func cockroachStoreCleanup(db string, s store.Store) {
|
|
||||||
keys, _ := s.List()
|
|
||||||
for _, k := range keys {
|
|
||||||
s.Delete(k)
|
|
||||||
}
|
|
||||||
s.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func memoryCleanup(db string, s store.Store) {
|
func memoryCleanup(db string, s store.Store) {
|
||||||
s.Close()
|
s.Close()
|
||||||
}
|
}
|
||||||
@@ -46,13 +31,11 @@ func TestStoreReInit(t *testing.T) {
|
|||||||
s store.Store
|
s store.Store
|
||||||
cleanup func(db string, s store.Store)
|
cleanup func(db string, s store.Store)
|
||||||
}{
|
}{
|
||||||
{name: "file", s: file.NewStore(store.Table("aaa")), cleanup: fileStoreCleanup},
|
|
||||||
{name: "memory", s: memory.NewStore(store.Table("aaa")), cleanup: memoryCleanup},
|
{name: "memory", s: memory.NewStore(store.Table("aaa")), cleanup: memoryCleanup},
|
||||||
{name: "cache", s: cache.NewStore(memory.NewStore(store.Table("aaa"))), cleanup: cacheCleanup},
|
{name: "cache", s: cache.NewStore(memory.NewStore(store.Table("aaa"))), cleanup: cacheCleanup},
|
||||||
}
|
}
|
||||||
for _, tc := range tcs {
|
for _, tc := range tcs {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
defer tc.cleanup(file.DefaultDatabase, tc.s)
|
|
||||||
tc.s.Init(store.Table("bbb"))
|
tc.s.Init(store.Table("bbb"))
|
||||||
if tc.s.Options().Table != "bbb" {
|
if tc.s.Options().Table != "bbb" {
|
||||||
t.Error("Init didn't reinitialise the store")
|
t.Error("Init didn't reinitialise the store")
|
||||||
@@ -67,13 +50,11 @@ func TestStoreBasic(t *testing.T) {
|
|||||||
s store.Store
|
s store.Store
|
||||||
cleanup func(db string, s store.Store)
|
cleanup func(db string, s store.Store)
|
||||||
}{
|
}{
|
||||||
{name: "file", s: file.NewStore(), cleanup: fileStoreCleanup},
|
|
||||||
{name: "memory", s: memory.NewStore(), cleanup: memoryCleanup},
|
{name: "memory", s: memory.NewStore(), cleanup: memoryCleanup},
|
||||||
{name: "cache", s: cache.NewStore(memory.NewStore()), cleanup: cacheCleanup},
|
{name: "cache", s: cache.NewStore(memory.NewStore()), cleanup: cacheCleanup},
|
||||||
}
|
}
|
||||||
for _, tc := range tcs {
|
for _, tc := range tcs {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
defer tc.cleanup(file.DefaultDatabase, tc.s)
|
|
||||||
runStoreTest(tc.s, t)
|
runStoreTest(tc.s, t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -86,13 +67,11 @@ func TestStoreTable(t *testing.T) {
|
|||||||
s store.Store
|
s store.Store
|
||||||
cleanup func(db string, s store.Store)
|
cleanup func(db string, s store.Store)
|
||||||
}{
|
}{
|
||||||
{name: "file", s: file.NewStore(store.Table("testTable")), cleanup: fileStoreCleanup},
|
|
||||||
{name: "memory", s: memory.NewStore(store.Table("testTable")), cleanup: memoryCleanup},
|
{name: "memory", s: memory.NewStore(store.Table("testTable")), cleanup: memoryCleanup},
|
||||||
{name: "cache", s: cache.NewStore(memory.NewStore(store.Table("testTable"))), cleanup: cacheCleanup},
|
{name: "cache", s: cache.NewStore(memory.NewStore(store.Table("testTable"))), cleanup: cacheCleanup},
|
||||||
}
|
}
|
||||||
for _, tc := range tcs {
|
for _, tc := range tcs {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
defer tc.cleanup(file.DefaultDatabase, tc.s)
|
|
||||||
runStoreTest(tc.s, t)
|
runStoreTest(tc.s, t)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -104,7 +83,6 @@ func TestStoreDatabase(t *testing.T) {
|
|||||||
s store.Store
|
s store.Store
|
||||||
cleanup func(db string, s store.Store)
|
cleanup func(db string, s store.Store)
|
||||||
}{
|
}{
|
||||||
{name: "file", s: file.NewStore(store.Database("testdb")), cleanup: fileStoreCleanup},
|
|
||||||
{name: "memory", s: memory.NewStore(store.Database("testdb")), cleanup: memoryCleanup},
|
{name: "memory", s: memory.NewStore(store.Database("testdb")), cleanup: memoryCleanup},
|
||||||
{name: "cache", s: cache.NewStore(memory.NewStore(store.Database("testdb"))), cleanup: cacheCleanup},
|
{name: "cache", s: cache.NewStore(memory.NewStore(store.Database("testdb"))), cleanup: cacheCleanup},
|
||||||
}
|
}
|
||||||
@@ -122,7 +100,6 @@ func TestStoreDatabaseTable(t *testing.T) {
|
|||||||
s store.Store
|
s store.Store
|
||||||
cleanup func(db string, s store.Store)
|
cleanup func(db string, s store.Store)
|
||||||
}{
|
}{
|
||||||
{name: "file", s: file.NewStore(store.Database("testdb"), store.Table("testTable")), cleanup: fileStoreCleanup},
|
|
||||||
{name: "memory", s: memory.NewStore(store.Database("testdb"), store.Table("testTable")), cleanup: memoryCleanup},
|
{name: "memory", s: memory.NewStore(store.Database("testdb"), store.Table("testTable")), cleanup: memoryCleanup},
|
||||||
{name: "cache", s: cache.NewStore(memory.NewStore(store.Database("testdb"), store.Table("testTable"))), cleanup: cacheCleanup},
|
{name: "cache", s: cache.NewStore(memory.NewStore(store.Database("testdb"), store.Table("testTable"))), cleanup: cacheCleanup},
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user