mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-19 06:31:57 +00:00
update for sdk2 libs. need to fix kv test
NOTE we only updating for tmlibs and abci
This commit is contained in:
+19
-28
@@ -10,13 +10,13 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
dbm "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/pubsub/query"
|
||||
|
||||
"github.com/tendermint/tendermint/state/txindex"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
db "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/pubsub/query"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -27,13 +27,13 @@ var _ txindex.TxIndexer = (*TxIndex)(nil)
|
||||
|
||||
// TxIndex is the simplest possible indexer, backed by key-value storage (levelDB).
|
||||
type TxIndex struct {
|
||||
store db.DB
|
||||
store dbm.DB
|
||||
tagsToIndex []string
|
||||
indexAllTags bool
|
||||
}
|
||||
|
||||
// NewTxIndex creates new KV indexer.
|
||||
func NewTxIndex(store db.DB, options ...func(*TxIndex)) *TxIndex {
|
||||
func NewTxIndex(store dbm.DB, options ...func(*TxIndex)) *TxIndex {
|
||||
txi := &TxIndex{store: store, tagsToIndex: make([]string, 0), indexAllTags: false}
|
||||
for _, o := range options {
|
||||
o(txi)
|
||||
@@ -87,7 +87,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
||||
|
||||
// index tx by tags
|
||||
for _, tag := range result.Result.Tags {
|
||||
if txi.indexAllTags || cmn.StringInSlice(tag.Key, txi.tagsToIndex) {
|
||||
if txi.indexAllTags || cmn.StringInSlice(string(tag.Key), txi.tagsToIndex) {
|
||||
storeBatch.Set(keyForTag(tag, result), hash)
|
||||
}
|
||||
}
|
||||
@@ -109,7 +109,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||
|
||||
// index tx by tags
|
||||
for _, tag := range result.Result.Tags {
|
||||
if txi.indexAllTags || cmn.StringInSlice(tag.Key, txi.tagsToIndex) {
|
||||
if txi.indexAllTags || cmn.StringInSlice(string(tag.Key), txi.tagsToIndex) {
|
||||
b.Set(keyForTag(tag, result), hash)
|
||||
}
|
||||
}
|
||||
@@ -270,18 +270,18 @@ func isRangeOperation(op query.Operator) bool {
|
||||
|
||||
func (txi *TxIndex) match(c query.Condition, startKey []byte) (hashes [][]byte) {
|
||||
if c.Op == query.OpEqual {
|
||||
it := txi.store.IteratorPrefix(startKey)
|
||||
defer it.Release()
|
||||
for it.Next() {
|
||||
it := dbm.IteratePrefix(txi.store, startKey)
|
||||
defer it.Close()
|
||||
for ; it.Valid(); it.Next() {
|
||||
hashes = append(hashes, it.Value())
|
||||
}
|
||||
} else if c.Op == query.OpContains {
|
||||
// XXX: doing full scan because startKey does not apply here
|
||||
// For example, if startKey = "account.owner=an" and search query = "accoutn.owner CONSISTS an"
|
||||
// we can't iterate with prefix "account.owner=an" because we might miss keys like "account.owner=Ulan"
|
||||
it := txi.store.Iterator()
|
||||
defer it.Release()
|
||||
for it.Next() {
|
||||
it := txi.store.Iterator(nil, nil)
|
||||
defer it.Close()
|
||||
for ; it.Valid(); it.Next() {
|
||||
if !isTagKey(it.Key()) {
|
||||
continue
|
||||
}
|
||||
@@ -296,10 +296,10 @@ func (txi *TxIndex) match(c query.Condition, startKey []byte) (hashes [][]byte)
|
||||
}
|
||||
|
||||
func (txi *TxIndex) matchRange(r queryRange, startKey []byte) (hashes [][]byte) {
|
||||
it := txi.store.IteratorPrefix(startKey)
|
||||
defer it.Release()
|
||||
it := dbm.IteratePrefix(txi.store, startKey)
|
||||
defer it.Close()
|
||||
LOOP:
|
||||
for it.Next() {
|
||||
for ; it.Valid(); it.Next() {
|
||||
if !isTagKey(it.Key()) {
|
||||
continue
|
||||
}
|
||||
@@ -376,17 +376,8 @@ func extractValueFromKey(key []byte) string {
|
||||
return parts[1]
|
||||
}
|
||||
|
||||
func keyForTag(tag *abci.KVPair, result *types.TxResult) []byte {
|
||||
switch tag.ValueType {
|
||||
case abci.KVPair_STRING:
|
||||
return []byte(fmt.Sprintf("%s/%v/%d/%d", tag.Key, tag.ValueString, result.Height, result.Index))
|
||||
case abci.KVPair_INT:
|
||||
return []byte(fmt.Sprintf("%s/%v/%d/%d", tag.Key, tag.ValueInt, result.Height, result.Index))
|
||||
// case abci.KVPair_TIME:
|
||||
// return []byte(fmt.Sprintf("%s/%d/%d/%d", tag.Key, tag.ValueTime.Unix(), result.Height, result.Index))
|
||||
default:
|
||||
panic(fmt.Sprintf("Undefined value type: %v", tag.ValueType))
|
||||
}
|
||||
func keyForTag(tag cmn.KVPair, result *types.TxResult) []byte {
|
||||
return []byte(fmt.Sprintf("%s/%d/%d", tag.Key, result.Height, result.Index))
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
+15
-14
@@ -11,6 +11,7 @@ import (
|
||||
abci "github.com/tendermint/abci/types"
|
||||
"github.com/tendermint/tendermint/state/txindex"
|
||||
"github.com/tendermint/tendermint/types"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
db "github.com/tendermint/tmlibs/db"
|
||||
"github.com/tendermint/tmlibs/pubsub/query"
|
||||
)
|
||||
@@ -19,7 +20,7 @@ func TestTxIndex(t *testing.T) {
|
||||
indexer := NewTxIndex(db.NewMemDB())
|
||||
|
||||
tx := types.Tx("HELLO WORLD")
|
||||
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []*abci.KVPair{}}}
|
||||
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []cmn.KVPair{}}}
|
||||
hash := tx.Hash()
|
||||
|
||||
batch := txindex.NewBatch(1)
|
||||
@@ -34,7 +35,7 @@ func TestTxIndex(t *testing.T) {
|
||||
assert.Equal(t, txResult, loadedTxResult)
|
||||
|
||||
tx2 := types.Tx("BYE BYE WORLD")
|
||||
txResult2 := &types.TxResult{1, 0, tx2, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []*abci.KVPair{}}}
|
||||
txResult2 := &types.TxResult{1, 0, tx2, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []cmn.KVPair{}}}
|
||||
hash2 := tx2.Hash()
|
||||
|
||||
err = indexer.Index(txResult2)
|
||||
@@ -49,10 +50,10 @@ func TestTxSearch(t *testing.T) {
|
||||
allowedTags := []string{"account.number", "account.owner", "account.date"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
|
||||
|
||||
txResult := txResultWithTags([]*abci.KVPair{
|
||||
{Key: "account.number", ValueType: abci.KVPair_INT, ValueInt: 1},
|
||||
{Key: "account.owner", ValueType: abci.KVPair_STRING, ValueString: "Ivan"},
|
||||
{Key: "not_allowed", ValueType: abci.KVPair_STRING, ValueString: "Vlad"},
|
||||
txResult := txResultWithTags([]cmn.KVPair{
|
||||
{Key: []byte("account.number"), Value: []byte{1}},
|
||||
{Key: []byte("account.owner"), Value: []byte("Ivan")},
|
||||
{Key: []byte("not_allowed"), Value: []byte("Vlad")},
|
||||
})
|
||||
hash := txResult.Tx.Hash()
|
||||
|
||||
@@ -106,9 +107,9 @@ func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
|
||||
allowedTags := []string{"account.number"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
|
||||
|
||||
txResult := txResultWithTags([]*abci.KVPair{
|
||||
{Key: "account.number", ValueType: abci.KVPair_INT, ValueInt: 1},
|
||||
{Key: "account.number", ValueType: abci.KVPair_INT, ValueInt: 2},
|
||||
txResult := txResultWithTags([]cmn.KVPair{
|
||||
{Key: []byte("account.number"), Value: []byte{1}},
|
||||
{Key: []byte("account.number"), Value: []byte{2}},
|
||||
})
|
||||
|
||||
err := indexer.Index(txResult)
|
||||
@@ -124,9 +125,9 @@ func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
|
||||
func TestIndexAllTags(t *testing.T) {
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexAllTags())
|
||||
|
||||
txResult := txResultWithTags([]*abci.KVPair{
|
||||
abci.KVPairString("account.owner", "Ivan"),
|
||||
abci.KVPairInt("account.number", 1),
|
||||
txResult := txResultWithTags([]cmn.KVPair{
|
||||
cmn.KVPair{[]byte("account.owner"), []byte("Ivan")},
|
||||
cmn.KVPair{[]byte("account.number"), []byte{1}},
|
||||
})
|
||||
|
||||
err := indexer.Index(txResult)
|
||||
@@ -143,14 +144,14 @@ func TestIndexAllTags(t *testing.T) {
|
||||
assert.Equal(t, []*types.TxResult{txResult}, results)
|
||||
}
|
||||
|
||||
func txResultWithTags(tags []*abci.KVPair) *types.TxResult {
|
||||
func txResultWithTags(tags []cmn.KVPair) *types.TxResult {
|
||||
tx := types.Tx("HELLO WORLD")
|
||||
return &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: tags}}
|
||||
}
|
||||
|
||||
func benchmarkTxIndex(txsCount int, b *testing.B) {
|
||||
tx := types.Tx("HELLO WORLD")
|
||||
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []*abci.KVPair{}}}
|
||||
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeTypeOK, Log: "", Tags: []cmn.KVPair{}}}
|
||||
|
||||
dir, err := ioutil.TempDir("", "tx_index_db")
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user