github.com/okex/exchain@v1.8.0/libs/tendermint/state/txindex/indexer.go (about)

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