-
Notifications
You must be signed in to change notification settings - Fork 810
feat: add in memory implementation of HeightIndex Database #4212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6eb6764
feat: added an in-memory mock implementation of blockdb for testing
DracoLi 5cf1948
make copies of the block
DracoLi d8d93e1
refactor memory database
DracoLi c5e218c
remove Inspect
DracoLi 7645992
move memdb into package and add simple tests
DracoLi 9997910
fix naming & separate tests
DracoLi 0ce9c20
chore: implement database.HeightIndex
DracoLi cde510e
feat: update to use database.HeightIndex and move out of blockdb
DracoLi be762aa
fix lint issues
DracoLi edeb8ff
Merge branch 'master' into dl/blockdb-memory-mock
DracoLi e70e213
create dbtest package for heightindexdb
DracoLi 88f2394
update close behaviour on close
DracoLi 0f9a5d9
clarify Put behaviour
DracoLi 1769a5c
fix test case
DracoLi 5454574
fix: pr feedback
DracoLi 344b0e1
update interface description
DracoLi bc432c3
refactor testCase struct
DracoLi bbeb41f
Change the return value req for nil or empty value puts
DracoLi ac5ca8f
update doc wording
DracoLi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package memdb | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
) | ||
|
||
var _ database.HeightIndex = (*Database)(nil) | ||
|
||
// Database is an in-memory implementation of database.HeightIndex | ||
type Database struct { | ||
mu sync.RWMutex | ||
data map[uint64][]byte | ||
closed bool | ||
} | ||
|
||
// Put stores data in memory at the given height | ||
func (d *Database) Put(height uint64, data []byte) error { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
if d.closed { | ||
return database.ErrClosed | ||
} | ||
|
||
if d.data == nil { | ||
d.data = make(map[uint64][]byte) | ||
} | ||
|
||
if len(data) == 0 { | ||
return database.ErrNotFound | ||
} | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
dataCopy := make([]byte, len(data)) | ||
copy(dataCopy, data) | ||
d.data[height] = dataCopy | ||
|
||
return nil | ||
} | ||
|
||
// Get retrieves data at the given height | ||
func (d *Database) Get(height uint64) ([]byte, error) { | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
d.mu.RLock() | ||
defer d.mu.RUnlock() | ||
|
||
if d.closed { | ||
return nil, database.ErrClosed | ||
} | ||
|
||
data, ok := d.data[height] | ||
if !ok { | ||
return nil, database.ErrNotFound | ||
} | ||
|
||
dataCopy := make([]byte, len(data)) | ||
copy(dataCopy, data) | ||
return dataCopy, nil | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
// Has checks if data exists at the given height | ||
func (d *Database) Has(height uint64) (bool, error) { | ||
d.mu.RLock() | ||
defer d.mu.RUnlock() | ||
|
||
if d.closed { | ||
return false, database.ErrClosed | ||
} | ||
|
||
_, ok := d.data[height] | ||
return ok, nil | ||
} | ||
|
||
// Close closes the in-memory database | ||
func (d *Database) Close() error { | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
d.closed = true | ||
d.data = nil | ||
return nil | ||
} | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
DracoLi marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package memdb | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
) | ||
|
||
func TestOperationsAfterClose(t *testing.T) { | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
db := &Database{} | ||
|
||
// Close database | ||
require.NoError(t, db.Close()) | ||
|
||
height := uint64(1) | ||
blockData := []byte("test block data") | ||
|
||
tests := []struct { | ||
name string | ||
fn func() error | ||
}{ | ||
{ | ||
name: "Put", | ||
fn: func() error { | ||
return db.Put(height, blockData) | ||
}, | ||
}, | ||
{ | ||
name: "Get", | ||
fn: func() error { | ||
_, err := db.Get(height) | ||
return err | ||
}, | ||
}, | ||
{ | ||
name: "Has", | ||
fn: func() error { | ||
_, err := db.Has(height) | ||
return err | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.fn() | ||
require.ErrorIs(t, err, database.ErrClosed) | ||
}) | ||
} | ||
} | ||
|
||
func TestPut(t *testing.T) { | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
db := &Database{} | ||
|
||
height := uint64(1) | ||
blockData := []byte("test block data") | ||
require.NoError(t, db.Put(height, blockData)) | ||
} | ||
|
||
func TestGet(t *testing.T) { | ||
db := &Database{} | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
height := uint64(1) | ||
blockData := []byte("test block data") | ||
require.NoError(t, db.Put(height, blockData)) | ||
|
||
// Read block back | ||
retrievedBlock, err := db.Get(height) | ||
require.NoError(t, err) | ||
require.Equal(t, blockData, retrievedBlock) | ||
} | ||
|
||
func TestHas(t *testing.T) { | ||
t.Run("non-existent block", func(t *testing.T) { | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
db := &Database{} | ||
exists, err := db.Has(uint64(1)) | ||
require.NoError(t, err) | ||
require.False(t, exists) | ||
}) | ||
|
||
t.Run("existing block", func(t *testing.T) { | ||
db := &Database{} | ||
blockData := []byte("test block data") | ||
require.NoError(t, db.Put(uint64(1), blockData)) | ||
exists, err := db.Has(uint64(1)) | ||
require.NoError(t, err) | ||
require.True(t, exists) | ||
}) | ||
} | ||
|
||
func TestPut_Overwrite(t *testing.T) { | ||
DracoLi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
db := &Database{} | ||
|
||
height := uint64(1) | ||
originalData := []byte("original data") | ||
updatedData := []byte("updated data") | ||
|
||
// Write original block | ||
require.NoError(t, db.Put(height, originalData)) | ||
|
||
// Overwrite with new data | ||
require.NoError(t, db.Put(height, updatedData)) | ||
|
||
// Verify updated data | ||
retrievedBlock, err := db.Get(height) | ||
require.NoError(t, err) | ||
require.Equal(t, updatedData, retrievedBlock) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.