diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2014-02-16 14:00:32 -0700 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2014-02-16 14:00:32 -0700 |
commit | f8fd84b2be54c2ca900521f5ca51a811bdb2a2c3 (patch) | |
tree | fbdf8f185404a1eaa92c927bf0fce63126b69f5f /transaction.go | |
parent | Add CreateBucketIfNotExists(). (diff) | |
parent | Add Transaction.ForEach(). (diff) | |
download | dedo-f8fd84b2be54c2ca900521f5ca51a811bdb2a2c3.tar.gz dedo-f8fd84b2be54c2ca900521f5ca51a811bdb2a2c3.tar.xz |
Merge pull request #36 from benbjohnson/for-each
ForEach()
Diffstat (limited to 'transaction.go')
-rw-r--r-- | transaction.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/transaction.go b/transaction.go index 0c9da7b..54aae8c 100644 --- a/transaction.go +++ b/transaction.go @@ -93,6 +93,25 @@ func (t *Transaction) Get(name string, key []byte) (value []byte, err error) { return c.Get(key), nil } +// ForEach executes a function for each key/value pair in a bucket. +// An error is returned if the bucket cannot be found. +func (t *Transaction) ForEach(name string, fn func(k, v []byte) error) error { + // Open a cursor on the bucket. + c, err := t.Cursor(name) + if err != nil { + return err + } + + // Iterate over each key/value pair in the bucket. + for k, v := c.First(); k != nil; k, v = c.Next() { + if err := fn(k, v); err != nil { + return err + } + } + + return nil +} + // page returns a reference to the page with a given id. // If page has been written to then a temporary bufferred page is returned. func (t *Transaction) page(id pgid) *page { |