core/rawdb, core/filtermaps: polish code

This commit is contained in:
Gary Rong 2025-03-31 19:50:58 +08:00
parent c825264e37
commit d1590055c5
3 changed files with 14 additions and 14 deletions

View file

@ -391,18 +391,18 @@ func (f *FilterMaps) safeDeleteWithLogs(deleteFn func(db ethdb.KeyValueStore, ha
) )
switch err := deleteFn(f.db, f.hashScheme, func(deleted bool) bool { switch err := deleteFn(f.db, f.hashScheme, func(deleted bool) bool {
if deleted && !logPrinted || time.Since(lastLogPrinted) > time.Second*10 { if deleted && !logPrinted || time.Since(lastLogPrinted) > time.Second*10 {
log.Info(action+" in progress...", "elapsed", time.Since(start)) log.Info(action+" in progress...", "elapsed", common.PrettyDuration(time.Since(start)))
logPrinted, lastLogPrinted = true, time.Now() logPrinted, lastLogPrinted = true, time.Now()
} }
return stopCb() return stopCb()
}); err { }); {
case nil: case err == nil:
if logPrinted { if logPrinted {
log.Info(action+" finished", "elapsed", time.Since(start)) log.Info(action+" finished", "elapsed", common.PrettyDuration(time.Since(start)))
} }
return nil return nil
case rawdb.ErrDeleteRangeInterrupted: case errors.Is(err, rawdb.ErrDeleteRangeInterrupted):
log.Warn(action+" interrupted", "elapsed", time.Since(start)) log.Warn(action+" interrupted", "elapsed", common.PrettyDuration(time.Since(start)))
return err return err
default: default:
log.Error(action+" failed", "error", err) log.Error(action+" failed", "error", err)
@ -759,7 +759,7 @@ func (f *FilterMaps) deleteTailEpoch(epoch uint32) (bool, error) {
if f.cleanedEpochsBefore > epoch { if f.cleanedEpochsBefore > epoch {
f.cleanedEpochsBefore = epoch f.cleanedEpochsBefore = epoch
} }
if err == rawdb.ErrDeleteRangeInterrupted { if errors.Is(err, rawdb.ErrDeleteRangeInterrupted) {
return false, nil return false, nil
} }
return false, err return false, err

View file

@ -366,7 +366,7 @@ func ReadFilterMapLastBlock(db ethdb.KeyValueReader, mapIndex uint32) (uint64, c
return 0, common.Hash{}, err return 0, common.Hash{}, err
} }
if len(enc) != 40 { if len(enc) != 40 {
return 0, common.Hash{}, errors.New("Invalid block number and id encoding") return 0, common.Hash{}, errors.New("invalid block number and id encoding")
} }
var id common.Hash var id common.Hash
copy(id[:], enc[8:]) copy(id[:], enc[8:])
@ -404,7 +404,7 @@ func ReadBlockLvPointer(db ethdb.KeyValueReader, blockNumber uint64) (uint64, er
return 0, err return 0, err
} }
if len(encPtr) != 8 { if len(encPtr) != 8 {
return 0, errors.New("Invalid log value pointer encoding") return 0, errors.New("invalid log value pointer encoding")
} }
return binary.BigEndian.Uint64(encPtr), nil return binary.BigEndian.Uint64(encPtr), nil
} }
@ -490,7 +490,7 @@ func DeleteFilterMapsDb(db ethdb.KeyValueStore, hashScheme bool, stopCallback fu
return deletePrefixRange(db, []byte(filterMapsPrefix), hashScheme, stopCallback) return deletePrefixRange(db, []byte(filterMapsPrefix), hashScheme, stopCallback)
} }
// DeleteFilterMapsDb removes the old bloombits database and the associated // DeleteBloomBitsDb removes the old bloombits database and the associated
// chain indexer database. // chain indexer database.
func DeleteBloomBitsDb(db ethdb.KeyValueStore, hashScheme bool, stopCallback func(bool) bool) error { func DeleteBloomBitsDb(db ethdb.KeyValueStore, hashScheme bool, stopCallback func(bool) bool) error {
if err := deletePrefixRange(db, bloomBitsPrefix, hashScheme, stopCallback); err != nil { if err := deletePrefixRange(db, bloomBitsPrefix, hashScheme, stopCallback); err != nil {

View file

@ -627,10 +627,10 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool,
if !hashScheme { if !hashScheme {
// delete entire range; use fast native range delete on pebble db // delete entire range; use fast native range delete on pebble db
for { for {
switch err := db.DeleteRange(start, end); err { switch err := db.DeleteRange(start, end); {
case nil: case err == nil:
return nil return nil
case ethdb.ErrTooManyKeys: case errors.Is(err, ethdb.ErrTooManyKeys):
if stopCallback(true) { if stopCallback(true) {
return ErrDeleteRangeInterrupted return ErrDeleteRangeInterrupted
} }
@ -650,7 +650,7 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool,
it := db.NewIterator(nil, start) it := db.NewIterator(nil, start)
defer func() { defer func() {
it.Release() // it might be replaced during the process it.Release() // it might be replaced during the process
log.Debug("SafeDeleteRange finished", "deleted", deleted, "skipped", skipped, "elapsed", time.Since(startTime)) log.Debug("SafeDeleteRange finished", "deleted", deleted, "skipped", skipped, "elapsed", common.PrettyDuration(time.Since(startTime)))
}() }()
for it.Next() && bytes.Compare(end, it.Key()) > 0 { for it.Next() && bytes.Compare(end, it.Key()) > 0 {