From 696e7fc394008252bc102f1f6b23ef3667f5bb9e Mon Sep 17 00:00:00 2001 From: Quentin Mc Gaw Date: Wed, 12 Feb 2025 13:05:36 +0100 Subject: [PATCH] chore(core/rawdb): allow to pass the same option multiple times --- core/rawdb/database.libevm.go | 31 +++++++++++++++++------------- core/rawdb/database.libevm_test.go | 10 +++++++--- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/core/rawdb/database.libevm.go b/core/rawdb/database.libevm.go index 78f86d7e277..f563f5a97f0 100644 --- a/core/rawdb/database.libevm.go +++ b/core/rawdb/database.libevm.go @@ -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) }) } diff --git a/core/rawdb/database.libevm_test.go b/core/rawdb/database.libevm_test.go index 963cf662ca2..684ab117738 100644 --- a/core/rawdb/database.libevm_test.go +++ b/core/rawdb/database.libevm_test.go @@ -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 | | // +-----------------------+-------------------------+---------+-------+ }