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)
}
batch := txindex.NewBatch()
batch := txindex.NewBatch(block.NumTxs)
for _, r := range txResults {
batch.Add(*r)
}

View File

@ -33,13 +33,15 @@ type Batch struct {
}
// NewBatch creates a new Batch.
func NewBatch() *Batch {
return &Batch{}
func NewBatch(n int) *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 {
b.Ops = append(b.Ops, result)
b.Ops[result.Index] = result
return nil
}

View File

@ -17,10 +17,10 @@ func TestTxIndex(t *testing.T) {
indexer := &TxIndex{store: db.NewMemDB()}
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()
batch := txindex.NewBatch()
batch := txindex.NewBatch(1)
batch.Add(*txResult)
err := indexer.AddBatch(batch)
require.Nil(t, err)
@ -32,7 +32,7 @@ func TestTxIndex(t *testing.T) {
func benchmarkTxIndex(txsCount int, b *testing.B) {
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")
if err != nil {
@ -43,8 +43,9 @@ func benchmarkTxIndex(txsCount int, b *testing.B) {
store := db.NewDB("tx_indexer", "leveldb", dir)
indexer := &TxIndex{store: store}
batch := txindex.NewBatch()
batch := txindex.NewBatch(txsCount)
for i := 0; i < txsCount; i++ {
txResult.Index += 1
batch.Add(*txResult)
}