diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-02-16 15:43:35 -0700 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-02-16 15:43:35 -0700 |
commit | 459b8eb4ab16516974ff616196e4a2593ecbb7b7 (patch) | |
tree | 67cb8ddbbde55b26db5fb3a7463cd181e41be465 /db.go | |
parent | Merge pull request #36 from benbjohnson/for-each (diff) | |
download | dedo-459b8eb4ab16516974ff616196e4a2593ecbb7b7.tar.gz dedo-459b8eb4ab16516974ff616196e4a2593ecbb7b7.tar.xz |
Read-only transactional block.
Diffstat (limited to 'db.go')
-rw-r--r-- | db.go | 18 |
1 files changed, 14 insertions, 4 deletions
@@ -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. |