Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kv indexer: add separator to start key when matching ranges #2925

Merged
merged 3 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 33 additions & 18 deletions state/txindex/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) {

for _, r := range ranges {
if !hashesInitialized {
hashes = txi.matchRange(r, []byte(r.key))
hashes = txi.matchRange(r, startKey(r.key))
hashesInitialized = true
} else {
hashes = intersect(hashes, txi.matchRange(r, []byte(r.key)))
hashes = intersect(hashes, txi.matchRange(r, startKey(r.key)))
}
}
}
Expand All @@ -190,10 +190,10 @@ func (txi *TxIndex) Search(q *query.Query) ([]*types.TxResult, error) {
}

if !hashesInitialized {
hashes = txi.match(c, startKey(c, height))
hashes = txi.match(c, startKeyForCondition(c, height))
hashesInitialized = true
} else {
hashes = intersect(hashes, txi.match(c, startKey(c, height)))
hashes = intersect(hashes, txi.match(c, startKeyForCondition(c, height)))
}
}

Expand Down Expand Up @@ -359,14 +359,14 @@ func (txi *TxIndex) match(c query.Condition, startKey []byte) (hashes [][]byte)
return
}

func (txi *TxIndex) matchRange(r queryRange, prefix []byte) (hashes [][]byte) {
func (txi *TxIndex) matchRange(r queryRange, startKey []byte) (hashes [][]byte) {
// create a map to prevent duplicates
hashesMap := make(map[string][]byte)

lowerBound := r.lowerBoundValue()
upperBound := r.upperBoundValue()

it := dbm.IteratePrefix(txi.store, prefix)
it := dbm.IteratePrefix(txi.store, startKey)
defer it.Close()
LOOP:
for ; it.Valid(); it.Next() {
Expand Down Expand Up @@ -409,16 +409,6 @@ LOOP:
///////////////////////////////////////////////////////////////////////////////
// Keys

func startKey(c query.Condition, height int64) []byte {
var key string
if height > 0 {
key = fmt.Sprintf("%s/%v/%d/", c.Tag, c.Operand, height)
} else {
key = fmt.Sprintf("%s/%v/", c.Tag, c.Operand)
}
return []byte(key)
}

func isTagKey(key []byte) bool {
return strings.Count(string(key), tagKeySeparator) == 3
}
Expand All @@ -429,11 +419,36 @@ func extractValueFromKey(key []byte) string {
}

func keyForTag(tag cmn.KVPair, result *types.TxResult) []byte {
return []byte(fmt.Sprintf("%s/%s/%d/%d", tag.Key, tag.Value, result.Height, result.Index))
return []byte(fmt.Sprintf("%s/%s/%d/%d",
tag.Key,
tag.Value,
result.Height,
result.Index,
))
}

func keyForHeight(result *types.TxResult) []byte {
return []byte(fmt.Sprintf("%s/%d/%d/%d", types.TxHeightKey, result.Height, result.Height, result.Index))
return []byte(fmt.Sprintf("%s/%d/%d/%d",
types.TxHeightKey,
result.Height,
result.Height,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is height supposed to be here twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It's sort of a hack.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a comment here about the rationale of this hack?

result.Index,
))
}

func startKeyForCondition(c query.Condition, height int64) []byte {
if height > 0 {
return startKey(c.Tag, c.Operand, height)
}
return startKey(c.Tag, c.Operand)
}

func startKey(fields ...interface{}) []byte {
var b bytes.Buffer
for _, f := range fields {
b.Write([]byte(fmt.Sprintf("%v", f) + tagKeySeparator))
melekes marked this conversation as resolved.
Show resolved Hide resolved
}
return b.Bytes()
}

///////////////////////////////////////////////////////////////////////////////
Expand Down
13 changes: 12 additions & 1 deletion state/txindex/kv/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestTxSearchOneTxWithMultipleSameTagsButDifferentValues(t *testing.T) {
}

func TestTxSearchMultipleTxs(t *testing.T) {
allowedTags := []string{"account.number"}
allowedTags := []string{"account.number", "account.number.id"}
indexer := NewTxIndex(db.NewMemDB(), IndexTags(allowedTags))

// indexed first, but bigger height (to test the order of transactions)
Expand Down Expand Up @@ -160,6 +160,17 @@ 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)
// https://github.com/tendermint/tendermint/issues/2908
txResult4 := txResultWithTags([]cmn.KVPair{
{Key: []byte("account.number.id"), Value: []byte("1")},
})
txResult4.Tx = types.Tx("Mike's account")
txResult4.Height = 2
txResult4.Index = 2
err = indexer.Index(txResult4)
require.NoError(t, err)

results, err := indexer.Search(query.MustParse("account.number >= 1"))
assert.NoError(t, err)

Expand Down