leveled iterator range use close interval

This commit is contained in:
siddontang 2014-05-07 12:38:51 +08:00
parent 0eb5881662
commit 09923bdf6a
3 changed files with 8 additions and 9 deletions

View File

@ -134,19 +134,18 @@ func (db *DB) NewWriteBatch() *WriteBatch {
return wb return wb
} }
//like c++ iterator, [begin, end) //[begin, end] close(inclusive) interval
//begin should less than end
//if begin is nil, we will seek to first //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 //limit <= 0, no limit
func (db *DB) Iterator(begin []byte, end []byte, limit int) *Iterator { func (db *DB) Iterator(begin []byte, end []byte, limit int) *Iterator {
return newIterator(db, db.iteratorOpts, begin, end, limit, forward) 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 //rbegin should bigger than rend
//if rbegin is nil, we will seek to last //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 //limit <= 0, no limit
func (db *DB) ReverseIterator(rbegin []byte, rend []byte, limit int) *Iterator { func (db *DB) ReverseIterator(rbegin []byte, rend []byte, limit int) *Iterator {
return newIterator(db, db.iteratorOpts, rbegin, rend, limit, backward) return newIterator(db, db.iteratorOpts, rbegin, rend, limit, backward)

View File

@ -62,11 +62,11 @@ func (it *Iterator) Valid() bool {
} }
if it.direction == forward { 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 return false
} }
} else { } 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 return false
} }
} }

View File

@ -176,7 +176,7 @@ func TestIterator(t *testing.T) {
} }
it.Close() it.Close()
if step != 5 { if step != 6 {
t.Fatal("invalid step", step) t.Fatal("invalid step", step)
} }
@ -245,7 +245,7 @@ func TestIterator(t *testing.T) {
} }
it.Close() it.Close()
if step != 2 { if step != 1 {
t.Fatal("invalid step", step) t.Fatal("invalid step", step)
} }