mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-23 12:07:17 +00:00
231e468e19
git-subtree-dir: erigon-lib git-subtree-mainline:3c8cbda809
git-subtree-split:93d9c9d9fe
59 lines
1.2 KiB
Go
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
|
|
}
|