ethdb: add ValueSize to Batch

This commit is contained in:
Felix Lange 2017-09-07 14:10:34 +02:00
parent 97beadd4fc
commit 1782adb95e
3 changed files with 19 additions and 2 deletions

View file

@ -280,12 +280,14 @@ func (db *LDBDatabase) NewBatch() Batch {
} }
type ldbBatch struct { type ldbBatch struct {
db *leveldb.DB db *leveldb.DB
b *leveldb.Batch b *leveldb.Batch
size int
} }
func (b *ldbBatch) Put(key, value []byte) error { func (b *ldbBatch) Put(key, value []byte) error {
b.b.Put(key, value) b.b.Put(key, value)
b.size += len(value)
return nil return nil
} }
@ -293,6 +295,10 @@ func (b *ldbBatch) Write() error {
return b.db.Write(b.b, nil) return b.db.Write(b.b, nil)
} }
func (b *ldbBatch) ValueSize() int {
return b.size
}
type table struct { type table struct {
db Database db Database
prefix string prefix string
@ -348,3 +354,7 @@ func (tb *tableBatch) Put(key, value []byte) error {
func (tb *tableBatch) Write() error { func (tb *tableBatch) Write() error {
return tb.batch.Write() return tb.batch.Write()
} }
func (tb *tableBatch) ValueSize() int {
return tb.batch.ValueSize()
}

View file

@ -39,5 +39,6 @@ type Database interface {
// when Write is called. Batch cannot be used concurrently. // when Write is called. Batch cannot be used concurrently.
type Batch interface { type Batch interface {
Putter Putter
ValueSize() int // amount of data in the batch
Write() error Write() error
} }

View file

@ -101,10 +101,12 @@ type kv struct{ k, v []byte }
type memBatch struct { type memBatch struct {
db *MemDatabase db *MemDatabase
writes []kv writes []kv
size int
} }
func (b *memBatch) Put(key, value []byte) error { func (b *memBatch) Put(key, value []byte) error {
b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)}) b.writes = append(b.writes, kv{common.CopyBytes(key), common.CopyBytes(value)})
b.size += len(value)
return nil return nil
} }
@ -117,3 +119,7 @@ func (b *memBatch) Write() error {
} }
return nil return nil
} }
func (b *memBatch) ValueSize() int {
return b.size
}