From e9e6ec3a2c22b3139c5787dd3e6c8a61960ec522 Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 28 Sep 2017 17:43:47 +0200 Subject: [PATCH] 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. --- db/go_level_db.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/db/go_level_db.go b/db/go_level_db.go index 31c85ce86..2d2ba44e3 100644 --- a/db/go_level_db.go +++ b/db/go_level_db.go @@ -6,6 +6,7 @@ import ( "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/errors" + "github.com/syndtr/goleveldb/leveldb/iterator" "github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/util" @@ -116,12 +117,32 @@ func (db *GoLevelDB) Stats() map[string]string { 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 { - return db.db.NewIterator(nil, nil) + return &goLevelDBIterator{db.db.NewIterator(nil, nil)} } 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 {