ethdb/memorydb: faster DeleteRange (#31038)
This PR replaces the iterator based DeleteRange implementation of memorydb with a simpler and much faster loop that directly deletes keys in the order of iteration instead of unnecessarily collecting keys in memory and sorting them. --------- Co-authored-by: Martin HS <martin@swende.se>
This commit is contained in:
parent
a7f9523ae1
commit
ea31bd9faf
|
@ -18,7 +18,6 @@
|
|||
package memorydb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
|
@ -125,12 +124,15 @@ func (db *Database) Delete(key []byte) error {
|
|||
// DeleteRange deletes all of the keys (and values) in the range [start,end)
|
||||
// (inclusive on start, exclusive on end).
|
||||
func (db *Database) DeleteRange(start, end []byte) error {
|
||||
it := db.NewIterator(nil, start)
|
||||
defer it.Release()
|
||||
db.lock.Lock()
|
||||
defer db.lock.Unlock()
|
||||
if db.db == nil {
|
||||
return errMemorydbClosed
|
||||
}
|
||||
|
||||
for it.Next() && bytes.Compare(end, it.Key()) > 0 {
|
||||
if err := db.Delete(it.Key()); err != nil {
|
||||
return err
|
||||
for key := range db.db {
|
||||
if key >= string(start) && key < string(end) {
|
||||
delete(db.db, key)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue