aboutsummaryrefslogtreecommitdiff
path: root/bucket.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-03-23 12:17:30 -0600
committerBen Johnson <benbjohnson@yahoo.com>2014-03-23 12:20:16 -0600
commit482f00fdfc52f0b4592014b37dc9e1749a7cfd6d (patch)
tree7ecbac715fd4556b77cb46ff6a1096a39111fab5 /bucket.go
parentMerge pull request #78 from benbjohnson/tx-managed (diff)
downloaddedo-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.go15
1 files changed, 12 insertions, 3 deletions
diff --git a/bucket.go b/bucket.go
index c72ddc8..e2bf60c 100644
--- a/bucket.go
+++ b/bucket.go
@@ -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 {