Use file store by default (as opposed to memory store) (#1498)

* Use file store by default (as opposed to memory store)

* Default table for file store
This commit is contained in:
Janos Dobronszki 2020-04-07 15:19:45 +02:00 committed by GitHub
parent 71538adfdc
commit aaee01b1a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -10,7 +10,7 @@ import (
gcli "github.com/micro/go-micro/v2/client/grpc" gcli "github.com/micro/go-micro/v2/client/grpc"
memTrace "github.com/micro/go-micro/v2/debug/trace/memory" memTrace "github.com/micro/go-micro/v2/debug/trace/memory"
gsrv "github.com/micro/go-micro/v2/server/grpc" gsrv "github.com/micro/go-micro/v2/server/grpc"
memStore "github.com/micro/go-micro/v2/store/memory" fileStore "github.com/micro/go-micro/v2/store/file"
) )
func init() { func init() {
@ -19,7 +19,7 @@ func init() {
// default server // default server
server.DefaultServer = gsrv.NewServer() server.DefaultServer = gsrv.NewServer()
// default store // default store
store.DefaultStore = memStore.NewStore() store.DefaultStore = fileStore.NewStore()
// set default trace // set default trace
trace.DefaultTracer = memTrace.NewTracer() trace.DefaultTracer = memTrace.NewTracer()
} }

View File

@ -17,6 +17,16 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
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 = os.TempDir()
)
// NewStore returns a memory store // NewStore returns a memory store
func NewStore(opts ...store.Option) store.Store { func NewStore(opts ...store.Option) store.Store {
s := &fileStore{ s := &fileStore{
@ -36,18 +46,17 @@ type fileStore struct {
} }
func (m *fileStore) Init(opts ...store.Option) error { func (m *fileStore) Init(opts ...store.Option) error {
// m.store.Flush()
for _, o := range opts { for _, o := range opts {
o(&m.options) o(&m.options)
} }
if m.options.Database == "" { if m.options.Database == "" {
m.options.Database = "default" m.options.Database = DefaultDatabase
} }
if m.options.Table == "" { if m.options.Table == "" {
// bbolt requires bucketname to not be empty // bbolt requires bucketname to not be empty
m.options.Table = "default" m.options.Table = DefaultTable
} }
dir := filepath.Join(os.TempDir(), "micro") dir := filepath.Join(DefaultDir, "micro")
fname := m.options.Database + ".db" fname := m.options.Database + ".db"
_ = os.Mkdir(dir, 0700) _ = os.Mkdir(dir, 0700)
m.dir = dir m.dir = dir