Skip to content

Commit 236147b

Browse files
ethdb: refactor Database interface (#30693)
1 parent 7180d26 commit 236147b

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

core/rawdb/accessors_indexes_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,25 @@ var newTestHasher = blocktest.NewHasher
3535
func TestLookupStorage(t *testing.T) {
3636
tests := []struct {
3737
name string
38-
writeTxLookupEntriesByBlock func(ethdb.Writer, *types.Block)
38+
writeTxLookupEntriesByBlock func(ethdb.KeyValueWriter, *types.Block)
3939
}{
4040
{
4141
"DatabaseV6",
42-
func(db ethdb.Writer, block *types.Block) {
42+
func(db ethdb.KeyValueWriter, block *types.Block) {
4343
WriteTxLookupEntriesByBlock(db, block)
4444
},
4545
},
4646
{
4747
"DatabaseV4-V5",
48-
func(db ethdb.Writer, block *types.Block) {
48+
func(db ethdb.KeyValueWriter, block *types.Block) {
4949
for _, tx := range block.Transactions() {
5050
db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes())
5151
}
5252
},
5353
},
5454
{
5555
"DatabaseV3",
56-
func(db ethdb.Writer, block *types.Block) {
56+
func(db ethdb.KeyValueWriter, block *types.Block) {
5757
for index, tx := range block.Transactions() {
5858
entry := LegacyTxLookupEntry{
5959
BlockHash: block.Hash(),

core/rawdb/freezer.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ const freezerTableSize = 2 * 1000 * 1000 * 1000
5858
// - The append-only nature ensures that disk writes are minimized.
5959
// - The in-order data ensures that disk reads are always optimized.
6060
type Freezer struct {
61-
frozen atomic.Uint64 // Number of items already frozen
62-
tail atomic.Uint64 // Number of the first stored item in the freezer
61+
datadir string
62+
frozen atomic.Uint64 // Number of items already frozen
63+
tail atomic.Uint64 // Number of the first stored item in the freezer
6364

6465
// This lock synchronizes writers and the truncate operation, as well as
6566
// the "atomic" (batched) read operations.
@@ -109,6 +110,7 @@ func NewFreezer(datadir string, namespace string, readonly bool, maxTableSize ui
109110
}
110111
// Open all the supported data tables
111112
freezer := &Freezer{
113+
datadir: datadir,
112114
readonly: readonly,
113115
tables: make(map[string]*freezerTable),
114116
instanceLock: lock,
@@ -172,6 +174,11 @@ func (f *Freezer) Close() error {
172174
return nil
173175
}
174176

177+
// AncientDatadir returns the path of the ancient store.
178+
func (f *Freezer) AncientDatadir() (string, error) {
179+
return f.datadir, nil
180+
}
181+
175182
// HasAncient returns an indicator whether the specified ancient data exists
176183
// in the freezer.
177184
func (f *Freezer) HasAncient(kind string, number uint64) (bool, error) {

core/rawdb/freezer_memory.go

+6
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,9 @@ func (f *MemoryFreezer) Reset() error {
419419
f.items, f.tail = 0, 0
420420
return nil
421421
}
422+
423+
// AncientDatadir returns the path of the ancient store.
424+
// Since the memory freezer is ephemeral, an empty string is returned.
425+
func (f *MemoryFreezer) AncientDatadir() (string, error) {
426+
return "", nil
427+
}

core/rawdb/freezer_resettable.go

+8
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ func (f *resettableFreezer) Sync() error {
202202
return f.freezer.Sync()
203203
}
204204

205+
// AncientDatadir returns the path of the ancient store.
206+
func (f *resettableFreezer) AncientDatadir() (string, error) {
207+
f.lock.RLock()
208+
defer f.lock.RUnlock()
209+
210+
return f.freezer.AncientDatadir()
211+
}
212+
205213
// cleanup removes the directory located in the specified path
206214
// has the name with deletion marker suffix.
207215
func cleanup(path string) error {

ethdb/database.go

+3-22
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,12 @@ type Reader interface {
162162
AncientReader
163163
}
164164

165-
// Writer contains the methods required to write data to both key-value as well as
166-
// immutable ancient data.
167-
type Writer interface {
168-
KeyValueWriter
169-
KeyValueRangeDeleter
170-
AncientWriter
171-
}
172-
173-
// Stater contains the methods required to retrieve states from both key-value as well as
174-
// immutable ancient data.
175-
type Stater interface {
176-
KeyValueStater
177-
AncientStater
178-
}
179-
180165
// AncientStore contains all the methods required to allow handling different
181166
// ancient data stores backing immutable data store.
182167
type AncientStore interface {
183168
AncientReader
184169
AncientWriter
170+
AncientStater
185171
io.Closer
186172
}
187173

@@ -196,11 +182,6 @@ type ResettableAncientStore interface {
196182
// Database contains all the methods required by the high level database to not
197183
// only access the key-value data store but also the ancient chain store.
198184
type Database interface {
199-
Reader
200-
Writer
201-
Batcher
202-
Iteratee
203-
Stater
204-
Compacter
205-
io.Closer
185+
KeyValueStore
186+
AncientStore
206187
}

0 commit comments

Comments
 (0)