You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

55 lines
1.2 KiB

package indexer
import (
"bytes"
"fmt"
db "github.com/tendermint/go-db"
"github.com/tendermint/go-wire"
"github.com/tendermint/tendermint/types"
)
// KV is a simplest possible indexer, backed by Key-Value storage (levelDB).
// It could only index transaction by its identifier.
type KV struct {
store db.DB
}
// NewKV returns new instance of KV indexer.
func NewKV(store db.DB) *KV {
return &KV{store: store}
}
// Tx gets transaction from the KV storage and returns it or nil if the
// transaction is not found.
func (indexer *KV) Tx(hash []byte) (*types.TxResult, error) {
if len(hash) == 0 {
return nil, ErrorEmptyHash
}
rawBytes := indexer.store.Get(hash)
if rawBytes == nil {
return nil, nil
}
r := bytes.NewReader(rawBytes)
var n int
var err error
txResult := wire.ReadBinary(&types.TxResult{}, r, 0, &n, &err).(*types.TxResult)
if err != nil {
return nil, fmt.Errorf("Error reading TxResult: %v", err)
}
return txResult, nil
}
// Batch writes a batch of transactions into the KV storage.
func (indexer *KV) Batch(b *Batch) error {
storeBatch := indexer.store.NewBatch()
for hash, result := range b.Ops {
rawBytes := wire.BinaryBytes(&result)
storeBatch.Set([]byte(hash), rawBytes)
}
storeBatch.Write()
return nil
}