77 "time"
88
99 "github.com/pkg/errors"
10+ "github.com/syndtr/goleveldb/leveldb/filter"
11+ optPkg "github.com/syndtr/goleveldb/leveldb/opt"
1012)
1113
1214const (
@@ -63,6 +65,7 @@ type Config struct {
6365 // Options for services
6466 RPC * RPCConfig `mapstructure:"rpc"`
6567 P2P * P2PConfig `mapstructure:"p2p"`
68+ DBCache * DBCacheConfig `mapstructure:"dbcache"`
6669 Mempool * MempoolConfig `mapstructure:"mempool"`
6770 Consensus * ConsensusConfig `mapstructure:"consensus"`
6871 TxIndex * TxIndexConfig `mapstructure:"tx_index"`
@@ -75,6 +78,7 @@ func DefaultConfig() *Config {
7578 BaseConfig : DefaultBaseConfig (),
7679 RPC : DefaultRPCConfig (),
7780 P2P : DefaultP2PConfig (),
81+ DBCache : DefaultDBCacheConfig (),
7882 Mempool : DefaultMempoolConfig (),
7983 Consensus : DefaultConsensusConfig (),
8084 TxIndex : DefaultTxIndexConfig (),
@@ -88,6 +92,7 @@ func TestConfig() *Config {
8892 BaseConfig : TestBaseConfig (),
8993 RPC : TestRPCConfig (),
9094 P2P : TestP2PConfig (),
95+ DBCache : TestDBCacheConfig (),
9196 Mempool : TestMempoolConfig (),
9297 Consensus : TestConsensusConfig (),
9398 TxIndex : TestTxIndexConfig (),
@@ -117,6 +122,9 @@ func (cfg *Config) ValidateBasic() error {
117122 if err := cfg .P2P .ValidateBasic (); err != nil {
118123 return errors .Wrap (err , "Error in [p2p] section" )
119124 }
125+ if err := cfg .DBCache .ValidateBasic (); err != nil {
126+ return errors .Wrap (err , "Error in [dbcache] section" )
127+ }
120128 if err := cfg .Mempool .ValidateBasic (); err != nil {
121129 return errors .Wrap (err , "Error in [mempool] section" )
122130 }
@@ -477,11 +485,11 @@ func DefaultP2PConfig() *P2PConfig {
477485 MaxNumOutboundPeers : 10 ,
478486 FlushThrottleTimeout : 10 * time .Millisecond ,
479487 MaxPacketMsgPayloadSize : 1024 * 1024 , // 1 MB
480- KeysPerRequest : 2500 , // would be around 250K for account node
488+ KeysPerRequest : 2500 , // would be around 250K for account node
481489 SendRate : 50 * 1024 * 1024 , // 50 MB/s
482490 RecvRate : 50 * 1024 * 1024 , // 50 MB/s
483- PingInterval : 60 * time .Second ,
484- PongTimeout : 45 * time .Second ,
491+ PingInterval : 60 * time .Second ,
492+ PongTimeout : 45 * time .Second ,
485493 PexReactor : true ,
486494 SeedMode : false ,
487495 AllowDuplicateIP : false ,
@@ -551,6 +559,73 @@ func DefaultFuzzConnConfig() *FuzzConnConfig {
551559 }
552560}
553561
562+ //-----------------------------------------------------------------------------
563+ // DBCacheConfig defines the cache related configuration (should not impact back compatibility) options of goleveldb
564+ type DBCacheConfig struct {
565+ // OpenFilesCacheCapacity defines the capacity of the open files caching.
566+ OpenFilesCacheCapacity int `mapstructure:"open_files_cache_capacity"`
567+
568+ // BlockCacheCapacity defines the capacity of the 'sorted table' block caching.
569+ BlockCacheCapacity int `mapstructure:"block_cache_capacity"`
570+
571+ // WriteBuffer defines maximum size of a 'memdb' before flushed to
572+ // 'sorted table'.
573+ WriteBuffer int `mapstructure:"write_buffer"`
574+
575+ // Filter defines an 'effective filter' to use. An 'effective filter'
576+ // if defined will be used to generate per-table filter block.
577+ // Greater than 0 would creates a new initialized bloom filter for given bitsPerKey.
578+ BitsPerKey int `mapstructure:"bits_per_key"`
579+ }
580+
581+ // DefaultDBCacheConfig returns a default configuration for goleveldb config
582+ func DefaultDBCacheConfig () * DBCacheConfig {
583+ return & DBCacheConfig {
584+ OpenFilesCacheCapacity : 1024 ,
585+ BlockCacheCapacity : 8 * optPkg .MiB ,
586+ WriteBuffer : 4 * optPkg .MiB ,
587+ BitsPerKey : 10 ,
588+ }
589+ }
590+
591+ // TestDBCacheConfig returns a configuration for testing
592+ func TestDBCacheConfig () * DBCacheConfig {
593+ cfg := DefaultDBCacheConfig ()
594+ cfg .OpenFilesCacheCapacity = 500
595+ cfg .BlockCacheCapacity = 8 * optPkg .MiB
596+ cfg .WriteBuffer = 4 * optPkg .MiB
597+ cfg .BitsPerKey = 0
598+ return cfg
599+ }
600+
601+ // ValidateBasic performs basic validation (checking param bounds, etc.) and
602+ // returns an error if any check fails.
603+ func (cfg * DBCacheConfig ) ValidateBasic () error {
604+ if cfg .OpenFilesCacheCapacity < 0 {
605+ return errors .New ("open_files_cache_capacity can't be negative" )
606+ }
607+ if cfg .BlockCacheCapacity < 0 {
608+ return errors .New ("block_cache_capacity can't be negative" )
609+ }
610+ if cfg .WriteBuffer < 0 {
611+ return errors .New ("write_buffer can't be negative" )
612+ }
613+ return nil
614+ }
615+
616+ func (cfg * DBCacheConfig ) ToGolevelDBOpt () * optPkg.Options {
617+ var fltr filter.Filter
618+ if cfg .BitsPerKey > 0 {
619+ fltr = filter .NewBloomFilter (cfg .BitsPerKey )
620+ }
621+ return & optPkg.Options {
622+ OpenFilesCacheCapacity : cfg .OpenFilesCacheCapacity ,
623+ BlockCacheCapacity : cfg .BlockCacheCapacity ,
624+ WriteBuffer : cfg .WriteBuffer ,
625+ Filter : fltr ,
626+ }
627+ }
628+
554629//-----------------------------------------------------------------------------
555630// MempoolConfig
556631
0 commit comments