mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-24 07:34:31 +00:00
chore(core/rawdb): InspectDatabase can receive multiple times the same option (#132)
This commit is contained in:
parent
b2c38ce397
commit
0c905680f3
2 changed files with 25 additions and 16 deletions
|
|
@ -27,28 +27,33 @@ import (
|
|||
type InspectDatabaseOption = options.Option[inspectDatabaseConfig]
|
||||
|
||||
type inspectDatabaseConfig struct {
|
||||
statRecorder func([]byte, common.StorageSize) bool
|
||||
isMeta func([]byte) bool
|
||||
statsTransformer func([][]string) [][]string
|
||||
statRecorders []func([]byte, common.StorageSize) bool
|
||||
isMetas []func([]byte) bool
|
||||
statsTransformers []func([][]string) [][]string
|
||||
}
|
||||
|
||||
func (c inspectDatabaseConfig) recordStat(key []byte, size common.StorageSize) bool {
|
||||
if r := c.statRecorder; r != nil {
|
||||
return r(key, size)
|
||||
matched := false
|
||||
for _, f := range c.statRecorders {
|
||||
if f(key, size) {
|
||||
matched = true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return matched
|
||||
}
|
||||
|
||||
func (c inspectDatabaseConfig) isMetadata(key []byte) bool {
|
||||
if m := c.isMeta; m != nil {
|
||||
return m(key)
|
||||
for _, f := range c.isMetas {
|
||||
if f(key) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c inspectDatabaseConfig) transformStats(stats [][]string) [][]string {
|
||||
if f := c.statsTransformer; f != nil {
|
||||
return f(stats)
|
||||
for _, f := range c.statsTransformers {
|
||||
stats = f(stats)
|
||||
}
|
||||
return stats
|
||||
}
|
||||
|
|
@ -63,7 +68,7 @@ func newInspectOpt(fn func(*inspectDatabaseConfig)) InspectDatabaseOption {
|
|||
// stopping further matches.
|
||||
func WithDatabaseStatRecorder(rec func(key []byte, size common.StorageSize) bool) InspectDatabaseOption {
|
||||
return newInspectOpt(func(c *inspectDatabaseConfig) {
|
||||
c.statRecorder = rec
|
||||
c.statRecorders = append(c.statRecorders, rec)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +80,7 @@ type DatabaseStat = stat
|
|||
// being counted with the metadata statistic i.f.f. the function returns true.
|
||||
func WithDatabaseMetadataKeys(isMetadata func(key []byte) bool) InspectDatabaseOption {
|
||||
return newInspectOpt(func(c *inspectDatabaseConfig) {
|
||||
c.isMeta = isMetadata
|
||||
c.isMetas = append(c.isMetas, isMetadata)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -85,6 +90,6 @@ func WithDatabaseMetadataKeys(isMetadata func(key []byte) bool) InspectDatabaseO
|
|||
// Each row contains 4 columns: database, category, size and count.
|
||||
func WithDatabaseStatsTransformer(transform func(rows [][]string) [][]string) InspectDatabaseOption {
|
||||
return newInspectOpt(func(c *inspectDatabaseConfig) {
|
||||
c.statsTransformer = transform
|
||||
c.statsTransformers = append(c.statsTransformers, transform)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,8 +52,9 @@ func ExampleInspectDatabase() {
|
|||
{key: []byte("iBxxx"), value: []byte("m")},
|
||||
// Optional stat record total = 5 + 7 = 12
|
||||
{key: []byte("mykey"), value: []byte("myvalue")},
|
||||
// metadata total = 13 + 7 = 20
|
||||
// metadata total = (13 + 7) + (14 + 7) = 41
|
||||
{key: []byte("mymetadatakey"), value: []byte("myvalue")},
|
||||
{key: []byte("mymetadatakey2"), value: []byte("myvalue")},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
@ -75,6 +76,9 @@ func ExampleInspectDatabase() {
|
|||
rawdb.WithDatabaseMetadataKeys(func(key []byte) bool {
|
||||
return bytes.Equal(key, []byte("mymetadatakey"))
|
||||
}),
|
||||
rawdb.WithDatabaseMetadataKeys(func(key []byte) bool {
|
||||
return bytes.Equal(key, []byte("mymetadatakey2"))
|
||||
}),
|
||||
rawdb.WithDatabaseStatsTransformer(func(rows [][]string) [][]string {
|
||||
sort.Slice(rows, func(i, j int) bool {
|
||||
ri, rj := rows[i], rows[j]
|
||||
|
|
@ -119,7 +123,7 @@ func ExampleInspectDatabase() {
|
|||
// | Key-Value store | Path trie state lookups | 0.00 B | 0 |
|
||||
// | Key-Value store | Path trie storage nodes | 0.00 B | 0 |
|
||||
// | Key-Value store | Receipt lists | 0.00 B | 0 |
|
||||
// | Key-Value store | Singleton metadata | 20.00 B | 1 |
|
||||
// | Key-Value store | Singleton metadata | 41.00 B | 2 |
|
||||
// | Key-Value store | Storage snapshot | 0.00 B | 0 |
|
||||
// | Key-Value store | Transaction index | 0.00 B | 0 |
|
||||
// | Key-Value store | Trie preimages | 0.00 B | 0 |
|
||||
|
|
@ -127,7 +131,7 @@ func ExampleInspectDatabase() {
|
|||
// | Light client | CHT trie nodes | 0.00 B | 0 |
|
||||
// | My database | My category | 12.00 B | 1 |
|
||||
// +-----------------------+-------------------------+---------+-------+
|
||||
// | TOTAL | 38.00 B | |
|
||||
// | TOTAL | 59.00 B | |
|
||||
// +-----------------------+-------------------------+---------+-------+
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue