Skip to content

Commit

Permalink
use dropprefix instead of delete all keys
Browse files Browse the repository at this point in the history
  • Loading branch information
y0sher committed Aug 12, 2024
1 parent 1b0bf94 commit ae9108d
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 35 deletions.
2 changes: 1 addition & 1 deletion ibft/genesisstorage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (i *ibftStorage) CleanAllInstances(logger *zap.Logger, msgID []byte) error
prefix := i.prefix
prefix = append(prefix, msgID[:]...)
prefix = append(prefix, []byte(instanceKey)...)
_, err := i.db.DeletePrefix(prefix)
err := i.db.DropPrefix(prefix)
if err != nil {
return errors.Wrap(err, "failed to remove decided")
}
Expand Down
2 changes: 1 addition & 1 deletion ibft/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (i *ibftStorage) CleanAllInstances(logger *zap.Logger, msgID []byte) error
prefix := i.prefix
prefix = append(prefix, msgID[:]...)
prefix = append(prefix, []byte(instanceKey)...)
_, err := i.db.DeletePrefix(prefix)
err := i.db.DropPrefix(prefix)
if err != nil {
return errors.Wrap(err, "failed to remove decided")
}
Expand Down
1 change: 0 additions & 1 deletion storage/basedb/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type Database interface {

// TODO: consider moving these functions into Reader and ReadWriter interfaces?
CountPrefix(prefix []byte) (int64, error)
DeletePrefix(prefix []byte) (int, error)
DropPrefix(prefix []byte) error
Update(fn func(Txn) error) error
Close() error
Expand Down
32 changes: 0 additions & 32 deletions storage/kv/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package kv
import (
"bytes"
"context"
errors2 "errors"
"sync"
"time"

Expand Down Expand Up @@ -159,19 +158,6 @@ func (b *BadgerDB) Delete(prefix []byte, key []byte) error {
})
}

// DeletePrefix all items with this prefix
func (b *BadgerDB) DeletePrefix(prefix []byte) (int, error) {
count := 0
err := b.db.Update(func(txn *badger.Txn) error {
errs := b.DeleteAllPrefixKeys(prefix, txn)
if errs != nil {
return errors2.Join(errs...)
}
return nil
})
return count, err
}

// GetAll returns all the items of a given collection
func (b *BadgerDB) GetAll(prefix []byte, handler func(int, basedb.Obj) error) error {
// we got issues when reading more than 100 items with iterator (items get mixed up)
Expand Down Expand Up @@ -242,24 +228,6 @@ func (b *BadgerDB) periodicallyReport(interval time.Duration) {
}
}

func (b *BadgerDB) DeleteAllPrefixKeys(prefix []byte, txn *badger.Txn) []error {
var errs []error
opt := badger.DefaultIteratorOptions
opt.Prefix = prefix
opt.PrefetchValues = false

it := txn.NewIterator(opt)
defer it.Close()

for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
if err := txn.Delete(it.Item().KeyCopy(nil)); err != nil {
errs = append(errs, err)
}
}

return errs
}

func (b *BadgerDB) listRawKeys(prefix []byte, txn *badger.Txn) [][]byte {
var keys [][]byte

Expand Down

0 comments on commit ae9108d

Please sign in to comment.