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 73d6e7c
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 10 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()
}
149 changes: 139 additions & 10 deletions pkg/meta/store_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package meta

import (
"fmt"
"os"
"path"
"reflect"
"testing"

"github.com/alibaba/pouch/pkg/utils"
)

type Demo struct {
Expand Down Expand Up @@ -117,20 +122,41 @@ func (d *Demo3) Key() string {
return d.B
}

var boltdbStore *Store
func initBoltDBStore(dbFile string) (*Store, error) {
boltdbCfg := Config{
Driver: "boltdb",
BaseDir: dbFile,
Buckets: []Bucket{
{"boltdb", reflect.TypeOf(Demo3{})},
},
}

var boltdbCfg = Config{
Driver: "boltdb",
BaseDir: "/tmp/bolt.db",
Buckets: []Bucket{
{"boltdb", reflect.TypeOf(Demo3{})},
},
return NewStore(boltdbCfg)
}

func ensureFileNotExist(file string) error {
_, err := os.Stat(file)
if err == nil {
os.Remove(file)
return nil
}

if !os.IsNotExist(err) {
return err
}

return nil
}

func TestBoltdbPut(t *testing.T) {
// initialize
var err error
boltdbStore, err = NewStore(boltdbCfg)
dbFile := path.Join("/tmp", utils.RandString(8, "TestBoltdbPut", ""))
if err := ensureFileNotExist(dbFile); err != nil {
t.Fatal(err)
}
defer ensureFileNotExist(dbFile)

boltdbStore, err := initBoltDBStore(dbFile)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -159,6 +185,26 @@ func TestBoltdbPut(t *testing.T) {
}

func TestBoltdbGet(t *testing.T) {
// initialize
dbFile := path.Join("/tmp", utils.RandString(8, "TestBoltdbGet", ""))
if err := ensureFileNotExist(dbFile); err != nil {
t.Fatal(err)
}
defer ensureFileNotExist(dbFile)

boltdbStore, err := initBoltDBStore(dbFile)
if err != nil {
t.Fatal(err)
}

// first put 1
if err := boltdbStore.Put(&Demo3{
A: 1,
B: "key",
}); err != nil {
t.Fatal(err)
}

obj, err := boltdbStore.Get("key")
if err != nil {
t.Fatal(err)
Expand All @@ -173,6 +219,26 @@ func TestBoltdbGet(t *testing.T) {
}

func TestBoltdbFetch(t *testing.T) {
// initialize
dbFile := path.Join("/tmp", utils.RandString(8, "TestBoltdbFetch", ""))
if err := ensureFileNotExist(dbFile); err != nil {
t.Fatal(err)
}
defer ensureFileNotExist(dbFile)

boltdbStore, err := initBoltDBStore(dbFile)
if err != nil {
t.Fatal(err)
}

// first put 2
if err := boltdbStore.Put(&Demo3{
A: 2,
B: "key2",
}); err != nil {
t.Fatal(err)
}

d := &Demo3{}
d.B = "key2"

Expand All @@ -186,21 +252,84 @@ func TestBoltdbFetch(t *testing.T) {
}

func TestBoltdbList(t *testing.T) {
// initialize
dbFile := path.Join("/tmp", utils.RandString(8, "TestBoltdbList", ""))
if err := ensureFileNotExist(dbFile); err != nil {
t.Fatal(err)
}
defer ensureFileNotExist(dbFile)

boltdbStore, err := initBoltDBStore(dbFile)
if err != nil {
t.Fatal(err)
}

// first put 2
if err := boltdbStore.Put(&Demo3{
A: 2,
B: "key2",
}); err != nil {
t.Fatal(err)
}

objs, err := boltdbStore.List()
if err != nil {
t.Fatal(err)
}
if len(objs) != 2 {
if len(objs) != 1 {
t.Fatalf("failed to list")
}
}

func TestBoltdbRemove(t *testing.T) {
// initialize
dbFile := path.Join("/tmp", utils.RandString(8, "TestBoltdbRemove", ""))
if err := ensureFileNotExist(dbFile); err != nil {
t.Fatal(err)
}
defer ensureFileNotExist(dbFile)

boltdbStore, err := initBoltDBStore(dbFile)
if err != nil {
t.Fatal(err)
}

// first put 1
if err := boltdbStore.Put(&Demo3{
A: 1,
B: "key",
}); err != nil {
t.Fatal(err)
}

if err := boltdbStore.Remove("key"); err != nil {
t.Fatal(err)
}
}

func TestBoltdbClose(t *testing.T) {
// initialize
dbFile := path.Join("/tmp", utils.RandString(8, "TestBoltdbClose", ""))
if err := ensureFileNotExist(dbFile); err != nil {
t.Fatal(err)
}
defer ensureFileNotExist(dbFile)

boltdbStore, err := initBoltDBStore(dbFile)
if err != nil {
t.Fatal(err)
}
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 73d6e7c

Please sign in to comment.