NewBatch takes size, batch.Add doesn't use append

This commit is contained in:
Ethan Buchman
2017-04-18 20:23:58 -04:00
parent b6a04a3456
commit 45876d0828
3 changed files with 12 additions and 9 deletions

View File

@ -246,7 +246,7 @@ func (s *State) ApplyBlock(eventCache types.Fireable, proxyAppConn proxy.AppConn
return fmt.Errorf("Commit failed for application: %v", err) return fmt.Errorf("Commit failed for application: %v", err)
} }
batch := txindex.NewBatch() batch := txindex.NewBatch(block.NumTxs)
for _, r := range txResults { for _, r := range txResults {
batch.Add(*r) batch.Add(*r)
} }

View File

@ -33,13 +33,15 @@ type Batch struct {
} }
// NewBatch creates a new Batch. // NewBatch creates a new Batch.
func NewBatch() *Batch { func NewBatch(n int) *Batch {
return &Batch{} return &Batch{
Ops: make([]types.TxResult, n),
}
} }
// Index adds or updates entry for the given hash. // Index adds or updates entry for the given result.Index.
func (b *Batch) Add(result types.TxResult) error { func (b *Batch) Add(result types.TxResult) error {
b.Ops = append(b.Ops, result) b.Ops[result.Index] = result
return nil return nil
} }

View File

@ -17,10 +17,10 @@ func TestTxIndex(t *testing.T) {
indexer := &TxIndex{store: db.NewMemDB()} indexer := &TxIndex{store: db.NewMemDB()}
tx := types.Tx("HELLO WORLD") tx := types.Tx("HELLO WORLD")
txResult := &types.TxResult{1, 1, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}} txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}}
hash := tx.Hash() hash := tx.Hash()
batch := txindex.NewBatch() batch := txindex.NewBatch(1)
batch.Add(*txResult) batch.Add(*txResult)
err := indexer.AddBatch(batch) err := indexer.AddBatch(batch)
require.Nil(t, err) require.Nil(t, err)
@ -32,7 +32,7 @@ func TestTxIndex(t *testing.T) {
func benchmarkTxIndex(txsCount int, b *testing.B) { func benchmarkTxIndex(txsCount int, b *testing.B) {
tx := types.Tx("HELLO WORLD") tx := types.Tx("HELLO WORLD")
txResult := &types.TxResult{1, 1, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}} txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: ""}}
dir, err := ioutil.TempDir("", "tx_indexer_db") dir, err := ioutil.TempDir("", "tx_indexer_db")
if err != nil { if err != nil {
@ -43,8 +43,9 @@ func benchmarkTxIndex(txsCount int, b *testing.B) {
store := db.NewDB("tx_indexer", "leveldb", dir) store := db.NewDB("tx_indexer", "leveldb", dir)
indexer := &TxIndex{store: store} indexer := &TxIndex{store: store}
batch := txindex.NewBatch() batch := txindex.NewBatch(txsCount)
for i := 0; i < txsCount; i++ { for i := 0; i < txsCount; i++ {
txResult.Index += 1
batch.Add(*txResult) batch.Add(*txResult)
} }