Skip to content

Commit 225955e

Browse files
committed
refactor: entryCache -> blockCache
1 parent e5336fc commit 225955e

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

x/blockdb/config.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const DefaultMaxDataFileSize = 500 * 1024 * 1024 * 1024
1111
// DefaultMaxDataFiles is the default maximum number of data files descriptors cached.
1212
const DefaultMaxDataFiles = 10
1313

14-
// DefaultEntryCacheSize is the default size of the entry cache.
15-
const DefaultEntryCacheSize = 256
14+
// DefaultBlockCacheSize is the default size of the block cache.
15+
const DefaultBlockCacheSize = 256
1616

1717
// DatabaseConfig contains configuration parameters for BlockDB.
1818
type DatabaseConfig struct {
@@ -31,8 +31,8 @@ type DatabaseConfig struct {
3131
// MaxDataFiles is the maximum number of data files descriptors cached.
3232
MaxDataFiles int
3333

34-
// EntryCacheSize is the size of the entry cache (default: 256).
35-
EntryCacheSize int
34+
// BlockCacheSize is the size of the block cache (default: 256).
35+
BlockCacheSize int
3636

3737
// CheckpointInterval defines how frequently (in blocks) the index file header is updated (default: 1024).
3838
CheckpointInterval uint64
@@ -49,7 +49,7 @@ func DefaultConfig() DatabaseConfig {
4949
MinimumHeight: 0,
5050
MaxDataFileSize: DefaultMaxDataFileSize,
5151
MaxDataFiles: DefaultMaxDataFiles,
52-
EntryCacheSize: DefaultEntryCacheSize,
52+
BlockCacheSize: DefaultBlockCacheSize,
5353
CheckpointInterval: 1024,
5454
SyncToDisk: true,
5555
}
@@ -98,9 +98,9 @@ func (c DatabaseConfig) WithMaxDataFiles(maxFiles int) DatabaseConfig {
9898
return c
9999
}
100100

101-
// WithEntryCacheSize returns a copy of the config with EntryCacheSize set to the given value.
102-
func (c DatabaseConfig) WithEntryCacheSize(size int) DatabaseConfig {
103-
c.EntryCacheSize = size
101+
// WithBlockCacheSize returns a copy of the config with BlockCacheSize set to the given value.
102+
func (c DatabaseConfig) WithBlockCacheSize(size int) DatabaseConfig {
103+
c.BlockCacheSize = size
104104
return c
105105
}
106106

@@ -127,8 +127,8 @@ func (c DatabaseConfig) Validate() error {
127127
if c.MaxDataFileSize == 0 {
128128
return errors.New("MaxDataFileSize must be positive")
129129
}
130-
if c.EntryCacheSize < 1 {
131-
return errors.New("EntryCacheSize cannot be less than 1")
130+
if c.BlockCacheSize < 1 {
131+
return errors.New("BlockCacheSize cannot be less than 1")
132132
}
133133
return nil
134134
}

x/blockdb/database.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ type Database struct {
177177
log logging.Logger
178178
closed bool
179179
fileCache *lru.Cache[int, *os.File]
180-
entryCache *lru.Cache[BlockHeight, BlockData]
180+
blockCache *lru.Cache[BlockHeight, BlockData]
181181
compressor compression.Compressor
182182

183183
// closeMu prevents the database from being closed while in use and prevents
@@ -225,7 +225,7 @@ func New(config DatabaseConfig, log logging.Logger) (*Database, error) {
225225
f.Close()
226226
}
227227
}),
228-
entryCache: lru.NewCache[BlockHeight, BlockData](config.EntryCacheSize),
228+
blockCache: lru.NewCache[BlockHeight, BlockData](config.BlockCacheSize),
229229
compressor: compressor,
230230
}
231231

@@ -234,7 +234,7 @@ func New(config DatabaseConfig, log logging.Logger) (*Database, error) {
234234
zap.String("dataDir", config.DataDir),
235235
zap.Uint64("maxDataFileSize", config.MaxDataFileSize),
236236
zap.Int("maxDataFiles", config.MaxDataFiles),
237-
zap.Int("entryCacheSize", config.EntryCacheSize),
237+
zap.Int("blockCacheSize", config.BlockCacheSize),
238238
)
239239

240240
if err := s.openAndInitializeIndex(); err != nil {
@@ -279,7 +279,7 @@ func (s *Database) Close() error {
279279
}
280280

281281
s.closeFiles()
282-
s.entryCache.Flush()
282+
s.blockCache.Flush()
283283

284284
s.log.Info("Block database closed successfully")
285285
return err
@@ -376,7 +376,7 @@ func (s *Database) Put(height BlockHeight, block BlockData) error {
376376
)
377377
return err
378378
}
379-
s.entryCache.Put(height, slices.Clone(block))
379+
s.blockCache.Put(height, slices.Clone(block))
380380

381381
s.log.Debug("Block written successfully",
382382
zap.Uint64("height", height),
@@ -441,7 +441,7 @@ func (s *Database) Get(height BlockHeight) (BlockData, error) {
441441
return nil, database.ErrClosed
442442
}
443443

444-
if c, ok := s.entryCache.Get(height); ok {
444+
if c, ok := s.blockCache.Get(height); ok {
445445
return slices.Clone(c), nil
446446
}
447447

@@ -495,7 +495,7 @@ func (s *Database) Get(height BlockHeight) (BlockData, error) {
495495
return nil, fmt.Errorf("checksum mismatch: calculated %d, stored %d", calculatedChecksum, bh.Checksum)
496496
}
497497

498-
s.entryCache.Put(height, slices.Clone(decompressed))
498+
s.blockCache.Put(height, slices.Clone(decompressed))
499499
return decompressed, nil
500500
}
501501

@@ -509,7 +509,7 @@ func (s *Database) Has(height BlockHeight) (bool, error) {
509509
return false, database.ErrClosed
510510
}
511511

512-
if _, ok := s.entryCache.Get(height); ok {
512+
if _, ok := s.blockCache.Get(height); ok {
513513
return true, nil
514514
}
515515
_, err := s.readBlockIndex(height)

x/blockdb/entry_cache_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ func TestCacheOnMiss(t *testing.T) {
1717
require.NoError(t, db.Put(height, block))
1818

1919
// Evict the entry from cache to simulate a cache miss
20-
db.entryCache.Evict(height)
20+
db.blockCache.Evict(height)
2121

2222
// Read the block - should populate the cache on cache miss
2323
_, err := db.Get(height)
2424
require.NoError(t, err)
2525

26-
_, ok := db.entryCache.Get(height)
26+
_, ok := db.blockCache.Get(height)
2727
require.True(t, ok)
2828
}
2929

@@ -33,7 +33,7 @@ func TestCacheGet(t *testing.T) {
3333
block := randomBlock(t)
3434

3535
// Populate cache directly without writing to database
36-
db.entryCache.Put(height, block)
36+
db.blockCache.Put(height, block)
3737

3838
// Get should return the block from cache
3939
data, err := db.Get(height)
@@ -47,7 +47,7 @@ func TestCacheHas(t *testing.T) {
4747
block := randomBlock(t)
4848

4949
// Populate cache directly without writing to database
50-
db.entryCache.Put(height, block)
50+
db.blockCache.Put(height, block)
5151

5252
// Has should return true from cache even though block is not in database
5353
has, err := db.Has(height)
@@ -66,7 +66,7 @@ func TestCachePutStoresClone(t *testing.T) {
6666
clone[0] = 99
6767

6868
// Cache should have the original unmodified data
69-
cached, ok := db.entryCache.Get(height)
69+
cached, ok := db.blockCache.Get(height)
7070
require.True(t, ok)
7171
require.Equal(t, block, cached)
7272
}
@@ -83,7 +83,7 @@ func TestCacheGetReturnsClone(t *testing.T) {
8383
data[0] = 99
8484

8585
// Cache should still have the original unmodified data
86-
cached, ok := db.entryCache.Get(height)
86+
cached, ok := db.blockCache.Get(height)
8787
require.True(t, ok)
8888
require.Equal(t, block, cached)
8989

@@ -100,14 +100,14 @@ func TestCachePutOverridesSameHeight(t *testing.T) {
100100
require.NoError(t, db.Put(height, b1))
101101

102102
// Verify first block is in cache
103-
cached, ok := db.entryCache.Get(height)
103+
cached, ok := db.blockCache.Get(height)
104104
require.True(t, ok)
105105
require.Equal(t, b1, cached)
106106

107107
// Put second block at same height and verify it overrides the first one
108108
b2 := randomBlock(t)
109109
require.NoError(t, db.Put(height, b2))
110-
cached, ok = db.entryCache.Get(height)
110+
cached, ok = db.blockCache.Get(height)
111111
require.True(t, ok)
112112
require.Equal(t, b2, cached)
113113
require.NotEqual(t, b1, cached)

x/blockdb/readblock_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestReadOperations(t *testing.T) {
5151
MaxDataFileSize: DefaultMaxDataFileSize,
5252
CheckpointInterval: 1024,
5353
MaxDataFiles: DefaultMaxDataFileSize,
54-
EntryCacheSize: DefaultEntryCacheSize,
54+
BlockCacheSize: DefaultBlockCacheSize,
5555
},
5656
},
5757
{
@@ -70,7 +70,7 @@ func TestReadOperations(t *testing.T) {
7070
MaxDataFileSize: DefaultMaxDataFileSize,
7171
CheckpointInterval: 1024,
7272
MaxDataFiles: DefaultMaxDataFileSize,
73-
EntryCacheSize: DefaultEntryCacheSize,
73+
BlockCacheSize: DefaultBlockCacheSize,
7474
},
7575
wantErr: ErrInvalidBlockHeight,
7676
},

0 commit comments

Comments
 (0)