use single data bucket

This commit is contained in:
Asim Aslam 2020-04-08 09:57:51 +01:00 committed by Vasiliy Tolstov
parent 1c20be9d91
commit 2ee0a43774
2 changed files with 14 additions and 11 deletions

15
file.go
View File

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

View File

@ -15,7 +15,7 @@ import (
func cleanup(db string, s store.Store) { func cleanup(db string, s store.Store) {
s.Close() s.Close()
dir := filepath.Join(DefaultDir, db + "/") dir := filepath.Join(DefaultDir, db+"/")
os.RemoveAll(dir) os.RemoveAll(dir)
} }
@ -249,19 +249,19 @@ func fileTest(s store.Store, t *testing.T) {
t.Error(err) t.Error(err)
} else { } else {
if len(results) != 5 { 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") { 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 // read the rest back
if results, err := s.Read("a", store.ReadLimit(30), store.ReadOffset(5), store.ReadPrefix()); err != nil { if results, err := s.Read("a", store.ReadLimit(30), store.ReadOffset(5), store.ReadPrefix()); err != nil {
t.Error(err) t.Fatal(err)
} else { } else {
if len(results) != 5 { if len(results) != 5 {
t.Error("Expected 5 results, got ", len(results)) t.Fatal("Expected 5 results, got ", len(results))
} }
} }
} }