Merge pull request #64 from benbjohnson/fix-empty-cursor-last

Fix Cursor.Last() on empty buckets.
pull/34/head
Ben Johnson 2014-03-13 13:51:43 -07:00
commit fdd7f2162e
2 changed files with 22 additions and 7 deletions

View File

@ -235,7 +235,7 @@ func (c *Cursor) nsearch(key []byte) {
// keyValue returns the key and value of the current leaf element.
func (c *Cursor) keyValue() ([]byte, []byte) {
ref := &c.stack[len(c.stack)-1]
if ref.index >= ref.count() {
if ref.count() == 0 || ref.index >= ref.count() {
return nil, nil
}

View File

@ -190,12 +190,27 @@ func TestTxDeleteBucketNotFound(t *testing.T) {
func TestTxCursorEmptyBucket(t *testing.T) {
withOpenDB(func(db *DB, path string) {
db.CreateBucket("widgets")
tx, _ := db.Tx()
c := tx.Bucket("widgets").Cursor()
k, v := c.First()
assert.Nil(t, k)
assert.Nil(t, v)
tx.Commit()
db.With(func(tx *Tx) error {
c := tx.Bucket("widgets").Cursor()
k, v := c.First()
assert.Nil(t, k)
assert.Nil(t, v)
return nil
})
})
}
// Ensure that a Tx cursor can reverse iterate over an empty bucket without error.
func TestCursorEmptyBucketReverse(t *testing.T) {
withOpenDB(func(db *DB, path string) {
db.CreateBucket("widgets")
db.With(func(tx *Tx) error {
c := tx.Bucket("widgets").Cursor()
k, v := c.Last()
assert.Nil(t, k)
assert.Nil(t, v)
return nil
})
})
}