1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

use single data bucket

This commit is contained in:
Asim Aslam 2020-04-08 09:57:51 +01:00
parent 4b0e27413e
commit 98fc3dfbad
2 changed files with 14 additions and 11 deletions

View File

@ -22,6 +22,9 @@ var (
DefaultTable = "micro"
// DefaultDir is the default directory for bbolt files
DefaultDir = os.TempDir()
// bucket used for data storage
dataBucket = "data"
)
// NewStore returns a memory store
@ -49,7 +52,7 @@ type record struct {
func (m *fileStore) delete(key string) error {
return m.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(m.options.Table))
b := tx.Bucket([]byte(dataBucket))
if b == nil {
return nil
}
@ -100,7 +103,7 @@ func (m *fileStore) init(opts ...store.Option) error {
// create the table
return db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(m.options.Table))
_, err := tx.CreateBucketIfNotExists([]byte(dataBucket))
return err
})
}
@ -109,7 +112,7 @@ func (m *fileStore) list(limit, offset uint) []string {
var allItems []string
m.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(m.options.Table))
b := tx.Bucket([]byte(dataBucket))
// nothing to read
if b == nil {
return nil
@ -164,7 +167,7 @@ func (m *fileStore) get(k string) (*store.Record, error) {
m.db.View(func(tx *bolt.Tx) error {
// @todo this is still very experimental...
b := tx.Bucket([]byte(m.options.Table))
b := tx.Bucket([]byte(dataBucket))
if b == nil {
return nil
}
@ -211,10 +214,10 @@ func (m *fileStore) set(r *store.Record) error {
data, _ := json.Marshal(item)
return m.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(m.options.Table))
b := tx.Bucket([]byte(dataBucket))
if b == nil {
var err error
b, err = tx.CreateBucketIfNotExists([]byte(m.options.Table))
b, err = tx.CreateBucketIfNotExists([]byte(dataBucket))
if err != nil {
return err
}

View File

@ -15,7 +15,7 @@ import (
func cleanup(db string, s store.Store) {
s.Close()
dir := filepath.Join(DefaultDir, db + "/")
dir := filepath.Join(DefaultDir, db+"/")
os.RemoveAll(dir)
}
@ -249,19 +249,19 @@ func fileTest(s store.Store, t *testing.T) {
t.Error(err)
} else {
if len(results) != 5 {
t.Error("Expected 5 results, got ", len(results))
t.Fatal("Expected 5 results, got ", len(results))
}
if !strings.HasPrefix(results[0].Key, "a") {
t.Errorf("Expected a prefix, got %s", results[0].Key)
t.Fatalf("Expected a prefix, got %s", results[0].Key)
}
}
// read the rest back
if results, err := s.Read("a", store.ReadLimit(30), store.ReadOffset(5), store.ReadPrefix()); err != nil {
t.Error(err)
t.Fatal(err)
} else {
if len(results) != 5 {
t.Error("Expected 5 results, got ", len(results))
t.Fatal("Expected 5 results, got ", len(results))
}
}
}