From 459b8eb4ab16516974ff616196e4a2593ecbb7b7 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Sun, 16 Feb 2014 15:43:35 -0700 Subject: Read-only transactional block. --- db.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'db.go') 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. -- cgit v1.2.3