Skip to content

Commit 696e7fc

Browse files
committed
chore(core/rawdb): allow to pass the same option multiple times
1 parent b2c38ce commit 696e7fc

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

core/rawdb/database.libevm.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,33 @@ import (
2727
type InspectDatabaseOption = options.Option[inspectDatabaseConfig]
2828

2929
type inspectDatabaseConfig struct {
30-
statRecorder func([]byte, common.StorageSize) bool
31-
isMeta func([]byte) bool
32-
statsTransformer func([][]string) [][]string
30+
statRecorders []func([]byte, common.StorageSize) bool
31+
isMetas []func([]byte) bool
32+
statsTransformers []func([][]string) [][]string
3333
}
3434

3535
func (c inspectDatabaseConfig) recordStat(key []byte, size common.StorageSize) bool {
36-
if r := c.statRecorder; r != nil {
37-
return r(key, size)
36+
matched := false
37+
for _, f := range c.statRecorders {
38+
if f(key, size) {
39+
matched = true
40+
}
3841
}
39-
return false
42+
return matched
4043
}
4144

4245
func (c inspectDatabaseConfig) isMetadata(key []byte) bool {
43-
if m := c.isMeta; m != nil {
44-
return m(key)
46+
for _, f := range c.isMetas {
47+
if f(key) {
48+
return true
49+
}
4550
}
4651
return false
4752
}
4853

4954
func (c inspectDatabaseConfig) transformStats(stats [][]string) [][]string {
50-
if f := c.statsTransformer; f != nil {
51-
return f(stats)
55+
for _, f := range c.statsTransformers {
56+
stats = f(stats)
5257
}
5358
return stats
5459
}
@@ -63,7 +68,7 @@ func newInspectOpt(fn func(*inspectDatabaseConfig)) InspectDatabaseOption {
6368
// stopping further matches.
6469
func WithDatabaseStatRecorder(rec func(key []byte, size common.StorageSize) bool) InspectDatabaseOption {
6570
return newInspectOpt(func(c *inspectDatabaseConfig) {
66-
c.statRecorder = rec
71+
c.statRecorders = append(c.statRecorders, rec)
6772
})
6873
}
6974

@@ -75,7 +80,7 @@ type DatabaseStat = stat
7580
// being counted with the metadata statistic i.f.f. the function returns true.
7681
func WithDatabaseMetadataKeys(isMetadata func(key []byte) bool) InspectDatabaseOption {
7782
return newInspectOpt(func(c *inspectDatabaseConfig) {
78-
c.isMeta = isMetadata
83+
c.isMetas = append(c.isMetas, isMetadata)
7984
})
8085
}
8186

@@ -85,6 +90,6 @@ func WithDatabaseMetadataKeys(isMetadata func(key []byte) bool) InspectDatabaseO
8590
// Each row contains 4 columns: database, category, size and count.
8691
func WithDatabaseStatsTransformer(transform func(rows [][]string) [][]string) InspectDatabaseOption {
8792
return newInspectOpt(func(c *inspectDatabaseConfig) {
88-
c.statsTransformer = transform
93+
c.statsTransformers = append(c.statsTransformers, transform)
8994
})
9095
}

core/rawdb/database.libevm_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ func ExampleInspectDatabase() {
5252
{key: []byte("iBxxx"), value: []byte("m")},
5353
// Optional stat record total = 5 + 7 = 12
5454
{key: []byte("mykey"), value: []byte("myvalue")},
55-
// metadata total = 13 + 7 = 20
55+
// metadata total = (13 + 7) + (14 + 7) = 41
5656
{key: []byte("mymetadatakey"), value: []byte("myvalue")},
57+
{key: []byte("mymetadatakey2"), value: []byte("myvalue")},
5758
},
5859
},
5960
}
@@ -75,6 +76,9 @@ func ExampleInspectDatabase() {
7576
rawdb.WithDatabaseMetadataKeys(func(key []byte) bool {
7677
return bytes.Equal(key, []byte("mymetadatakey"))
7778
}),
79+
rawdb.WithDatabaseMetadataKeys(func(key []byte) bool {
80+
return bytes.Equal(key, []byte("mymetadatakey2"))
81+
}),
7882
rawdb.WithDatabaseStatsTransformer(func(rows [][]string) [][]string {
7983
sort.Slice(rows, func(i, j int) bool {
8084
ri, rj := rows[i], rows[j]
@@ -119,15 +123,15 @@ func ExampleInspectDatabase() {
119123
// | Key-Value store | Path trie state lookups | 0.00 B | 0 |
120124
// | Key-Value store | Path trie storage nodes | 0.00 B | 0 |
121125
// | Key-Value store | Receipt lists | 0.00 B | 0 |
122-
// | Key-Value store | Singleton metadata | 20.00 B | 1 |
126+
// | Key-Value store | Singleton metadata | 41.00 B | 2 |
123127
// | Key-Value store | Storage snapshot | 0.00 B | 0 |
124128
// | Key-Value store | Transaction index | 0.00 B | 0 |
125129
// | Key-Value store | Trie preimages | 0.00 B | 0 |
126130
// | Light client | Bloom trie nodes | 0.00 B | 0 |
127131
// | Light client | CHT trie nodes | 0.00 B | 0 |
128132
// | My database | My category | 12.00 B | 1 |
129133
// +-----------------------+-------------------------+---------+-------+
130-
// | TOTAL | 38.00 B | |
134+
// | TOTAL | 59.00 B | |
131135
// +-----------------------+-------------------------+---------+-------+
132136
}
133137

0 commit comments

Comments
 (0)