Browse Source

storage: add newBackend and newBatchTx

This is for ease of testing.
Yicheng Qin 10 years ago
parent
commit
4b9b0cbcc1
2 changed files with 11 additions and 3 deletions
  1. 5 3
      storage/backend/backend.go
  2. 6 0
      storage/backend/batch_tx.go

+ 5 - 3
storage/backend/backend.go

@@ -27,6 +27,10 @@ type backend struct {
 }
 }
 
 
 func New(path string, d time.Duration, limit int) Backend {
 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)
 	db, err := bolt.Open(path, 0600, nil)
 	if err != nil {
 	if err != nil {
 		log.Panicf("backend: cannot open database at %s (%v)", path, err)
 		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,
 		batchInterval: d,
 		batchLimit:    limit,
 		batchLimit:    limit,
-		batchTx:       &batchTx{},
 
 
 		stopc: make(chan struct{}),
 		stopc: make(chan struct{}),
 		donec: make(chan struct{}),
 		donec: make(chan struct{}),
 	}
 	}
-	b.batchTx.backend = b
-	b.batchTx.Commit()
+	b.batchTx = newBatchTx(b)
 	go b.run()
 	go b.run()
 	return b
 	return b
 }
 }

+ 6 - 0
storage/backend/batch_tx.go

@@ -26,6 +26,12 @@ type batchTx struct {
 	pending int
 	pending int
 }
 }
 
 
+func newBatchTx(backend *backend) *batchTx {
+	tx := &batchTx{backend: backend}
+	tx.Commit()
+	return tx
+}
+
 func (t *batchTx) UnsafeCreateBucket(name []byte) {
 func (t *batchTx) UnsafeCreateBucket(name []byte) {
 	_, err := t.tx.CreateBucket(name)
 	_, err := t.tx.CreateBucket(name)
 	if err != nil && err != bolt.ErrBucketExists {
 	if err != nil && err != bolt.ErrBucketExists {