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.

54 lines
1.4 KiB

7 years ago
  1. package txindex
  2. import (
  3. "errors"
  4. "github.com/tendermint/tendermint/types"
  5. )
  6. // TxIndexer interface defines methods to index and search transactions.
  7. type TxIndexer interface {
  8. // AddBatch analyzes, indexes or stores a batch of transactions.
  9. // NOTE: We do not specify Index method for analyzing a single transaction
  10. // here because it bears heavy perfomance loses. Almost all advanced indexers
  11. // support batching.
  12. AddBatch(b *Batch) 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. }
  17. //----------------------------------------------------
  18. // Txs are written as a batch
  19. // Batch groups together multiple Index operations to be performed at the same time.
  20. // NOTE: Batch is NOT thread-safe and must not be modified after starting its execution.
  21. type Batch struct {
  22. Ops []types.TxResult
  23. }
  24. // NewBatch creates a new Batch.
  25. func NewBatch(n int) *Batch {
  26. return &Batch{
  27. Ops: make([]types.TxResult, n),
  28. }
  29. }
  30. // Add or update an entry for the given result.Index.
  31. func (b *Batch) Add(result types.TxResult) error {
  32. b.Ops[result.Index] = result
  33. return nil
  34. }
  35. // Size returns the total number of operations inside the batch.
  36. func (b *Batch) Size() int {
  37. return len(b.Ops)
  38. }
  39. //----------------------------------------------------
  40. // Errors
  41. // ErrorEmptyHash indicates empty hash
  42. var ErrorEmptyHash = errors.New("Transaction hash cannot be empty")