From e538e0e0772163437c08c30c5c4eee0424c6feac Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 30 Nov 2017 20:02:39 -0600 Subject: [PATCH] config variable to index all tags --- config/config.go | 10 ++++++++-- node/node.go | 8 +++++++- state/txindex/kv/kv.go | 31 +++++++++++++++++++++++++------ state/txindex/kv/kv_test.go | 12 ++++++------ 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/config/config.go b/config/config.go index fc3671d8e..ea3fa13e4 100644 --- a/config/config.go +++ b/config/config.go @@ -430,13 +430,19 @@ type TxIndexConfig struct { // bloat. This is, of course, depends on the indexer's DB and the volume of // transactions. IndexTags string `mapstructure:"index_tags"` + + // When set to true, tells indexer to index all tags. Note this may be not + // desirable (see the comment above). IndexTags has a precedence over + // IndexAllTags (i.e. when given both, IndexTags will be indexed). + IndexAllTags bool `mapstructure:"index_all_tags"` } // DefaultTxIndexConfig returns a default configuration for the transaction indexer. func DefaultTxIndexConfig() *TxIndexConfig { return &TxIndexConfig{ - Indexer: "kv", - IndexTags: "", + Indexer: "kv", + IndexTags: "", + IndexAllTags: false, } } diff --git a/node/node.go b/node/node.go index 865b8741d..7841a103d 100644 --- a/node/node.go +++ b/node/node.go @@ -288,7 +288,13 @@ func NewNode(config *cfg.Config, if err != nil { return nil, err } - txIndexer = kv.NewTxIndex(store, strings.Split(config.TxIndex.IndexTags, ",")) + if config.TxIndex.IndexTags != "" { + txIndexer = kv.NewTxIndex(store, kv.IndexTags(strings.Split(config.TxIndex.IndexTags, ","))) + } else if config.TxIndex.IndexAllTags { + txIndexer = kv.NewTxIndex(store, kv.IndexAllTags()) + } else { + txIndexer = kv.NewTxIndex(store) + } default: txIndexer = &null.TxIndex{} } diff --git a/state/txindex/kv/kv.go b/state/txindex/kv/kv.go index 413569b1d..5ca4d0628 100644 --- a/state/txindex/kv/kv.go +++ b/state/txindex/kv/kv.go @@ -27,13 +27,32 @@ var _ txindex.TxIndexer = (*TxIndex)(nil) // TxIndex is the simplest possible indexer, backed by key-value storage (levelDB). type TxIndex struct { - store db.DB - tagsToIndex []string + store db.DB + tagsToIndex []string + indexAllTags bool } // NewTxIndex creates new KV indexer. -func NewTxIndex(store db.DB, tagsToIndex []string) *TxIndex { - return &TxIndex{store: store, tagsToIndex: tagsToIndex} +func NewTxIndex(store db.DB, options ...func(*TxIndex)) *TxIndex { + txi := &TxIndex{store: store, tagsToIndex: make([]string, 0), indexAllTags: 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) { + return func(txi *TxIndex) { + txi.tagsToIndex = tags + } +} + +// IndexAllTags is an option for indexing all tags. +func IndexAllTags() func(*TxIndex) { + return func(txi *TxIndex) { + txi.indexAllTags = true + } } // Get gets transaction from the TxIndex storage and returns it or nil if the @@ -68,7 +87,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error { // index tx by tags for _, tag := range result.Result.Tags { - if cmn.StringInSlice(tag.Key, txi.tagsToIndex) { + if txi.indexAllTags || cmn.StringInSlice(tag.Key, txi.tagsToIndex) { storeBatch.Set(keyForTag(tag, result), hash) } } @@ -90,7 +109,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error { // index tx by tags for _, tag := range result.Result.Tags { - if cmn.StringInSlice(tag.Key, txi.tagsToIndex) { + if txi.indexAllTags || cmn.StringInSlice(tag.Key, txi.tagsToIndex) { b.Set(keyForTag(tag, result), hash) } } diff --git a/state/txindex/kv/kv_test.go b/state/txindex/kv/kv_test.go index 3da91a5d4..e55f4887d 100644 --- a/state/txindex/kv/kv_test.go +++ b/state/txindex/kv/kv_test.go @@ -16,7 +16,7 @@ import ( ) func TestTxIndex(t *testing.T) { - indexer := NewTxIndex(db.NewMemDB(), []string{}) + indexer := NewTxIndex(db.NewMemDB()) tx := types.Tx("HELLO WORLD") txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: "", Tags: []*abci.KVPair{}}} @@ -46,8 +46,8 @@ func TestTxIndex(t *testing.T) { } func TestTxSearch(t *testing.T) { - tagsToIndex := []string{"account.number", "account.owner", "account.date"} - indexer := NewTxIndex(db.NewMemDB(), tagsToIndex) + tags := []string{"account.number", "account.owner", "account.date"} + indexer := NewTxIndex(db.NewMemDB(), IndexTags(tags)) tx := types.Tx("HELLO WORLD") tags := []*abci.KVPair{ @@ -105,8 +105,8 @@ func TestTxSearch(t *testing.T) { } func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) { - tagsToIndex := []string{"account.number"} - indexer := NewTxIndex(db.NewMemDB(), tagsToIndex) + tags := []string{"account.number"} + indexer := NewTxIndex(db.NewMemDB(), IndexTags(tags)) tx := types.Tx("SAME MULTIPLE TAGS WITH DIFFERENT VALUES") tags := []*abci.KVPair{ @@ -136,7 +136,7 @@ func benchmarkTxIndex(txsCount int, b *testing.B) { defer os.RemoveAll(dir) // nolint: errcheck store := db.NewDB("tx_index", "leveldb", dir) - indexer := NewTxIndex(store, []string{}) + indexer := NewTxIndex(store) batch := txindex.NewBatch(txsCount) for i := 0; i < txsCount; i++ {