Browse Source

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

pull/412/head
Ethan Buchman 8 years ago
parent
commit
45876d0828
3 changed files with 12 additions and 9 deletions
  1. +1
    -1
      state/execution.go
  2. +6
    -4
      state/txindex/indexer.go
  3. +5
    -4
      state/txindex/kv/kv_test.go

+ 1
- 1
state/execution.go 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)
}


+ 6
- 4
state/txindex/indexer.go 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
}


+ 5
- 4
state/txindex/kv/kv_test.go 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)
}


Loading…
Cancel
Save