From 4b9b0cbcc1814e3956307d7003f1e76877802dae Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Fri, 28 Aug 2015 22:03:32 -0700 Subject: [PATCH] storage: add newBackend and newBatchTx This is for ease of testing. --- storage/backend/backend.go | 8 +++++--- storage/backend/batch_tx.go | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/storage/backend/backend.go b/storage/backend/backend.go index 9389d308d..0fb9f02eb 100644 --- a/storage/backend/backend.go +++ b/storage/backend/backend.go @@ -27,6 +27,10 @@ type backend struct { } func New(path string, d time.Duration, limit int) Backend { + return newBackend(path, d, limit) +} + +func newBackend(path string, d time.Duration, limit int) *backend { db, err := bolt.Open(path, 0600, nil) if err != nil { log.Panicf("backend: cannot open database at %s (%v)", path, err) @@ -37,13 +41,11 @@ func New(path string, d time.Duration, limit int) Backend { batchInterval: d, batchLimit: limit, - batchTx: &batchTx{}, stopc: make(chan struct{}), donec: make(chan struct{}), } - b.batchTx.backend = b - b.batchTx.Commit() + b.batchTx = newBatchTx(b) go b.run() return b } diff --git a/storage/backend/batch_tx.go b/storage/backend/batch_tx.go index 92aced332..56797b85c 100644 --- a/storage/backend/batch_tx.go +++ b/storage/backend/batch_tx.go @@ -26,6 +26,12 @@ type batchTx struct { pending int } +func newBatchTx(backend *backend) *batchTx { + tx := &batchTx{backend: backend} + tx.Commit() + return tx +} + func (t *batchTx) UnsafeCreateBucket(name []byte) { _, err := t.tx.CreateBucket(name) if err != nil && err != bolt.ErrBucketExists {