github.com/evdatsion/aphelion-dpos-bft@v0.32.1/state/txindex/indexer.go (about)

     1  package txindex
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/evdatsion/aphelion-dpos-bft/libs/pubsub/query"
     7  	"github.com/evdatsion/aphelion-dpos-bft/types"
     8  )
     9  
    10  // TxIndexer interface defines methods to index and search transactions.
    11  type TxIndexer interface {
    12  
    13  	// AddBatch analyzes, indexes and stores a batch of transactions.
    14  	AddBatch(b *Batch) error
    15  
    16  	// Index analyzes, indexes and stores a single transaction.
    17  	Index(result *types.TxResult) error
    18  
    19  	// Get returns the transaction specified by hash or nil if the transaction is not indexed
    20  	// or stored.
    21  	Get(hash []byte) (*types.TxResult, error)
    22  
    23  	// Search allows you to query for transactions.
    24  	Search(q *query.Query) ([]*types.TxResult, error)
    25  }
    26  
    27  //----------------------------------------------------
    28  // Txs are written as a batch
    29  
    30  // Batch groups together multiple Index operations to be performed at the same time.
    31  // NOTE: Batch is NOT thread-safe and must not be modified after starting its execution.
    32  type Batch struct {
    33  	Ops []*types.TxResult
    34  }
    35  
    36  // NewBatch creates a new Batch.
    37  func NewBatch(n int64) *Batch {
    38  	return &Batch{
    39  		Ops: make([]*types.TxResult, n),
    40  	}
    41  }
    42  
    43  // Add or update an entry for the given result.Index.
    44  func (b *Batch) Add(result *types.TxResult) error {
    45  	b.Ops[result.Index] = result
    46  	return nil
    47  }
    48  
    49  // Size returns the total number of operations inside the batch.
    50  func (b *Batch) Size() int {
    51  	return len(b.Ops)
    52  }
    53  
    54  //----------------------------------------------------
    55  // Errors
    56  
    57  // ErrorEmptyHash indicates empty hash
    58  var ErrorEmptyHash = errors.New("Transaction hash cannot be empty")