chore(core/rawdb): InspectDatabase can receive multiple times the same option (#132)

This commit is contained in:
Quentin McGaw 2025-02-12 13:55:40 +01:00 committed by GitHub
parent b2c38ce397
commit 0c905680f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 16 deletions

View file

@ -27,28 +27,33 @@ import (
type InspectDatabaseOption = options.Option[inspectDatabaseConfig] type InspectDatabaseOption = options.Option[inspectDatabaseConfig]
type inspectDatabaseConfig struct { type inspectDatabaseConfig struct {
statRecorder func([]byte, common.StorageSize) bool statRecorders []func([]byte, common.StorageSize) bool
isMeta func([]byte) bool isMetas []func([]byte) bool
statsTransformer func([][]string) [][]string statsTransformers []func([][]string) [][]string
} }
func (c inspectDatabaseConfig) recordStat(key []byte, size common.StorageSize) bool { func (c inspectDatabaseConfig) recordStat(key []byte, size common.StorageSize) bool {
if r := c.statRecorder; r != nil { matched := false
return r(key, size) for _, f := range c.statRecorders {
if f(key, size) {
matched = true
}
} }
return false return matched
} }
func (c inspectDatabaseConfig) isMetadata(key []byte) bool { func (c inspectDatabaseConfig) isMetadata(key []byte) bool {
if m := c.isMeta; m != nil { for _, f := range c.isMetas {
return m(key) if f(key) {
return true
}
} }
return false return false
} }
func (c inspectDatabaseConfig) transformStats(stats [][]string) [][]string { func (c inspectDatabaseConfig) transformStats(stats [][]string) [][]string {
if f := c.statsTransformer; f != nil { for _, f := range c.statsTransformers {
return f(stats) stats = f(stats)
} }
return stats return stats
} }
@ -63,7 +68,7 @@ func newInspectOpt(fn func(*inspectDatabaseConfig)) InspectDatabaseOption {
// stopping further matches. // stopping further matches.
func WithDatabaseStatRecorder(rec func(key []byte, size common.StorageSize) bool) InspectDatabaseOption { func WithDatabaseStatRecorder(rec func(key []byte, size common.StorageSize) bool) InspectDatabaseOption {
return newInspectOpt(func(c *inspectDatabaseConfig) { 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. // being counted with the metadata statistic i.f.f. the function returns true.
func WithDatabaseMetadataKeys(isMetadata func(key []byte) bool) InspectDatabaseOption { func WithDatabaseMetadataKeys(isMetadata func(key []byte) bool) InspectDatabaseOption {
return newInspectOpt(func(c *inspectDatabaseConfig) { 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. // Each row contains 4 columns: database, category, size and count.
func WithDatabaseStatsTransformer(transform func(rows [][]string) [][]string) InspectDatabaseOption { func WithDatabaseStatsTransformer(transform func(rows [][]string) [][]string) InspectDatabaseOption {
return newInspectOpt(func(c *inspectDatabaseConfig) { return newInspectOpt(func(c *inspectDatabaseConfig) {
c.statsTransformer = transform c.statsTransformers = append(c.statsTransformers, transform)
}) })
} }

View file

@ -52,8 +52,9 @@ func ExampleInspectDatabase() {
{key: []byte("iBxxx"), value: []byte("m")}, {key: []byte("iBxxx"), value: []byte("m")},
// Optional stat record total = 5 + 7 = 12 // Optional stat record total = 5 + 7 = 12
{key: []byte("mykey"), value: []byte("myvalue")}, {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("mymetadatakey"), value: []byte("myvalue")},
{key: []byte("mymetadatakey2"), value: []byte("myvalue")},
}, },
}, },
} }
@ -75,6 +76,9 @@ func ExampleInspectDatabase() {
rawdb.WithDatabaseMetadataKeys(func(key []byte) bool { rawdb.WithDatabaseMetadataKeys(func(key []byte) bool {
return bytes.Equal(key, []byte("mymetadatakey")) return bytes.Equal(key, []byte("mymetadatakey"))
}), }),
rawdb.WithDatabaseMetadataKeys(func(key []byte) bool {
return bytes.Equal(key, []byte("mymetadatakey2"))
}),
rawdb.WithDatabaseStatsTransformer(func(rows [][]string) [][]string { rawdb.WithDatabaseStatsTransformer(func(rows [][]string) [][]string {
sort.Slice(rows, func(i, j int) bool { sort.Slice(rows, func(i, j int) bool {
ri, rj := rows[i], rows[j] 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 state lookups | 0.00 B | 0 |
// | Key-Value store | Path trie storage nodes | 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 | 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 | Storage snapshot | 0.00 B | 0 |
// | Key-Value store | Transaction index | 0.00 B | 0 | // | Key-Value store | Transaction index | 0.00 B | 0 |
// | Key-Value store | Trie preimages | 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 | // | Light client | CHT trie nodes | 0.00 B | 0 |
// | My database | My category | 12.00 B | 1 | // | My database | My category | 12.00 B | 1 |
// +-----------------------+-------------------------+---------+-------+ // +-----------------------+-------------------------+---------+-------+
// | TOTAL | 38.00 B | | // | TOTAL | 59.00 B | |
// +-----------------------+-------------------------+---------+-------+ // +-----------------------+-------------------------+---------+-------+
} }