github.com/koko1123/flow-go-1@v0.29.6/storage/badger/operation/modifiers.go (about)

     1  package operation
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/dgraph-io/badger/v3"
     7  
     8  	"github.com/koko1123/flow-go-1/module/metrics"
     9  	"github.com/koko1123/flow-go-1/storage"
    10  	"github.com/koko1123/flow-go-1/storage/badger/transaction"
    11  )
    12  
    13  func SkipDuplicates(op func(*badger.Txn) error) func(tx *badger.Txn) error {
    14  	return func(tx *badger.Txn) error {
    15  		err := op(tx)
    16  		if errors.Is(err, storage.ErrAlreadyExists) {
    17  			metrics.GetStorageCollector().SkipDuplicate()
    18  			return nil
    19  		}
    20  		return err
    21  	}
    22  }
    23  
    24  func SkipNonExist(op func(*badger.Txn) error) func(tx *badger.Txn) error {
    25  	return func(tx *badger.Txn) error {
    26  		err := op(tx)
    27  		if errors.Is(err, badger.ErrKeyNotFound) {
    28  			return nil
    29  		}
    30  		if errors.Is(err, storage.ErrNotFound) {
    31  			return nil
    32  		}
    33  		return err
    34  	}
    35  }
    36  
    37  func RetryOnConflict(action func(func(*badger.Txn) error) error, op func(tx *badger.Txn) error) error {
    38  	for {
    39  		err := action(op)
    40  		if errors.Is(err, badger.ErrConflict) {
    41  			metrics.GetStorageCollector().RetryOnConflict()
    42  			continue
    43  		}
    44  		return err
    45  	}
    46  }
    47  
    48  func RetryOnConflictTx(db *badger.DB, action func(*badger.DB, func(*transaction.Tx) error) error, op func(*transaction.Tx) error) error {
    49  	for {
    50  		err := action(db, op)
    51  		if errors.Is(err, badger.ErrConflict) {
    52  			metrics.GetStorageCollector().RetryOnConflict()
    53  			continue
    54  		}
    55  		return err
    56  	}
    57  }