From 0792eb6e26f5b3f1eac7508ab26c934aab9acba2 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 8 May 2014 09:22:51 +0800 Subject: [PATCH] iterator first offset bug fix --- leveldb/iterator.go | 8 ++++++-- leveldb/leveldb_test.go | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/leveldb/iterator.go b/leveldb/iterator.go index d3e19bd..599b04e 100644 --- a/leveldb/iterator.go +++ b/leveldb/iterator.go @@ -98,8 +98,12 @@ func newIterator(db *DB, opts *levigo.ReadOptions, r *Range, offset int, limit i } for i := 0; i < offset; i++ { - if it.Valid() { - it.Next() + if it.it.Valid() { + if it.direction == IteratorForward { + it.it.Next() + } else { + it.it.Prev() + } } } diff --git a/leveldb/leveldb_test.go b/leveldb/leveldb_test.go index 4f478f3..9f68781 100644 --- a/leveldb/leveldb_test.go +++ b/leveldb/leveldb_test.go @@ -152,7 +152,7 @@ func TestIterator(t *testing.T) { } it = db.Iterator(k(1), k(5), RangeClose, 1, 3) - if err := checkIterator(it, 2, 3); err != nil { + if err := checkIterator(it, 2, 3, 4); err != nil { t.Fatal(err) } @@ -177,7 +177,7 @@ func TestIterator(t *testing.T) { } it = db.RevIterator(k(1), k(5), RangeClose, 1, 3) - if err := checkIterator(it, 4, 3); err != nil { + if err := checkIterator(it, 4, 3, 2); err != nil { t.Fatal(err) }