Skip to content

Commit

Permalink
chore(core/rawdb): allow to pass the same option multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Feb 12, 2025
1 parent b2c38ce commit 696e7fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
31 changes: 18 additions & 13 deletions core/rawdb/database.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
})
}

Expand All @@ -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)
})
}

Expand All @@ -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)
})
}
10 changes: 7 additions & 3 deletions core/rawdb/database.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
},
},
}
Expand All @@ -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]
Expand Down Expand Up @@ -119,15 +123,15 @@ 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 |
// | Light client | Bloom trie nodes | 0.00 B | 0 |
// | 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 | |
// +-----------------------+-------------------------+---------+-------+
}

Expand Down

0 comments on commit 696e7fc

Please sign in to comment.