Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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