Skip to content

Commit

Permalink
blockchain: change nodesbackend deletion
Browse files Browse the repository at this point in the history
For NodesBackend, the cached entry is not removed from the cache even
if it's being deleted as subsiquent calls would be made to fetch the
key. Caching it as removed saves on disk reads and on flushes, the leaf
is not attempted to be flushed if it's marked as fresh.
  • Loading branch information
kcalvinalvin committed Aug 26, 2024
1 parent 934f6cb commit f097be2
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion blockchain/utreexoio.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ func NodesBackendDelete(tx *leveldb.Transaction, k uint64) error {
// Delete removes the given key from the underlying map. No-op if the key
// doesn't exist.
func (m *NodesBackEnd) Delete(k uint64) {
// Don't delete as the same key may get called to be removed multiple times.
// Cache it as removed so that we don't call expensive flushes on keys that
// are not in the database.
leaf, _ := m.cache.Get(k)
l := utreexobackends.CachedLeaf{
Leaf: leaf.Leaf,
Expand Down Expand Up @@ -254,7 +257,7 @@ func (m *NodesBackEnd) UsageStats() (int64, int64) {
// flush saves all the cached entries to disk and resets the cache map.
func (m *NodesBackEnd) Flush(ldbTx *leveldb.Transaction) error {
err := m.cache.ForEach(func(k uint64, v utreexobackends.CachedLeaf) error {
if v.IsRemoved() {
if v.IsRemoved() && !v.IsFresh() {
err := NodesBackendDelete(ldbTx, k)
if err != nil {
return err
Expand Down

0 comments on commit f097be2

Please sign in to comment.