Browse Source

fix c level db iterator

pull/1842/head
Ethan Buchman 7 years ago
parent
commit
66b0e8fa2d
2 changed files with 30 additions and 20 deletions
  1. +8
    -8
      db/backend_test.go
  2. +22
    -12
      db/c_level_db.go

+ 8
- 8
db/backend_test.go View File

@ -70,12 +70,12 @@ func TestBackendsNilKeys(t *testing.T) {
}
func TestGoLevelDBBackendStr(t *testing.T) {
name := cmn.Fmt("test_%x", cmn.RandStr(12))
db := NewDB(name, LevelDBBackendStr, "")
defer cleanupDBDir("", name)
if _, ok := backends[CLevelDBBackendStr]; !ok {
_, ok := db.(*GoLevelDB)
assert.True(t, ok)
}
name := cmn.Fmt("test_%x", cmn.RandStr(12))
db := NewDB(name, LevelDBBackendStr, "")
defer cleanupDBDir("", name)
if _, ok := backends[CLevelDBBackendStr]; !ok {
_, ok := db.(*GoLevelDB)
assert.True(t, ok)
}
}

+ 22
- 12
db/c_level_db.go View File

@ -162,16 +162,7 @@ func (mBatch *cLevelDBBatch) Write() {
func (db *CLevelDB) Iterator(start, end []byte) Iterator {
itr := db.db.NewIterator(db.ro)
if len(start) > 0 {
itr.Seek(start)
} else {
itr.SeekToFirst()
}
return &cLevelDBIterator{
itr: itr,
start: start,
end: end,
}
return newCLevelDBIterator(itr, start, end)
}
func (db *CLevelDB) ReverseIterator(start, end []byte) Iterator {
@ -187,6 +178,21 @@ type cLevelDBIterator struct {
invalid bool
}
func newCLevelDBIterator(itr *levigo.Iterator, start, end []byte) *cLevelDBIterator {
if len(start) > 0 {
itr.Seek(start)
} else {
itr.SeekToFirst()
}
return &cLevelDBIterator{
itr: itr,
start: start,
end: end,
}
}
func (c *cLevelDBIterator) Domain() ([]byte, []byte) {
return c.start, c.end
}
@ -223,12 +229,16 @@ func (c *cLevelDBIterator) Next() {
}
// levigo has no upper bound when iterating, so need to check ourselves
func (c *cLevelDBIterator) checkEndKey() []byte {
func (c *cLevelDBIterator) checkEndKey() {
if !c.itr.Valid() {
c.invalid = true
return
}
key := c.itr.Key()
if c.end != nil && bytes.Compare(key, c.end) > 0 {
c.invalid = true
}
return key
}
func (c *cLevelDBIterator) Close() {


Loading…
Cancel
Save