mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-21 06:36:19 +00:00
Rename Tag(s) to Event(s) (#4046)
* Rename Tag(s) to Event(s) - tag was replaced with event, but in some places it still mentions tag, would be easier to understand if we tried to replace it with event to not confuse people. Signed-off-by: Marko Baricevic <marbar3778@yahoo.com> * more changes from tag -> event * rename events to compositeKeys and keys * Apply suggestions from code review Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * add minor documentation on how composite keys are constructed * rename eventkey to compositekey Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com> * add changelog entry & add info to regenerate confid to changelog entry
This commit is contained in:
@@ -29,7 +29,7 @@ func NewIndexerService(idr TxIndexer, eventBus *types.EventBus) *IndexerService
|
||||
}
|
||||
|
||||
// OnStart implements cmn.Service by subscribing for all transactions
|
||||
// and indexing them by tags.
|
||||
// and indexing them by events.
|
||||
func (is *IndexerService) OnStart() error {
|
||||
// Use SubscribeUnbuffered here to ensure both subscriptions does not get
|
||||
// cancelled due to not pulling messages fast enough. Cause this might
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestIndexerServiceIndexesBlocks(t *testing.T) {
|
||||
|
||||
// tx indexer
|
||||
store := db.NewMemDB()
|
||||
txIndexer := kv.NewTxIndex(store, kv.IndexAllTags())
|
||||
txIndexer := kv.NewTxIndex(store, kv.IndexAllEvents())
|
||||
|
||||
service := txindex.NewIndexerService(txIndexer, eventBus)
|
||||
service.SetLogger(log.TestingLogger())
|
||||
|
||||
+22
-22
@@ -27,31 +27,31 @@ var _ txindex.TxIndexer = (*TxIndex)(nil)
|
||||
|
||||
// TxIndex is the simplest possible indexer, backed by key-value storage (levelDB).
|
||||
type TxIndex struct {
|
||||
store dbm.DB
|
||||
tagsToIndex []string
|
||||
indexAllTags bool
|
||||
store dbm.DB
|
||||
compositeKeysToIndex []string
|
||||
indexAllEvents bool
|
||||
}
|
||||
|
||||
// NewTxIndex creates new KV indexer.
|
||||
func NewTxIndex(store dbm.DB, options ...func(*TxIndex)) *TxIndex {
|
||||
txi := &TxIndex{store: store, tagsToIndex: make([]string, 0), indexAllTags: false}
|
||||
txi := &TxIndex{store: store, compositeKeysToIndex: make([]string, 0), indexAllEvents: false}
|
||||
for _, o := range options {
|
||||
o(txi)
|
||||
}
|
||||
return txi
|
||||
}
|
||||
|
||||
// IndexTags is an option for setting which tags to index.
|
||||
func IndexTags(tags []string) func(*TxIndex) {
|
||||
// IndexEvents is an option for setting which composite keys to index.
|
||||
func IndexEvents(compositeKeys []string) func(*TxIndex) {
|
||||
return func(txi *TxIndex) {
|
||||
txi.tagsToIndex = tags
|
||||
txi.compositeKeysToIndex = compositeKeys
|
||||
}
|
||||
}
|
||||
|
||||
// IndexAllTags is an option for indexing all tags.
|
||||
func IndexAllTags() func(*TxIndex) {
|
||||
// IndexAllEvents is an option for indexing all events.
|
||||
func IndexAllEvents() func(*TxIndex) {
|
||||
return func(txi *TxIndex) {
|
||||
txi.indexAllTags = true
|
||||
txi.indexAllEvents = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
||||
txi.indexEvents(result, hash, storeBatch)
|
||||
|
||||
// index tx by height
|
||||
if txi.indexAllTags || cmn.StringInSlice(types.TxHeightKey, txi.tagsToIndex) {
|
||||
if txi.indexAllEvents || cmn.StringInSlice(types.TxHeightKey, txi.compositeKeysToIndex) {
|
||||
storeBatch.Set(keyForHeight(result), hash)
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||
txi.indexEvents(result, hash, b)
|
||||
|
||||
// index tx by height
|
||||
if txi.indexAllTags || cmn.StringInSlice(types.TxHeightKey, txi.tagsToIndex) {
|
||||
if txi.indexAllEvents || cmn.StringInSlice(types.TxHeightKey, txi.compositeKeysToIndex) {
|
||||
b.Set(keyForHeight(result), hash)
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ func (txi *TxIndex) indexEvents(result *types.TxResult, hash []byte, store dbm.S
|
||||
}
|
||||
|
||||
compositeTag := fmt.Sprintf("%s.%s", event.Type, string(attr.Key))
|
||||
if txi.indexAllTags || cmn.StringInSlice(compositeTag, txi.tagsToIndex) {
|
||||
if txi.indexAllEvents || cmn.StringInSlice(compositeTag, txi.compositeKeysToIndex) {
|
||||
store.Set(keyForEvent(compositeTag, attr.Value, result), hash)
|
||||
}
|
||||
}
|
||||
@@ -260,7 +260,7 @@ func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) {
|
||||
|
||||
func lookForHash(conditions []query.Condition) (hash []byte, ok bool, err error) {
|
||||
for _, c := range conditions {
|
||||
if c.Tag == types.TxHashKey {
|
||||
if c.CompositeKey == types.TxHashKey {
|
||||
decoded, err := hex.DecodeString(c.Operand.(string))
|
||||
return decoded, true, err
|
||||
}
|
||||
@@ -271,7 +271,7 @@ func lookForHash(conditions []query.Condition) (hash []byte, ok bool, err error)
|
||||
// lookForHeight returns a height if there is an "height=X" condition.
|
||||
func lookForHeight(conditions []query.Condition) (height int64) {
|
||||
for _, c := range conditions {
|
||||
if c.Tag == types.TxHeightKey && c.Op == query.OpEqual {
|
||||
if c.CompositeKey == types.TxHeightKey && c.Op == query.OpEqual {
|
||||
return c.Operand.(int64)
|
||||
}
|
||||
}
|
||||
@@ -340,9 +340,9 @@ func lookForRanges(conditions []query.Condition) (ranges queryRanges, indexes []
|
||||
ranges = make(queryRanges)
|
||||
for i, c := range conditions {
|
||||
if isRangeOperation(c.Op) {
|
||||
r, ok := ranges[c.Tag]
|
||||
r, ok := ranges[c.CompositeKey]
|
||||
if !ok {
|
||||
r = queryRange{key: c.Tag}
|
||||
r = queryRange{key: c.CompositeKey}
|
||||
}
|
||||
switch c.Op {
|
||||
case query.OpGreater:
|
||||
@@ -356,7 +356,7 @@ func lookForRanges(conditions []query.Condition) (ranges queryRanges, indexes []
|
||||
r.includeUpperBound = true
|
||||
r.upperBound = c.Operand
|
||||
}
|
||||
ranges[c.Tag] = r
|
||||
ranges[c.CompositeKey] = r
|
||||
indexes = append(indexes, i)
|
||||
}
|
||||
}
|
||||
@@ -404,7 +404,7 @@ func (txi *TxIndex) match(
|
||||
// XXX: startKey does not apply here.
|
||||
// For example, if startKey = "account.owner/an/" and search query = "account.owner CONTAINS an"
|
||||
// we can't iterate with prefix "account.owner/an/" because we might miss keys like "account.owner/Ulan/"
|
||||
it := dbm.IteratePrefix(txi.store, startKey(c.Tag))
|
||||
it := dbm.IteratePrefix(txi.store, startKey(c.CompositeKey))
|
||||
defer it.Close()
|
||||
|
||||
for ; it.Valid(); it.Next() {
|
||||
@@ -491,7 +491,7 @@ LOOP:
|
||||
tmpHashes[string(it.Value())] = it.Value()
|
||||
}
|
||||
|
||||
// XXX: passing time in a ABCI Tags is not yet implemented
|
||||
// XXX: passing time in a ABCI Events is not yet implemented
|
||||
// case time.Time:
|
||||
// v := strconv.ParseInt(extractValueFromKey(it.Key()), 10, 64)
|
||||
// if v == r.upperBound {
|
||||
@@ -554,9 +554,9 @@ func keyForHeight(result *types.TxResult) []byte {
|
||||
|
||||
func startKeyForCondition(c query.Condition, height int64) []byte {
|
||||
if height > 0 {
|
||||
return startKey(c.Tag, c.Operand, height)
|
||||
return startKey(c.CompositeKey, c.Operand, height)
|
||||
}
|
||||
return startKey(c.Tag, c.Operand)
|
||||
return startKey(c.CompositeKey, c.Operand)
|
||||
}
|
||||
|
||||
func startKey(fields ...interface{}) []byte {
|
||||
|
||||
@@ -24,8 +24,8 @@ func BenchmarkTxSearch(b *testing.B) {
|
||||
b.Errorf("failed to create database: %s", err)
|
||||
}
|
||||
|
||||
allowedTags := []string{"transfer.address", "transfer.amount"}
|
||||
indexer := NewTxIndex(db, IndexTags(allowedTags))
|
||||
allowedKeys := []string{"transfer.address", "transfer.amount"}
|
||||
indexer := NewTxIndex(db, IndexEvents(allowedKeys))
|
||||
|
||||
for i := 0; i < 35000; i++ {
|
||||
events := []abci.Event{
|
||||
|
||||
+21
-21
@@ -65,8 +65,8 @@ func TestTxIndex(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTxSearch(t *testing.T) {
|
||||
allowedTags := []string{"account.number", "account.owner", "account.date"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
|
||||
allowedKeys := []string{"account.number", "account.owner", "account.date"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexEvents(allowedKeys))
|
||||
|
||||
txResult := txResultWithEvents([]abci.Event{
|
||||
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("1")}}},
|
||||
@@ -84,11 +84,11 @@ func TestTxSearch(t *testing.T) {
|
||||
}{
|
||||
// search by hash
|
||||
{fmt.Sprintf("tx.hash = '%X'", hash), 1},
|
||||
// search by exact match (one tag)
|
||||
// search by exact match (one key)
|
||||
{"account.number = 1", 1},
|
||||
// search by exact match (two tags)
|
||||
// search by exact match (two keys)
|
||||
{"account.number = 1 AND account.owner = 'Ivan'", 1},
|
||||
// search by exact match (two tags)
|
||||
// search by exact match (two keys)
|
||||
{"account.number = 1 AND account.owner = 'Vlad'", 0},
|
||||
{"account.owner = 'Vlad' AND account.number = 1", 0},
|
||||
{"account.number >= 1 AND account.owner = 'Vlad'", 0},
|
||||
@@ -103,17 +103,17 @@ func TestTxSearch(t *testing.T) {
|
||||
{"account.number >= 1", 1},
|
||||
// search by range (upper bound)
|
||||
{"account.number <= 5", 1},
|
||||
// search using not allowed tag
|
||||
// search using not allowed key
|
||||
{"not_allowed = 'boom'", 0},
|
||||
// search for not existing tx result
|
||||
{"account.number >= 2 AND account.number <= 5", 0},
|
||||
// search using not existing tag
|
||||
// search using not existing key
|
||||
{"account.date >= TIME 2013-05-03T14:45:00Z", 0},
|
||||
// search using CONTAINS
|
||||
{"account.owner CONTAINS 'an'", 1},
|
||||
// search for non existing value using CONTAINS
|
||||
{"account.owner CONTAINS 'Vlad'", 0},
|
||||
// search using the wrong tag (of numeric type) using CONTAINS
|
||||
// search using the wrong key (of numeric type) using CONTAINS
|
||||
{"account.number CONTAINS 'Iv'", 0},
|
||||
}
|
||||
|
||||
@@ -132,8 +132,8 @@ func TestTxSearch(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
allowedTags := []string{"account.number", "sender"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
|
||||
allowedKeys := []string{"account.number", "sender"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexEvents(allowedKeys))
|
||||
|
||||
// index tx using events indexing (composite key)
|
||||
txResult1 := txResultWithEvents([]abci.Event{
|
||||
@@ -144,7 +144,7 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
err := indexer.Index(txResult1)
|
||||
require.NoError(t, err)
|
||||
|
||||
// index tx also using deprecated indexing (tag as key)
|
||||
// index tx also using deprecated indexing (event as key)
|
||||
txResult2 := txResultWithEvents(nil)
|
||||
txResult2.Tx = types.Tx("HELLO WORLD 2")
|
||||
|
||||
@@ -174,20 +174,20 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
{fmt.Sprintf("tx.hash = '%X'", hash1), []*types.TxResult{txResult1}},
|
||||
// search by hash
|
||||
{fmt.Sprintf("tx.hash = '%X'", hash2), []*types.TxResult{txResult2}},
|
||||
// search by exact match (one tag)
|
||||
// search by exact match (one key)
|
||||
{"account.number = 1", []*types.TxResult{txResult1}},
|
||||
{"account.number >= 1 AND account.number <= 5", []*types.TxResult{txResult1}},
|
||||
// search by range (lower bound)
|
||||
{"account.number >= 1", []*types.TxResult{txResult1}},
|
||||
// search by range (upper bound)
|
||||
{"account.number <= 5", []*types.TxResult{txResult1}},
|
||||
// search using not allowed tag
|
||||
// search using not allowed key
|
||||
{"not_allowed = 'boom'", []*types.TxResult{}},
|
||||
// search for not existing tx result
|
||||
{"account.number >= 2 AND account.number <= 5", []*types.TxResult{}},
|
||||
// search using not existing tag
|
||||
// search using not existing key
|
||||
{"account.date >= TIME 2013-05-03T14:45:00Z", []*types.TxResult{}},
|
||||
// search by deprecated tag
|
||||
// search by deprecated key
|
||||
{"sender = 'addr1'", []*types.TxResult{txResult2}},
|
||||
}
|
||||
|
||||
@@ -202,8 +202,8 @@ func TestTxSearchDeprecatedIndexing(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
|
||||
allowedTags := []string{"account.number"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
|
||||
allowedKeys := []string{"account.number"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexEvents(allowedKeys))
|
||||
|
||||
txResult := txResultWithEvents([]abci.Event{
|
||||
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number"), Value: []byte("1")}}},
|
||||
@@ -221,8 +221,8 @@ func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTxSearchMultipleTxs(t *testing.T) {
|
||||
allowedTags := []string{"account.number", "account.number.id"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))
|
||||
allowedKeys := []string{"account.number", "account.number.id"}
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexEvents(allowedKeys))
|
||||
|
||||
// indexed first, but bigger height (to test the order of transactions)
|
||||
txResult := txResultWithEvents([]abci.Event{
|
||||
@@ -256,7 +256,7 @@ func TestTxSearchMultipleTxs(t *testing.T) {
|
||||
err = indexer.Index(txResult3)
|
||||
require.NoError(t, err)
|
||||
|
||||
// indexed fourth (to test we don't include txs with similar tags)
|
||||
// indexed fourth (to test we don't include txs with similar events)
|
||||
// https://github.com/tendermint/tendermint/issues/2908
|
||||
txResult4 := txResultWithEvents([]abci.Event{
|
||||
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("number.id"), Value: []byte("1")}}},
|
||||
@@ -275,7 +275,7 @@ func TestTxSearchMultipleTxs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIndexAllTags(t *testing.T) {
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexAllTags())
|
||||
indexer := NewTxIndex(db.NewMemDB(), IndexAllEvents())
|
||||
|
||||
txResult := txResultWithEvents([]abci.Event{
|
||||
{Type: "account", Attributes: []cmn.KVPair{{Key: []byte("owner"), Value: []byte("Ivan")}}},
|
||||
|
||||
Reference in New Issue
Block a user