core, ethdb, tests, trie: implement NewBatchWithSize API for batcher #24392 (#1085)

This commit is contained in:
Daniel Liu 2025-06-17 13:26:50 +08:00 committed by GitHub
parent bebc87e2f0
commit f07824db20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 40 additions and 0 deletions

View file

@ -39,6 +39,7 @@ type XDCXDAO interface {
Has(key []byte) (bool, error)
Delete(key []byte) error
NewBatch() ethdb.Batch
NewBatchWithSize(size int) ethdb.Batch
HasAncient(kind string, number uint64) (bool, error)
Ancient(kind string, number uint64) ([]byte, error)
Ancients() (uint64, error)

View file

@ -103,6 +103,10 @@ func (db *BatchDatabase) NewBatch() ethdb.Batch {
return db.db.NewBatch()
}
func (db *BatchDatabase) NewBatchWithSize(size int) ethdb.Batch {
return db.db.NewBatch()
}
func (db *BatchDatabase) DeleteItemByTxHash(txhash common.Hash, val interface{}) {
}

View file

@ -882,6 +882,10 @@ func (db *MongoDatabase) NewBatch() ethdb.Batch {
return nil
}
func (db *MongoDatabase) NewBatchWithSize(size int) ethdb.Batch {
return nil
}
type keyvalue struct {
key []byte
value []byte

View file

@ -159,6 +159,11 @@ func (t *table) NewBatch() ethdb.Batch {
return &tableBatch{t.db.NewBatch(), t.prefix}
}
// NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
func (t *table) NewBatchWithSize(size int) ethdb.Batch {
return &tableBatch{t.db.NewBatchWithSize(size), t.prefix}
}
// tableBatch is a wrapper around a database batch that prefixes each key access
// with a pre-configured string.
type tableBatch struct {

View file

@ -43,4 +43,7 @@ type Batcher interface {
// NewBatch creates a write-only database that buffers changes to its host db
// until a final write is called.
NewBatch() Batch
// NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
NewBatchWithSize(size int) Batch
}

View file

@ -213,6 +213,14 @@ func (db *Database) NewBatch() ethdb.Batch {
}
}
// NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
func (db *Database) NewBatchWithSize(size int) ethdb.Batch {
return &batch{
db: db.db,
b: leveldb.MakeBatch(size),
}
}
// NewIterator creates a binary-alphabetical iterator over a subset
// of database content with a particular key prefix, starting at a particular
// initial key (or after, if it does not exist).

View file

@ -129,6 +129,13 @@ func (db *Database) NewBatch() ethdb.Batch {
}
}
// NewBatchWithSize creates a write-only database batch with pre-allocated buffer.
func (db *Database) NewBatchWithSize(size int) ethdb.Batch {
return &batch{
db: db,
}
}
// NewIterator creates a binary-alphabetical iterator over a subset
// of database content with a particular key prefix, starting at a particular
// initial key (or after, if it does not exist).

View file

@ -14,6 +14,9 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
//go:build gofuzz
// +build gofuzz
package bn256
import (

View file

@ -471,6 +471,10 @@ func (l *loggingDb) NewBatch() ethdb.Batch {
return l.backend.NewBatch()
}
func (l *loggingDb) NewBatchWithSize(size int) ethdb.Batch {
return l.backend.NewBatchWithSize(size)
}
func (l *loggingDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
fmt.Printf("NewIterator\n")
return l.backend.NewIterator(prefix, start)

View file

@ -589,6 +589,7 @@ func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement
func (s *spongeDb) Get(key []byte) ([]byte, error) { return nil, errors.New("no such elem") }
func (s *spongeDb) Delete(key []byte) error { panic("implement me") }
func (s *spongeDb) NewBatch() ethdb.Batch { return &spongeBatch{s} }
func (s *spongeDb) NewBatchWithSize(size int) ethdb.Batch { return &spongeBatch{s} }
func (s *spongeDb) Stat(property string) (string, error) { panic("implement me") }
func (s *spongeDb) Compact(start []byte, limit []byte) error { panic("implement me") }
func (s *spongeDb) Close() error { return nil }