diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-23 12:17:30 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-03-23 12:20:16 -0600 |
commit | 482f00fdfc52f0b4592014b37dc9e1749a7cfd6d (patch) | |
tree | 7ecbac715fd4556b77cb46ff6a1096a39111fab5 /bucket.go | |
parent | Merge pull request #78 from benbjohnson/tx-managed (diff) | |
download | dedo-482f00fdfc52f0b4592014b37dc9e1749a7cfd6d.tar.gz dedo-482f00fdfc52f0b4592014b37dc9e1749a7cfd6d.tar.xz |
Add ErrTxClosed error.
Commit/Rollback and mutable calls on Tx and Bucket now return ErrTxClosed
if the transaction has already been committed or rolled back. Non-mutable
calls have added an assertion to check if the transaction is closed which
will cause a panic. I don't want to introduce an error return for accessor
methods that are being used improperly so I think the panic is appropriate.
Diffstat (limited to 'bucket.go')
-rw-r--r-- | bucket.go | 15 |
1 files changed, 12 insertions, 3 deletions
@@ -55,7 +55,9 @@ func (b *Bucket) Get(key []byte) []byte { // If the key exist then its previous value will be overwritten. // Returns an error if the bucket was created from a read-only transaction, if the key is blank, if the key is too large, or if the value is too large. func (b *Bucket) Put(key []byte, value []byte) error { - if !b.Writable() { + if b.tx.db == nil { + return ErrTxClosed + } else if !b.Writable() { return ErrBucketNotWritable } @@ -82,7 +84,9 @@ func (b *Bucket) Put(key []byte, value []byte) error { // If the key does not exist then nothing is done and a nil error is returned. // Returns an error if the bucket was created from a read-only transaction. func (b *Bucket) Delete(key []byte) error { - if !b.Writable() { + if b.tx.db == nil { + return ErrTxClosed + } else if !b.Writable() { return ErrBucketNotWritable } @@ -98,7 +102,9 @@ func (b *Bucket) Delete(key []byte) error { // NextSequence returns an autoincrementing integer for the bucket. func (b *Bucket) NextSequence() (int, error) { - if !b.Writable() { + if b.tx.db == nil { + return 0, ErrTxClosed + } else if !b.Writable() { return 0, ErrBucketNotWritable } @@ -118,6 +124,9 @@ func (b *Bucket) NextSequence() (int, error) { // If the provided function returns an error then the iteration is stopped and // the error is returned to the caller. func (b *Bucket) ForEach(fn func(k, v []byte) error) error { + if b.tx.db == nil { + return ErrTxClosed + } c := b.Cursor() for k, v := c.First(); k != nil; k, v = c.Next() { if err := fn(k, v); err != nil { |