Browse Source

Implement Key/Value on Iterator for GoLevelDB

This is needed because leveldb reuses the keys on each iteration.
In our wrapper, we copy the key/value so that it is safe to store.
pull/1842/head
Alexis Sellier 7 years ago
parent
commit
e9e6ec3a2c
1 changed files with 23 additions and 2 deletions
  1. +23
    -2
      db/go_level_db.go

+ 23
- 2
db/go_level_db.go View File

@ -6,6 +6,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/iterator"
"github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util" "github.com/syndtr/goleveldb/leveldb/util"
@ -116,12 +117,32 @@ func (db *GoLevelDB) Stats() map[string]string {
return stats return stats
} }
type goLevelDBIterator struct {
iterator.Iterator
}
func (it *goLevelDBIterator) Key() []byte {
key := it.Key()
k := make([]byte, len(key))
copy(k, key)
return k
}
func (it *goLevelDBIterator) Value() []byte {
val := it.Value()
v := make([]byte, len(val))
copy(v, val)
return v
}
func (db *GoLevelDB) Iterator() Iterator { func (db *GoLevelDB) Iterator() Iterator {
return db.db.NewIterator(nil, nil)
return &goLevelDBIterator{db.db.NewIterator(nil, nil)}
} }
func (db *GoLevelDB) IteratorPrefix(prefix []byte) Iterator { func (db *GoLevelDB) IteratorPrefix(prefix []byte) Iterator {
return db.db.NewIterator(util.BytesPrefix(prefix), nil)
return &goLevelDBIterator{db.db.NewIterator(util.BytesPrefix(prefix), nil)}
} }
func (db *GoLevelDB) NewBatch() Batch { func (db *GoLevelDB) NewBatch() Batch {


Loading…
Cancel
Save