github.com/Finschia/ostracon@v1.1.5/state/txindex/indexer.go (about)

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