lib/db: Flush batch based on size and refactor (fixes #5531) (#5536)

Flush the batch when exceeding a certain size, instead of when reaching a number
of batched operations.
Move batch to lowlevel to be able to use it in NamespacedKV.
Increase the leveldb memory buffer from 4 to 16 MiB.
This commit is contained in:
Simon Frei
2019-02-15 00:15:13 +01:00
committed by Audrius Butkevicius
parent e2204d0071
commit ca3ae64bbf
6 changed files with 55 additions and 57 deletions

View File

@@ -19,7 +19,8 @@ import (
const (
dbMaxOpenFiles = 100
dbWriteBuffer = 4 << 20
dbWriteBuffer = 16 << 20
dbFlushBatch = dbWriteBuffer / 4 // Some leeway for any leveldb in-memory optimizations
)
// Lowlevel is the lowest level database interface. It has a very simple
@@ -127,3 +128,29 @@ func leveldbIsCorrupted(err error) bool {
return false
}
type batch struct {
*leveldb.Batch
db *Lowlevel
}
func (db *Lowlevel) newBatch() *batch {
return &batch{
Batch: new(leveldb.Batch),
db: db,
}
}
// checkFlush flushes and resets the batch if its size exceeds dbFlushBatch.
func (b *batch) checkFlush() {
if len(b.Dump()) > dbFlushBatch {
b.flush()
b.Reset()
}
}
func (b *batch) flush() {
if err := b.db.Write(b.Batch, nil); err != nil {
panic(err)
}
}