From f16d5646f013810820a9a4bd74c050d9abe137f3 Mon Sep 17 00:00:00 2001 From: Michael Wan Date: Wed, 31 Oct 2018 01:38:42 -0400 Subject: [PATCH] feature: add Close func for Store backend Signed-off-by: Michael Wan --- pkg/meta/backend.go | 4 ++++ pkg/meta/boltdb.go | 6 ++++++ pkg/meta/local.go | 5 +++++ pkg/meta/store.go | 5 +++++ pkg/meta/store_test.go | 13 +++++++++++++ 5 files changed, 33 insertions(+) diff --git a/pkg/meta/backend.go b/pkg/meta/backend.go index 17265ae6be..84358eb4e5 100644 --- a/pkg/meta/backend.go +++ b/pkg/meta/backend.go @@ -24,6 +24,10 @@ type Backend interface { // Path returns the path with the specified key. Path(key string) string + + // Close releases all resources used by the store + // It does not make any changes to store. + Close() error } // Register registers a backend to be daemon's store. diff --git a/pkg/meta/boltdb.go b/pkg/meta/boltdb.go index 45049c24a6..d5f996f31b 100644 --- a/pkg/meta/boltdb.go +++ b/pkg/meta/boltdb.go @@ -160,3 +160,9 @@ func (b *bolt) List(bucket string) ([][]byte, error) { return values, err } + +// Close releases all database resources. +// All transactions must be closed before closing the database. +func (b *bolt) Close() error { + return b.db.Close() +} diff --git a/pkg/meta/local.go b/pkg/meta/local.go index f6cbd1dec6..727d6c3e5e 100644 --- a/pkg/meta/local.go +++ b/pkg/meta/local.go @@ -177,6 +177,11 @@ func (s *localStore) Keys(fileName string) ([]string, error) { return keys, nil } +// Close do nothing in local store +func (s *localStore) Close() error { + return nil +} + func mkdirIfNotExist(dir string) error { if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { diff --git a/pkg/meta/store.go b/pkg/meta/store.go index fb2b6afca7..13a034306b 100644 --- a/pkg/meta/store.go +++ b/pkg/meta/store.go @@ -270,3 +270,8 @@ func (s *Store) KeysWithPrefix(prefix string) ([]string, error) { func (s *Store) Path(key string) string { return s.backend.Path(key) } + +// Shutdown releases all resources used by the backend +func (s *Store) Shutdown() error { + return s.backend.Close() +} diff --git a/pkg/meta/store_test.go b/pkg/meta/store_test.go index c5ad138cde..7ce8d74ea4 100644 --- a/pkg/meta/store_test.go +++ b/pkg/meta/store_test.go @@ -1,6 +1,7 @@ package meta import ( + "fmt" "reflect" "testing" ) @@ -201,6 +202,18 @@ func TestBoltdbRemove(t *testing.T) { } } +func TestBoltdbClose(t *testing.T) { + if err := boltdbStore.Shutdown(); err != nil { + t.Fatal(err) + } + + // test List again, should occur error here + _, err := boltdbStore.List() + if err == nil { + t.Fatal(fmt.Errorf("still can visit the boltdb after execute close db action")) + } +} + type Demo4 struct { A int B string