Browse Source

db: cleveldb iterator

pull/1842/head
Ethan Buchman 7 years ago
parent
commit
bcacaf164b
2 changed files with 22 additions and 15 deletions
  1. +19
    -13
      db/c_level_db.go
  2. +3
    -2
      db/common_test.go

+ 19
- 13
db/c_level_db.go View File

@ -17,6 +17,8 @@ func init() {
registerDBCreator(CLevelDBBackendStr, dbCreator, false)
}
var _ DB = (*CLevelDB)(nil)
type CLevelDB struct {
db *levigo.DB
ro *levigo.ReadOptions
@ -158,13 +160,17 @@ func (mBatch *cLevelDBBatch) Write() {
// Iterator
func (db *CLevelDB) Iterator(start, end []byte) Iterator {
/*
XXX
itr := db.db.NewIterator(db.ro)
itr.Seek([]byte{0x00})
return cLevelDBIterator{itr}
*/
return nil
itr := db.db.NewIterator(db.ro)
if len(start) > 0 {
itr.Seek(start)
} else {
itr.SeekToFirst()
}
return cLevelDBIterator{
itr: itr,
start: start,
end: end,
}
}
func (db *CLevelDB) ReverseIterator(start, end []byte) Iterator {
@ -172,15 +178,15 @@ func (db *CLevelDB) ReverseIterator(start, end []byte) Iterator {
return nil
}
var _ Iterator = (*cLevelDBIterator)(nil)
type cLevelDBIterator struct {
itr *levigo.Iterator
itr *levigo.Iterator
start, end []byte
}
func (c cLevelDBIterator) Seek(key []byte) {
if key == nil {
key = []byte{0x00}
}
c.itr.Seek(key)
func (c cLevelDBIterator) Domain() ([]byte, []byte) {
return c.start, c.end
}
func (c cLevelDBIterator) Valid() bool {


+ 3
- 2
db/common_test.go View File

@ -5,18 +5,19 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cmn "github.com/tendermint/tmlibs/common"
)
func checkValid(t *testing.T, itr Iterator, expected bool) {
valid := itr.Valid()
assert.Equal(t, expected, valid)
require.Equal(t, expected, valid)
}
func checkNext(t *testing.T, itr Iterator, expected bool) {
itr.Next()
valid := itr.Valid()
assert.Equal(t, expected, valid)
require.Equal(t, expected, valid)
}
func checkNextPanics(t *testing.T, itr Iterator) {


Loading…
Cancel
Save