ethdb: refactor Database interface (#30693)

This commit is contained in:
Marius van der Wijden 2024-10-29 09:32:40 +01:00 committed by Martin HS
parent b7767efed0
commit 3017efedc2
5 changed files with 30 additions and 28 deletions

View File

@ -35,17 +35,17 @@ var newTestHasher = blocktest.NewHasher
func TestLookupStorage(t *testing.T) {
tests := []struct {
name string
writeTxLookupEntriesByBlock func(ethdb.Writer, *types.Block)
writeTxLookupEntriesByBlock func(ethdb.KeyValueWriter, *types.Block)
}{
{
"DatabaseV6",
func(db ethdb.Writer, block *types.Block) {
func(db ethdb.KeyValueWriter, block *types.Block) {
WriteTxLookupEntriesByBlock(db, block)
},
},
{
"DatabaseV4-V5",
func(db ethdb.Writer, block *types.Block) {
func(db ethdb.KeyValueWriter, block *types.Block) {
for _, tx := range block.Transactions() {
db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes())
}
@ -53,7 +53,7 @@ func TestLookupStorage(t *testing.T) {
},
{
"DatabaseV3",
func(db ethdb.Writer, block *types.Block) {
func(db ethdb.KeyValueWriter, block *types.Block) {
for index, tx := range block.Transactions() {
entry := LegacyTxLookupEntry{
BlockHash: block.Hash(),

View File

@ -58,6 +58,7 @@ const freezerTableSize = 2 * 1000 * 1000 * 1000
// - The append-only nature ensures that disk writes are minimized.
// - The in-order data ensures that disk reads are always optimized.
type Freezer struct {
datadir string
frozen atomic.Uint64 // Number of items already frozen
tail atomic.Uint64 // Number of the first stored item in the freezer
@ -109,6 +110,7 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
}
// Open all the supported data tables
freezer := &Freezer{
datadir: datadir,
readonly: readonly,
tables: make(map[string]*freezerTable),
instanceLock: lock,
@ -172,6 +174,11 @@ func (f *Freezer) Close() error {
return nil
}
// AncientDatadir returns the path of the ancient store.
func (f *Freezer) AncientDatadir() (string, error) {
return f.datadir, nil
}
// HasAncient returns an indicator whether the specified ancient data exists
// in the freezer.
func (f *Freezer) HasAncient(kind string, number uint64) (bool, error) {

View File

@ -419,3 +419,9 @@ func (f *MemoryFreezer) Reset() error {
f.items, f.tail = 0, 0
return nil
}
// AncientDatadir returns the path of the ancient store.
// Since the memory freezer is ephemeral, an empty string is returned.
func (f *MemoryFreezer) AncientDatadir() (string, error) {
return "", nil
}

View File

@ -202,6 +202,14 @@ func (f *resettableFreezer) Sync() error {
return f.freezer.Sync()
}
// AncientDatadir returns the path of the ancient store.
func (f *resettableFreezer) AncientDatadir() (string, error) {
f.lock.RLock()
defer f.lock.RUnlock()
return f.freezer.AncientDatadir()
}
// cleanup removes the directory located in the specified path
// has the name with deletion marker suffix.
func cleanup(path string) error {

View File

@ -162,26 +162,12 @@ type Reader interface {
AncientReader
}
// Writer contains the methods required to write data to both key-value as well as
// immutable ancient data.
type Writer interface {
KeyValueWriter
KeyValueRangeDeleter
AncientWriter
}
// Stater contains the methods required to retrieve states from both key-value as well as
// immutable ancient data.
type Stater interface {
KeyValueStater
AncientStater
}
// AncientStore contains all the methods required to allow handling different
// ancient data stores backing immutable data store.
type AncientStore interface {
AncientReader
AncientWriter
AncientStater
io.Closer
}
@ -196,11 +182,6 @@ type ResettableAncientStore interface {
// Database contains all the methods required by the high level database to not
// only access the key-value data store but also the ancient chain store.
type Database interface {
Reader
Writer
Batcher
Iteratee
Stater
Compacter
io.Closer
KeyValueStore
AncientStore
}