tendermint/state/txindex/indexer.go

59 lines
1.5 KiB
Go
Raw Normal View History

2017-04-18 16:29:02 -07:00
package txindex
import (
"errors"
"github.com/tendermint/tendermint/types"
2017-11-28 12:12:04 -08:00
"github.com/tendermint/tmlibs/pubsub/query"
2017-04-18 16:29:02 -07:00
)
// TxIndexer interface defines methods to index and search transactions.
2017-04-18 16:29:02 -07:00
type TxIndexer interface {
// AddBatch analyzes, indexes and stores a batch of transactions.
2017-11-28 16:58:39 -08:00
AddBatch(b *Batch) error
// Index analyzes, indexes and stores a single transaction.
2017-11-28 16:58:39 -08:00
Index(result *types.TxResult) error
2017-04-18 16:29:02 -07:00
2017-10-23 08:05:20 -07:00
// Get returns the transaction specified by hash or nil if the transaction is not indexed
2017-04-18 16:29:02 -07:00
// or stored.
Get(hash []byte) (*types.TxResult, error)
2017-11-28 12:12:04 -08:00
// Search allows you to query for transactions.
Search(q *query.Query) ([]*types.TxResult, error)
2017-04-18 16:29:02 -07:00
}
//----------------------------------------------------
// Txs are written as a batch
2017-10-17 00:59:05 -07:00
// Batch groups together multiple Index operations to be performed at the same time.
// NOTE: Batch is NOT thread-safe and must not be modified after starting its execution.
2017-04-18 16:29:02 -07:00
type Batch struct {
2017-11-26 17:16:21 -08:00
Ops []*types.TxResult
2017-04-18 16:29:02 -07:00
}
// NewBatch creates a new Batch.
func NewBatch(n int) *Batch {
return &Batch{
2017-11-26 17:16:21 -08:00
Ops: make([]*types.TxResult, n),
}
2017-04-18 16:29:02 -07:00
}
// Add or update an entry for the given result.Index.
2017-11-26 17:16:21 -08:00
func (b *Batch) Add(result *types.TxResult) error {
b.Ops[result.Index] = result
2017-04-18 16:29:02 -07:00
return nil
}
// Size returns the total number of operations inside the batch.
func (b *Batch) Size() int {
return len(b.Ops)
}
//----------------------------------------------------
// Errors
// ErrorEmptyHash indicates empty hash
var ErrorEmptyHash = errors.New("Transaction hash cannot be empty")