storage: add newBackend and newBatchTx

This is for ease of testing.
This commit is contained in:
Yicheng Qin 2015-08-28 22:03:32 -07:00
parent d2cb732c7b
commit 4b9b0cbcc1
2 changed files with 11 additions and 3 deletions

View File

@ -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
}

View File

@ -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 {