From 4b0e27413e14190a358e66e38061f01a95e59c3b Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 8 Apr 2020 09:51:10 +0100 Subject: [PATCH] add Store Close method (#1500) * add Store Close method * Update sync store build failure --- store/cache/cache.go | 4 ++++ store/cloudflare/cloudflare.go | 4 ++++ store/cockroach/cockroach.go | 7 +++++++ store/etcd/etcd.go | 4 ++++ store/file/file.go | 13 ++++++++++--- store/file/file_test.go | 15 ++++++++------- store/memory/memory.go | 5 +++++ store/noop.go | 4 ++++ store/service/service.go | 4 ++++ store/store.go | 2 ++ sync/store/cache.go | 4 ++++ 11 files changed, 56 insertions(+), 10 deletions(-) diff --git a/store/cache/cache.go b/store/cache/cache.go index a52c2bb3..96790b7a 100644 --- a/store/cache/cache.go +++ b/store/cache/cache.go @@ -23,6 +23,10 @@ func NewCache(stores ...store.Store) store.Store { return c } +func (c *cache) Close() error { + return nil +} + func (c *cache) Init(...store.Option) error { if len(c.stores) < 2 { return errors.New("cache requires at least 2 stores") diff --git a/store/cloudflare/cloudflare.go b/store/cloudflare/cloudflare.go index c9099f1b..c7fa5541 100644 --- a/store/cloudflare/cloudflare.go +++ b/store/cloudflare/cloudflare.go @@ -101,6 +101,10 @@ func validateOptions(account, token, namespace string) { } } +func (w *workersKV) Close() error { + return nil +} + func (w *workersKV) Init(opts ...store.Option) error { for _, o := range opts { o(&w.options) diff --git a/store/cockroach/cockroach.go b/store/cockroach/cockroach.go index 5f242810..29d78743 100644 --- a/store/cockroach/cockroach.go +++ b/store/cockroach/cockroach.go @@ -36,6 +36,13 @@ type sqlStore struct { options store.Options } +func (s *sqlStore) Close() error { + if s.db != nil { + return s.db.Close() + } + return nil +} + func (s *sqlStore) Init(opts ...store.Option) error { for _, o := range opts { o(&s.options) diff --git a/store/etcd/etcd.go b/store/etcd/etcd.go index a51fd425..d4d9cf97 100644 --- a/store/etcd/etcd.go +++ b/store/etcd/etcd.go @@ -32,6 +32,10 @@ func NewStore(opts ...store.Option) store.Store { return e } +func (e *etcdStore) Close() error { + return e.client.Close() +} + func (e *etcdStore) Init(opts ...store.Option) error { for _, o := range opts { o(&e.options) diff --git a/store/file/file.go b/store/file/file.go index 2f924380..2a62207d 100644 --- a/store/file/file.go +++ b/store/file/file.go @@ -72,13 +72,13 @@ func (m *fileStore) init(opts ...store.Option) error { } // create a directory /tmp/micro - dir := filepath.Join(DefaultDir, "micro") + dir := filepath.Join(DefaultDir, m.options.Database) // create the database handle - fname := m.options.Database + ".db" + fname := m.options.Table + ".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) + os.MkdirAll(dir, 0700) m.dir = dir m.fileName = fname @@ -223,6 +223,13 @@ func (m *fileStore) set(r *store.Record) error { }) } +func (m *fileStore) Close() error { + if m.db != nil { + return m.db.Close() + } + return nil +} + func (m *fileStore) Init(opts ...store.Option) error { return m.init(opts...) } diff --git a/store/file/file_test.go b/store/file/file_test.go index 86c533fc..b3466b9a 100644 --- a/store/file/file_test.go +++ b/store/file/file_test.go @@ -13,14 +13,15 @@ import ( "github.com/micro/go-micro/v2/store" ) -func cleanup() { - dir := filepath.Join(DefaultDir, "micro/") +func cleanup(db string, s store.Store) { + s.Close() + dir := filepath.Join(DefaultDir, db + "/") os.RemoveAll(dir) } func TestFileStoreReInit(t *testing.T) { - defer cleanup() s := NewStore(store.Table("aaa")) + defer cleanup(DefaultDatabase, s) s.Init(store.Table("bbb")) if s.Options().Table != "bbb" { t.Error("Init didn't reinitialise the store") @@ -28,26 +29,26 @@ func TestFileStoreReInit(t *testing.T) { } func TestFileStoreBasic(t *testing.T) { - defer cleanup() s := NewStore() + defer cleanup(DefaultDatabase, s) fileTest(s, t) } func TestFileStoreTable(t *testing.T) { - defer cleanup() s := NewStore(store.Table("testTable")) + defer cleanup(DefaultDatabase, s) fileTest(s, t) } func TestFileStoreDatabase(t *testing.T) { - defer cleanup() s := NewStore(store.Database("testdb")) + defer cleanup("testdb", s) fileTest(s, t) } func TestFileStoreDatabaseTable(t *testing.T) { - defer cleanup() s := NewStore(store.Table("testTable"), store.Database("testdb")) + defer cleanup("testdb", s) fileTest(s, t) } diff --git a/store/memory/memory.go b/store/memory/memory.go index e2f6d15e..97d19072 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -30,6 +30,11 @@ type memoryStore struct { store *cache.Cache } +func (m *memoryStore) Close() error { + m.store.Flush() + return nil +} + func (m *memoryStore) Init(opts ...store.Option) error { m.store.Flush() for _, o := range opts { diff --git a/store/noop.go b/store/noop.go index 0c6ebb4b..25d4850a 100644 --- a/store/noop.go +++ b/store/noop.go @@ -29,3 +29,7 @@ func (n *noopStore) Delete(key string, opts ...DeleteOption) error { func (n *noopStore) List(opts ...ListOption) ([]string, error) { return []string{}, nil } + +func (n *noopStore) Close() error { + return nil +} diff --git a/store/service/service.go b/store/service/service.go index f55bbd04..f3829e9a 100644 --- a/store/service/service.go +++ b/store/service/service.go @@ -32,6 +32,10 @@ type serviceStore struct { Client pb.StoreService } +func (s *serviceStore) Close() error { + return nil +} + func (s *serviceStore) Init(opts ...store.Option) error { for _, o := range opts { o(&s.options) diff --git a/store/store.go b/store/store.go index 057ad4ea..d073f4e1 100644 --- a/store/store.go +++ b/store/store.go @@ -28,6 +28,8 @@ type Store interface { Delete(key string, opts ...DeleteOption) error // List returns any keys that match, or an empty list with no error if none matched. List(opts ...ListOption) ([]string, error) + // Close the store + Close() error // String returns the name of the implementation. String() string } diff --git a/sync/store/cache.go b/sync/store/cache.go index 3c13e48a..2385e961 100644 --- a/sync/store/cache.go +++ b/sync/store/cache.go @@ -41,6 +41,10 @@ func NewCache(opts ...Option) Cache { return c } +func (c *cache) Close() error { + return nil +} + // Init initialises the storeOptions func (c *cache) Init(opts ...store.Option) error { for _, o := range opts {