Skip to content

Commit

Permalink
feature: add Close func for Store backend
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wan <[email protected]>
  • Loading branch information
HusterWan committed Oct 31, 2018
1 parent 4f6e625 commit f16d564
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/meta/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions pkg/meta/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
5 changes: 5 additions & 0 deletions pkg/meta/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/meta/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
13 changes: 13 additions & 0 deletions pkg/meta/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meta

import (
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f16d564

Please sign in to comment.