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.

58 lines
1.5 KiB

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