From 09923bdf6a4654d60fecd98091e526c1fc2414fb Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 7 May 2014 12:38:51 +0800 Subject: [PATCH] leveled iterator range use close interval --- leveldb/db.go | 9 ++++----- leveldb/iterator.go | 4 ++-- leveldb/leveldb_test.go | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/leveldb/db.go b/leveldb/db.go index c348ed4..0fa0462 100644 --- a/leveldb/db.go +++ b/leveldb/db.go @@ -134,19 +134,18 @@ func (db *DB) NewWriteBatch() *WriteBatch { return wb } -//like c++ iterator, [begin, end) -//begin should less than end +//[begin, end] close(inclusive) interval //if begin is nil, we will seek to first -//if end is nil, we will next until read last +//if end is nil, we will seek to last //limit <= 0, no limit func (db *DB) Iterator(begin []byte, end []byte, limit int) *Iterator { return newIterator(db, db.iteratorOpts, begin, end, limit, forward) } -//like c++ reverse_iterator, [rbegin, rend) +//[rbegin, rend] close(inclusive) interval //rbegin should bigger than rend //if rbegin is nil, we will seek to last -//if end is nil, we will next until read first +//if end is nil, we will seek to first //limit <= 0, no limit func (db *DB) ReverseIterator(rbegin []byte, rend []byte, limit int) *Iterator { return newIterator(db, db.iteratorOpts, rbegin, rend, limit, backward) diff --git a/leveldb/iterator.go b/leveldb/iterator.go index e8f1e19..7cbc213 100644 --- a/leveldb/iterator.go +++ b/leveldb/iterator.go @@ -62,11 +62,11 @@ func (it *Iterator) Valid() bool { } if it.direction == forward { - if it.stop != nil && bytes.Compare(it.Key(), it.stop) >= 0 { + if it.stop != nil && bytes.Compare(it.Key(), it.stop) > 0 { return false } } else { - if it.stop != nil && bytes.Compare(it.Key(), it.stop) <= 0 { + if it.stop != nil && bytes.Compare(it.Key(), it.stop) < 0 { return false } } diff --git a/leveldb/leveldb_test.go b/leveldb/leveldb_test.go index 6b9934b..6bee93c 100644 --- a/leveldb/leveldb_test.go +++ b/leveldb/leveldb_test.go @@ -176,7 +176,7 @@ func TestIterator(t *testing.T) { } it.Close() - if step != 5 { + if step != 6 { t.Fatal("invalid step", step) } @@ -245,7 +245,7 @@ func TestIterator(t *testing.T) { } it.Close() - if step != 2 { + if step != 1 { t.Fatal("invalid step", step) }