diff --git a/ethdb/database.go b/ethdb/database.go index 992968fdef..93755dd7e3 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -280,12 +280,14 @@ func (db *LDBDatabase) NewBatch() Batch { } type ldbBatch struct { - db *leveldb.DB - b *leveldb.Batch + db *leveldb.DB + b *leveldb.Batch + size int } func (b *ldbBatch) Put(key, value []byte) error { b.b.Put(key, value) + b.size += len(value) return nil } @@ -293,6 +295,10 @@ func (b *ldbBatch) Write() error { return b.db.Write(b.b, nil) } +func (b *ldbBatch) ValueSize() int { + return b.size +} + type table struct { db Database prefix string @@ -348,3 +354,7 @@ func (tb *tableBatch) Put(key, value []byte) error { func (tb *tableBatch) Write() error { return tb.batch.Write() } + +func (tb *tableBatch) ValueSize() int { + return tb.batch.ValueSize() +} diff --git a/ethdb/interface.go b/ethdb/interface.go index 6a61289bff..99a5b770d5 100644 --- a/ethdb/interface.go +++ b/ethdb/interface.go @@ -39,5 +39,6 @@ type Database interface { // when Write is called. Batch cannot be used concurrently. type Batch interface { Putter + ValueSize() int // amount of data in the batch Write() error } diff --git a/ethdb/memory_database.go b/ethdb/memory_database.go index 7f91220305..916f88e6e1 100644 --- a/ethdb/memory_database.go +++ b/ethdb/memory_database.go @@ -101,10 +101,12 @@ type kv struct{ k, v []byte } type memBatch struct { db *MemDatabase writes []kv + size int } func (b *memBatch) Put(key, value []byte) error { b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)}) + b.size += len(value) return nil } @@ -117,3 +119,7 @@ func (b *memBatch) Write() error { } return nil } + +func (b *memBatch) ValueSize() int { + return b.size +}