mirror of https://github.com/status-im/op-geth.git
ethdb: add NewBatch
This commit is contained in:
parent
8c4dab77ba
commit
8b32f10f16
|
@ -268,3 +268,23 @@ func (self *LDBDatabase) meter(refresh time.Duration) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: remove this stuff and expose leveldb directly
|
||||
|
||||
func (db *LDBDatabase) NewBatch() Batch {
|
||||
return &ldbBatch{db: db.db, b: new(leveldb.Batch)}
|
||||
}
|
||||
|
||||
type ldbBatch struct {
|
||||
db *leveldb.DB
|
||||
b *leveldb.Batch
|
||||
}
|
||||
|
||||
func (b *ldbBatch) Put(key, value []byte) error {
|
||||
b.b.Put(key, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *ldbBatch) Write() error {
|
||||
return b.db.Write(b.b, nil)
|
||||
}
|
||||
|
|
|
@ -22,4 +22,10 @@ type Database interface {
|
|||
Delete(key []byte) error
|
||||
Close()
|
||||
Flush() error
|
||||
NewBatch() Batch
|
||||
}
|
||||
|
||||
type Batch interface {
|
||||
Put(key, value []byte) error
|
||||
Write() error
|
||||
}
|
||||
|
|
|
@ -95,3 +95,26 @@ func (db *MemDatabase) LastKnownTD() []byte {
|
|||
func (db *MemDatabase) Flush() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *MemDatabase) NewBatch() Batch {
|
||||
return &memBatch{db: db}
|
||||
}
|
||||
|
||||
type kv struct{ k, v []byte }
|
||||
|
||||
type memBatch struct {
|
||||
db *MemDatabase
|
||||
writes []kv
|
||||
}
|
||||
|
||||
func (w *memBatch) Put(key, value []byte) error {
|
||||
w.writes = append(w.writes, kv{key, common.CopyBytes(value)})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *memBatch) Write() error {
|
||||
for _, kv := range w.writes {
|
||||
w.db.db[string(kv.k)] = kv.v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue