Browse Source

config variable to index all tags

pull/835/head
Anton Kaliaev 7 years ago
parent
commit
e538e0e077
No known key found for this signature in database GPG Key ID: 7B6881D965918214
4 changed files with 46 additions and 15 deletions
  1. +8
    -2
      config/config.go
  2. +7
    -1
      node/node.go
  3. +25
    -6
      state/txindex/kv/kv.go
  4. +6
    -6
      state/txindex/kv/kv_test.go

+ 8
- 2
config/config.go View File

@ -430,13 +430,19 @@ type TxIndexConfig struct {
// bloat. This is, of course, depends on the indexer's DB and the volume of // bloat. This is, of course, depends on the indexer's DB and the volume of
// transactions. // transactions.
IndexTags string `mapstructure:"index_tags"` 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. // DefaultTxIndexConfig returns a default configuration for the transaction indexer.
func DefaultTxIndexConfig() *TxIndexConfig { func DefaultTxIndexConfig() *TxIndexConfig {
return &TxIndexConfig{ return &TxIndexConfig{
Indexer: "kv",
IndexTags: "",
Indexer: "kv",
IndexTags: "",
IndexAllTags: false,
} }
} }


+ 7
- 1
node/node.go View File

@ -288,7 +288,13 @@ func NewNode(config *cfg.Config,
if err != nil { if err != nil {
return nil, err 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: default:
txIndexer = &null.TxIndex{} txIndexer = &null.TxIndex{}
} }


+ 25
- 6
state/txindex/kv/kv.go View File

@ -27,13 +27,32 @@ var _ txindex.TxIndexer = (*TxIndex)(nil)
// TxIndex is the simplest possible indexer, backed by key-value storage (levelDB). // TxIndex is the simplest possible indexer, backed by key-value storage (levelDB).
type TxIndex struct { type TxIndex struct {
store db.DB
tagsToIndex []string
store db.DB
tagsToIndex []string
indexAllTags bool
} }
// NewTxIndex creates new KV indexer. // 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 // 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 // index tx by tags
for _, tag := range result.Result.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) storeBatch.Set(keyForTag(tag, result), hash)
} }
} }
@ -90,7 +109,7 @@ func (txi *TxIndex) Index(result *types.TxResult) error {
// index tx by tags // index tx by tags
for _, tag := range result.Result.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) b.Set(keyForTag(tag, result), hash)
} }
} }


+ 6
- 6
state/txindex/kv/kv_test.go View File

@ -16,7 +16,7 @@ import (
) )
func TestTxIndex(t *testing.T) { func TestTxIndex(t *testing.T) {
indexer := NewTxIndex(db.NewMemDB(), []string{})
indexer := NewTxIndex(db.NewMemDB())
tx := types.Tx("HELLO WORLD") tx := types.Tx("HELLO WORLD")
txResult := &types.TxResult{1, 0, tx, abci.ResponseDeliverTx{Data: []byte{0}, Code: abci.CodeType_OK, Log: "", Tags: []*abci.KVPair{}}} 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) { 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") tx := types.Tx("HELLO WORLD")
tags := []*abci.KVPair{ tags := []*abci.KVPair{
@ -105,8 +105,8 @@ func TestTxSearch(t *testing.T) {
} }
func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(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") tx := types.Tx("SAME MULTIPLE TAGS WITH DIFFERENT VALUES")
tags := []*abci.KVPair{ tags := []*abci.KVPair{
@ -136,7 +136,7 @@ func benchmarkTxIndex(txsCount int, b *testing.B) {
defer os.RemoveAll(dir) // nolint: errcheck defer os.RemoveAll(dir) // nolint: errcheck
store := db.NewDB("tx_index", "leveldb", dir) store := db.NewDB("tx_index", "leveldb", dir)
indexer := NewTxIndex(store, []string{})
indexer := NewTxIndex(store)
batch := txindex.NewBatch(txsCount) batch := txindex.NewBatch(txsCount)
for i := 0; i < txsCount; i++ { for i := 0; i < txsCount; i++ {


Loading…
Cancel
Save