aboutsummaryrefslogtreecommitdiff
path: root/db.go
diff options
context:
space:
mode:
authorBen Johnson <benbjohnson@yahoo.com>2014-02-16 15:43:35 -0700
committerBen Johnson <benbjohnson@yahoo.com>2014-02-16 15:43:35 -0700
commit459b8eb4ab16516974ff616196e4a2593ecbb7b7 (patch)
tree67cb8ddbbde55b26db5fb3a7463cd181e41be465 /db.go
parentMerge pull request #36 from benbjohnson/for-each (diff)
downloaddedo-459b8eb4ab16516974ff616196e4a2593ecbb7b7.tar.gz
dedo-459b8eb4ab16516974ff616196e4a2593ecbb7b7.tar.xz
Read-only transactional block.
Diffstat (limited to 'db.go')
-rw-r--r--db.go18
1 files changed, 14 insertions, 4 deletions
diff --git a/db.go b/db.go
index 0276d7a..b7338c3 100644
--- a/db.go
+++ b/db.go
@@ -359,15 +359,25 @@ func (db *DB) Do(fn func(*RWTransaction) error) error {
return t.Commit()
}
-// ForEach executes a function for each key/value pair in a bucket.
-// An error is returned if the bucket cannot be found.
-func (db *DB) ForEach(name string, fn func(k, v []byte) error) error {
+// With executes a function within the context of a Transaction.
+// Any error that is returned from the function is returned from the With() method.
+func (db *DB) With(fn func(*Transaction) error) error {
t, err := db.Transaction()
if err != nil {
return err
}
defer t.Close()
- return t.ForEach(name, fn)
+
+ // If an error is returned from the function then pass it through.
+ return fn(t)
+}
+
+// ForEach executes a function for each key/value pair in a bucket.
+// An error is returned if the bucket cannot be found.
+func (db *DB) ForEach(name string, fn func(k, v []byte) error) error {
+ return db.With(func(t *Transaction) error {
+ return t.ForEach(name, fn)
+ })
}
// Bucket retrieves a reference to a bucket.