Browse Source

Add IteratorPrefix method to Iterator

pull/1842/head
Alexis Sellier 7 years ago
parent
commit
219d4e8427
3 changed files with 14 additions and 1 deletions
  1. +1
    -0
      db/db.go
  2. +5
    -0
      db/go_level_db.go
  3. +8
    -1
      db/mem_db.go

+ 1
- 0
db/db.go View File

@ -11,6 +11,7 @@ type DB interface {
Close() Close()
NewBatch() Batch NewBatch() Batch
Iterator() Iterator Iterator() Iterator
IteratorPrefix([]byte) Iterator
// For debugging // For debugging
Print() Print()


+ 5
- 0
db/go_level_db.go View File

@ -7,6 +7,7 @@ import (
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
. "github.com/tendermint/tmlibs/common" . "github.com/tendermint/tmlibs/common"
) )
@ -119,6 +120,10 @@ func (db *GoLevelDB) Iterator() Iterator {
return db.db.NewIterator(nil, nil) return db.db.NewIterator(nil, nil)
} }
func (db *GoLevelDB) IteratorPrefix(prefix []byte) Iterator {
return db.db.NewIterator(util.BytesPrefix(prefix), nil)
}
func (db *GoLevelDB) NewBatch() Batch { func (db *GoLevelDB) NewBatch() Batch {
batch := new(leveldb.Batch) batch := new(leveldb.Batch)
return &goLevelDBBatch{db, batch} return &goLevelDBBatch{db, batch}


+ 8
- 1
db/mem_db.go View File

@ -2,6 +2,7 @@ package db
import ( import (
"fmt" "fmt"
"strings"
"sync" "sync"
) )
@ -108,6 +109,10 @@ func (it *memDBIterator) Error() error {
} }
func (db *MemDB) Iterator() Iterator { func (db *MemDB) Iterator() Iterator {
return db.IteratorPrefix([]byte{})
}
func (db *MemDB) IteratorPrefix(prefix []byte) Iterator {
it := newMemDBIterator() it := newMemDBIterator()
it.db = db it.db = db
it.last = -1 it.last = -1
@ -117,7 +122,9 @@ func (db *MemDB) Iterator() Iterator {
// unfortunately we need a copy of all of the keys // unfortunately we need a copy of all of the keys
for key, _ := range db.db { for key, _ := range db.db {
it.keys = append(it.keys, key)
if strings.HasPrefix(key, string(prefix)) {
it.keys = append(it.keys, key)
}
} }
return it return it
} }


Loading…
Cancel
Save