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 {
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()
}

View file

@ -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
}

View file

@ -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
}