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
|
package memorydb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"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)
|
// DeleteRange deletes all of the keys (and values) in the range [start,end)
|
||||||
// (inclusive on start, exclusive on end).
|
// (inclusive on start, exclusive on end).
|
||||||
func (db *Database) DeleteRange(start, end []byte) error {
|
func (db *Database) DeleteRange(start, end []byte) error {
|
||||||
it := db.NewIterator(nil, start)
|
db.lock.Lock()
|
||||||
defer it.Release()
|
defer db.lock.Unlock()
|
||||||
|
if db.db == nil {
|
||||||
|
return errMemorydbClosed
|
||||||
|
}
|
||||||
|
|
||||||
for it.Next() && bytes.Compare(end, it.Key()) > 0 {
|
for key := range db.db {
|
||||||
if err := db.Delete(it.Key()); err != nil {
|
if key >= string(start) && key < string(end) {
|
||||||
return err
|
delete(db.db, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue