func (s *badgerStore) IterateKeys(prefix kvstore.KeyPrefix, consumerFunc kvstore.IteratorKeyConsumerFunc, iterDirection ...kvstore.IterDirection) error {
	return s.instance.View(func(txn *badger.Txn) (err error) {
		keyPrefix := s.buildKeyPrefix(prefix)
		iteratorOptions := badger.DefaultIteratorOptions
		iteratorOptions.Prefix = keyPrefix
		iteratorOptions.PrefetchValues = false
		iteratorOptions.Reverse = kvstore.GetIterDirection(iterDirection...) == kvstore.IterDirectionBackward
		it := txn.NewIterator(iteratorOptions)
		defer it.Close()
		startFunc, validFunc, moveFunc, err := s.getIterFuncs(it, keyPrefix, iterDirection...)
		if err != nil {
			return err
		}
		for startFunc(); validFunc(); moveFunc() {
			if !consumerFunc(it.Item().KeyCopy(nil)[len(s.dbPrefix):]) {
				break
			}
		}
		return nil
	})
}
IterateKeys iterates over all keys with the provided prefix. You can pass kvstore.EmptyPrefix to iterate over all keys.
Optionally the direction for the iteration can be passed (default: IterDirectionForward).
package badger
import (
	"errors"
	"sync"
	"github.com/dgraph-io/badger/v2"
	"github.com/iotaledger/hive.go/byteutils"
	"github.com/iotaledger/hive.go/kvstore"
	"github.com/iotaledger/hive.go/kvstore/utils"
	"github.com/iotaledger/hive.go/types"
)
type badgerStore struct {
	instance *badger.DB
	dbPrefix []byte
}
func New(db *badger.DB) kvstore.KVStore {
	return &badgerStore{
		instance: db,
	}
}
func (s *badgerStore) WithRealm(realm kvstore.Realm) kvstore.KVStore {
	return &badgerStore{
		instance: s.instance,
		dbPrefix: realm,
	}
}
func (s *badgerStore) Realm() []byte {
	return s.dbPrefix
}
func (s *badgerStore) buildKeyPrefix(prefix kvstore.KeyPrefix) kvstore.KeyPrefix {
	return byteutils.ConcatBytes(s.dbPrefix, prefix)
}
func (s *badgerStore) Shutdown() {
}
func (s *badgerStore) getIterFuncs(it *badger.Iterator, keyPrefix []byte, iterDirection ...kvstore.IterDirection) (start func(), valid func() bool, move func(), err error) {
	startFunc := it.Rewind
	validFunc := it.Valid
	moveFunc := it.Next
	if len(keyPrefix) > 0 {
		startFunc = func() {
			it.Seek(keyPrefix)
		}
		validFunc = func() bool {
			return it.ValidForPrefix(keyPrefix)
		}
	}
	if kvstore.GetIterDirection(iterDirection...) == kvstore.IterDirectionBackward {
		if len(keyPrefix) > 0 {
			prefixUpperBound := utils.KeyPrefixUpperBound(keyPrefix)
			if prefixUpperBound == nil {
				return nil, nil, nil, errors.New("no upper bound for prefix")
			}
			startFunc = func() {
				it.Seek(prefixUpperBound)
				if !validFunc() {
					moveFunc()
				}
			}
		}
	}
	return startFunc, validFunc, moveFunc, nil
}
func (s *badgerStore) Iterate(prefix kvstore.KeyPrefix, consumerFunc kvstore.IteratorKeyValueConsumerFunc, iterDirection ...kvstore.IterDirection) error {
	return s.instance.View(func(txn *badger.Txn) (err error) {
		keyPrefix := s.buildKeyPrefix(prefix)
		iteratorOptions := badger.DefaultIteratorOptions
		iteratorOptions.Prefix = keyPrefix
		iteratorOptions.PrefetchValues = true
		iteratorOptions.Reverse = kvstore.GetIterDirection(iterDirection...) == kvstore.IterDirectionBackward
		it := txn.NewIterator(iteratorOptions)
		defer it.Close()
		startFunc, validFunc, moveFunc, err := s.getIterFuncs(it, keyPrefix, iterDirection...)
		if err != nil {
			return err
		}
		for startFunc(); validFunc(); moveFunc() {
			item := it.Item()
			value, err := item.ValueCopy(nil)
			if err != nil {
				panic(err)
			}
			if !consumerFunc(item.KeyCopy(nil)[len(s.dbPrefix):], value) {
				break
			}
		}
		return nil
	})
}