mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-18 02:40:45 +00:00
internal/ethapi: improve ChaindbCompact() and ChaindbProperty() #19021 #19856 #28207, fix XFN-139 (#1722)
* internal/ethapi: clean up and properly abstract database accesses #19021 * internal/ethapi: fix debug.chaindbProperty #19856 * internal/ethapi: compact db missing key starts with 0xff #28207
This commit is contained in:
parent
851bdc45db
commit
611668c237
1 changed files with 15 additions and 20 deletions
|
|
@ -53,8 +53,6 @@ import (
|
||||||
"github.com/XinFinOrg/XDPoSChain/params"
|
"github.com/XinFinOrg/XDPoSChain/params"
|
||||||
"github.com/XinFinOrg/XDPoSChain/rlp"
|
"github.com/XinFinOrg/XDPoSChain/rlp"
|
||||||
"github.com/XinFinOrg/XDPoSChain/rpc"
|
"github.com/XinFinOrg/XDPoSChain/rpc"
|
||||||
"github.com/syndtr/goleveldb/leveldb"
|
|
||||||
"github.com/syndtr/goleveldb/leveldb/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -3080,33 +3078,30 @@ func (api *DebugAPI) PrintBlock(ctx context.Context, number uint64) (string, err
|
||||||
return block.String(), nil
|
return block.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChaindbProperty returns leveldb properties of the chain database.
|
// ChaindbProperty returns leveldb properties of the key-value database.
|
||||||
func (api *DebugAPI) ChaindbProperty(property string) (string, error) {
|
func (api *DebugAPI) ChaindbProperty(property string) (string, error) {
|
||||||
ldb, ok := api.b.ChainDb().(interface {
|
|
||||||
LDB() *leveldb.DB
|
|
||||||
})
|
|
||||||
if !ok {
|
|
||||||
return "", errors.New("chaindbProperty does not work for memory databases")
|
|
||||||
}
|
|
||||||
if property == "" {
|
if property == "" {
|
||||||
property = "leveldb.stats"
|
property = "leveldb.stats"
|
||||||
} else if !strings.HasPrefix(property, "leveldb.") {
|
} else if !strings.HasPrefix(property, "leveldb.") {
|
||||||
property = "leveldb." + property
|
property = "leveldb." + property
|
||||||
}
|
}
|
||||||
return ldb.LDB().GetProperty(property)
|
return api.b.ChainDb().Stat(property)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChaindbCompact flattens the entire key-value database into a single level,
|
||||||
|
// removing all unused slots and merging all keys.
|
||||||
func (api *DebugAPI) ChaindbCompact() error {
|
func (api *DebugAPI) ChaindbCompact() error {
|
||||||
ldb, ok := api.b.ChainDb().(interface {
|
cstart := time.Now()
|
||||||
LDB() *leveldb.DB
|
for b := 0; b <= 255; b++ {
|
||||||
})
|
var (
|
||||||
if !ok {
|
start = []byte{byte(b)}
|
||||||
return errors.New("chaindbCompact does not work for memory databases")
|
end = []byte{byte(b + 1)}
|
||||||
|
)
|
||||||
|
if b == 255 {
|
||||||
|
end = nil
|
||||||
}
|
}
|
||||||
for b := byte(0); b < 255; b++ {
|
log.Info("Compacting database", "range", fmt.Sprintf("%#X-%#X", start, end), "elapsed", common.PrettyDuration(time.Since(cstart)))
|
||||||
log.Info("Compacting chain database", "range", fmt.Sprintf("0x%0.2X-0x%0.2X", b, b+1))
|
if err := api.b.ChainDb().Compact(start, end); err != nil {
|
||||||
err := ldb.LDB().CompactRange(util.Range{Start: []byte{b}, Limit: []byte{b + 1}})
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Database compaction failed", "err", err)
|
log.Error("Database compaction failed", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue