Browse Source

state/txindex and pkg per indexer impl

pull/412/head
Ethan Buchman 7 years ago
parent
commit
d572bb0c5d
11 changed files with 122 additions and 117 deletions
  1. +3
    -3
      state/execution.go
  2. +3
    -3
      state/execution_test.go
  3. +5
    -5
      state/state.go
  4. +0
    -21
      state/tx/indexer.go
  5. +0
    -32
      state/tx/indexer/batch.go
  6. +0
    -6
      state/tx/indexer/error.go
  7. +0
    -19
      state/tx/indexer/null.go
  8. +60
    -0
      state/txindex/indexer.go
  9. +14
    -13
      state/txindex/kv/kv.go
  10. +16
    -15
      state/txindex/kv/kv_test.go
  11. +21
    -0
      state/txindex/null/null.go

+ 3
- 3
state/execution.go View File

@ -9,7 +9,7 @@ import (
. "github.com/tendermint/go-common"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tendermint/proxy"
txindexer "github.com/tendermint/tendermint/state/tx/indexer"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
)
@ -241,12 +241,12 @@ func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConn
return fmt.Errorf("Commit failed for application: %v", err)
}
batch := txindexer.NewBatch()
batch := txindex.NewBatch()
for i, r := range txResults {
tx := block.Txs[i]
batch.Index(tx.Hash(), *r)
}
s.TxIndexer.Batch(batch)
s.TxIndexer.AddBatch(batch)
return nil
}


+ 3
- 3
state/execution_test.go View File

@ -11,7 +11,7 @@ import (
cfg "github.com/tendermint/tendermint/config/tendermint_test"
"github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/proxy"
txindexer "github.com/tendermint/tendermint/state/tx/indexer"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
)
@ -81,10 +81,10 @@ type dummyIndexer struct {
Indexed int
}
func (indexer *dummyIndexer) Tx(hash []byte) (*types.TxResult, error) {
func (indexer *dummyIndexer) Get(hash []byte) (*types.TxResult, error) {
return nil, nil
}
func (indexer *dummyIndexer) Batch(batch *txindexer.Batch) error {
func (indexer *dummyIndexer) AddBatch(batch *txindex.Batch) error {
indexer.Indexed += batch.Size()
return nil
}

+ 5
- 5
state/state.go View File

@ -10,8 +10,8 @@ import (
cfg "github.com/tendermint/go-config"
dbm "github.com/tendermint/go-db"
"github.com/tendermint/go-wire"
"github.com/tendermint/tendermint/state/tx"
txindexer "github.com/tendermint/tendermint/state/tx/indexer"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/state/txindex/null"
"github.com/tendermint/tendermint/types"
)
@ -41,7 +41,7 @@ type State struct {
// AppHash is updated after Commit
AppHash []byte
TxIndexer tx.Indexer `json:"-"` // Transaction indexer.
TxIndexer txindex.TxIndexer `json:"-"` // Transaction indexer.
}
func LoadState(db dbm.DB) *State {
@ -49,7 +49,7 @@ func LoadState(db dbm.DB) *State {
}
func loadState(db dbm.DB, key []byte) *State {
s := &State{db: db, TxIndexer: &txindexer.Null{}}
s := &State{db: db, TxIndexer: &null.TxIndex{}}
buf := db.Get(key)
if len(buf) == 0 {
return nil
@ -188,6 +188,6 @@ func MakeGenesisState(db dbm.DB, genDoc *types.GenesisDoc) *State {
Validators: types.NewValidatorSet(validators),
LastValidators: types.NewValidatorSet(nil),
AppHash: genDoc.AppHash,
TxIndexer: &txindexer.Null{}, // we do not need indexer during replay and in tests
TxIndexer: &null.TxIndex{}, // we do not need indexer during replay and in tests
}
}

+ 0
- 21
state/tx/indexer.go View File

@ -1,21 +0,0 @@
package tx
import (
txindexer "github.com/tendermint/tendermint/state/tx/indexer"
"github.com/tendermint/tendermint/types"
)
// Indexer interface defines methods to index and search transactions.
type Indexer interface {
// Batch analyzes, indexes or stores a batch of transactions.
//
// NOTE We do not specify Index method for analyzing a single transaction
// here because it bears heavy perfomance loses. Almost all advanced indexers
// support batching.
Batch(b *txindexer.Batch) error
// Tx returns specified transaction or nil if the transaction is not indexed
// or stored.
Tx(hash []byte) (*types.TxResult, error)
}

+ 0
- 32
state/tx/indexer/batch.go View File

@ -1,32 +0,0 @@
package indexer
import "github.com/tendermint/tendermint/types"
// A Batch groups together multiple Index operations you would like performed
// at the same time. The Batch structure is NOT thread-safe. You should only
// perform operations on a batch from a single thread at a time. Once batch
// execution has started, you may not modify it.
type Batch struct {
Ops map[string]types.TxResult
}
// NewBatch creates a new Batch.
func NewBatch() *Batch {
return &Batch{
Ops: make(map[string]types.TxResult),
}
}
// Index adds or updates entry for the given hash.
func (b *Batch) Index(hash []byte, result types.TxResult) error {
if len(hash) == 0 {
return ErrorEmptyHash
}
b.Ops[string(hash)] = result
return nil
}
// Size returns the total number of operations inside the batch.
func (b *Batch) Size() int {
return len(b.Ops)
}

+ 0
- 6
state/tx/indexer/error.go View File

@ -1,6 +0,0 @@
package indexer
import "errors"
// ErrorEmptyHash indicates empty hash
var ErrorEmptyHash = errors.New("Transaction hash cannot be empty")

+ 0
- 19
state/tx/indexer/null.go View File

@ -1,19 +0,0 @@
package indexer
import (
"errors"
"github.com/tendermint/tendermint/types"
)
// Null acts as a /dev/null.
type Null struct{}
// Tx panics.
func (indexer *Null) Tx(hash []byte) (*types.TxResult, error) {
return nil, errors.New(`Indexing is disabled (set 'tx_indexer = "kv"' in config)`)
}
// Batch returns nil.
func (indexer *Null) Batch(batch *Batch) error {
return nil
}

+ 60
- 0
state/txindex/indexer.go View File

@ -0,0 +1,60 @@
package txindex
import (
"errors"
"github.com/tendermint/tendermint/types"
)
// Indexer interface defines methods to index and search transactions.
type TxIndexer interface {
// Batch analyzes, indexes or stores a batch of transactions.
//
// NOTE We do not specify Index method for analyzing a single transaction
// here because it bears heavy perfomance loses. Almost all advanced indexers
// support batching.
AddBatch(b *Batch) error
// Tx returns specified transaction or nil if the transaction is not indexed
// or stored.
Get(hash []byte) (*types.TxResult, error)
}
//----------------------------------------------------
// Txs are written as a batch
// A Batch groups together multiple Index operations you would like performed
// at the same time. The Batch structure is NOT thread-safe. You should only
// perform operations on a batch from a single thread at a time. Once batch
// execution has started, you may not modify it.
type Batch struct {
Ops map[string]types.TxResult
}
// NewBatch creates a new Batch.
func NewBatch() *Batch {
return &Batch{
Ops: make(map[string]types.TxResult),
}
}
// Index adds or updates entry for the given hash.
func (b *Batch) Index(hash []byte, result types.TxResult) error {
if len(hash) == 0 {
return ErrorEmptyHash
}
b.Ops[string(hash)] = result
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")

state/tx/indexer/kv.go → state/txindex/kv/kv.go View File


state/tx/indexer/kv_test.go → state/txindex/kv/kv_test.go View File


+ 21
- 0
state/txindex/null/null.go View File

@ -0,0 +1,21 @@
package null
import (
"errors"
"github.com/tendermint/tendermint/state/txindex"
"github.com/tendermint/tendermint/types"
)
// TxIndex acts as a /dev/null.
type TxIndex struct{}
// Tx panics.
func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
return nil, errors.New(`Indexing is disabled (set 'tx_indexer = "kv"' in config)`)
}
// Batch returns nil.
func (txi *TxIndex) AddBatch(batch *txindex.Batch) error {
return nil
}

Loading…
Cancel
Save