erigon-pulse/erigon-lib/kv/memdb/memory_mutation_diff.go
battlmonstr 231e468e19 Add 'erigon-lib/' from commit '93d9c9d9fe4bd8a49f7a98a6bce0f0da7094c7d3'
git-subtree-dir: erigon-lib
git-subtree-mainline: 3c8cbda809
git-subtree-split: 93d9c9d9fe
2023-09-20 14:50:25 +02:00

59 lines
1.2 KiB
Go

package memdb
import "github.com/ledgerwatch/erigon-lib/kv"
type entry struct {
k []byte
v []byte
}
type MemoryDiff struct {
diff map[table][]entry // god.
deletedEntries map[string][]string
clearedTableNames []string
}
type table struct {
name string
dupsort bool
}
func (m *MemoryDiff) Flush(tx kv.RwTx) error {
// Obliterate buckets who are to be deleted
for _, bucket := range m.clearedTableNames {
if err := tx.ClearBucket(bucket); err != nil {
return err
}
}
// Obliterate entries who are to be deleted
for bucket, keys := range m.deletedEntries {
for _, key := range keys {
if err := tx.Delete(bucket, []byte(key)); err != nil {
return err
}
}
}
// Iterate over each bucket and apply changes accordingly.
for bucketInfo, bucketDiff := range m.diff {
if bucketInfo.dupsort {
dbCursor, err := tx.RwCursorDupSort(bucketInfo.name)
if err != nil {
return err
}
defer dbCursor.Close()
for _, entry := range bucketDiff {
if err := dbCursor.Put(entry.k, entry.v); err != nil {
return err
}
}
} else {
for _, entry := range bucketDiff {
if err := tx.Put(bucketInfo.name, entry.k, entry.v); err != nil {
return err
}
}
}
}
return nil
}