You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

  1. package txindex
  2. import (
  3. "errors"
  4. "github.com/tendermint/tendermint/types"
  5. )
  6. // Indexer interface defines methods to index and search transactions.
  7. type TxIndexer interface {
  8. // Batch analyzes, indexes or stores a batch of transactions.
  9. //
  10. // NOTE We do not specify Index method for analyzing a single transaction
  11. // here because it bears heavy perfomance loses. Almost all advanced indexers
  12. // support batching.
  13. AddBatch(b *Batch) error
  14. // Tx returns specified transaction or nil if the transaction is not indexed
  15. // or stored.
  16. Get(hash []byte) (*types.TxResult, error)
  17. }
  18. //----------------------------------------------------
  19. // Txs are written as a batch
  20. // A Batch groups together multiple Index operations you would like performed
  21. // at the same time. The Batch structure is NOT thread-safe. You should only
  22. // perform operations on a batch from a single thread at a time. Once batch
  23. // execution has started, you may not modify it.
  24. type Batch struct {
  25. Ops []types.TxResult
  26. }
  27. // NewBatch creates a new Batch.
  28. func NewBatch(n int) *Batch {
  29. return &Batch{
  30. Ops: make([]types.TxResult, n),
  31. }
  32. }
  33. // Index adds or updates entry for the given result.Index.
  34. func (b *Batch) Add(result types.TxResult) error {
  35. b.Ops[result.Index] = result
  36. return nil
  37. }
  38. // Size returns the total number of operations inside the batch.
  39. func (b *Batch) Size() int {
  40. return len(b.Ops)
  41. }
  42. //----------------------------------------------------
  43. // Errors
  44. // ErrorEmptyHash indicates empty hash
  45. var ErrorEmptyHash = errors.New("Transaction hash cannot be empty")