From f4a6e22db9144ac3e3ef8028cfdc9c085ce48e5d Mon Sep 17 00:00:00 2001 From: Jasmina Malicevic Date: Thu, 22 Dec 2022 16:17:57 +0100 Subject: [PATCH] Code cleanup --- state/indexer/block/kv/kv.go | 18 +++++++++--------- state/indexer/block/kv/util.go | 14 +++++--------- state/txindex/kv/kv.go | 14 ++++++-------- state/txindex/kv/utils.go | 14 +++++--------- 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/state/indexer/block/kv/kv.go b/state/indexer/block/kv/kv.go index 479dcbd6f..2ef7148b4 100644 --- a/state/indexer/block/kv/kv.go +++ b/state/indexer/block/kv/kv.go @@ -118,19 +118,19 @@ func (idx *BlockerIndexer) Search(ctx context.Context, q *query.Query) ([]int64, // If there is an exact height query, return the result immediately // (if it exists). - var height int64 + // var height int64 var ok bool - var heightIdx int + // var heightIdx int var heightInfo HeightInfo if matchEvents { // If we are not matching events and block.height = 3 occurs more than once, the later value will // overwrite the first one. For match.events it will create problems. If we have a height range, we // should ignore height equality. conditions, heightInfo, ok = dedupHeight(conditions) - height = heightInfo.height - heightIdx = heightInfo.heightEqIdx + // height = heightInfo.height + // heightIdx = heightInfo.heightEqIdx } else { - height, ok, heightIdx = lookForHeight(conditions) + heightInfo.height, ok, heightInfo.heightEqIdx = lookForHeight(conditions) } // Extract ranges. If both upper and lower bounds exist, it's better to get @@ -149,13 +149,13 @@ func (idx *BlockerIndexer) Search(ctx context.Context, q *query.Query) ([]int64, // in the query (the second part of the ||), we don't need to query // per event conditions and return all events within the height range. if ok && (!matchEvents || (matchEvents && heightInfo.onlyHeightEq)) { - ok, err := idx.Has(height) + ok, err := idx.Has(heightInfo.height) if err != nil { return nil, err } if ok { - return []int64{height}, nil + return []int64{heightInfo.height}, nil } return results, nil @@ -163,8 +163,8 @@ func (idx *BlockerIndexer) Search(ctx context.Context, q *query.Query) ([]int64, var heightsInitialized bool filteredHeights := make(map[string][]byte) - if matchEvents && heightIdx != -1 { - skipIndexes = append(skipIndexes, heightIdx) + if matchEvents && heightInfo.heightEqIdx != -1 { + skipIndexes = append(skipIndexes, heightInfo.heightEqIdx) } if len(ranges) > 0 { diff --git a/state/indexer/block/kv/util.go b/state/indexer/block/kv/util.go index 0f72a0f8d..b146723ba 100644 --- a/state/indexer/block/kv/util.go +++ b/state/indexer/block/kv/util.go @@ -183,8 +183,8 @@ func dedupHeight(conditions []query.Condition) (dedupConditions []query.Conditio // If we found a range make sure we set the hegiht idx to -1 as the height equality // will be removed heightInfo.heightEqIdx = -1 + heightInfo.height = 0 found = false - heightInfo.heightEqIdx = 0 heightInfo.onlyHeightEq = false } return dedupConditions, heightInfo, found @@ -209,15 +209,11 @@ func dedupMatchEvents(conditions []query.Condition) ([]query.Condition, bool) { } func checkHeightConditions(heightInfo HeightInfo, keyHeight int64) bool { - if heightInfo.heightRange.Key != "" { - if !checkBounds(heightInfo.heightRange, keyHeight) { - return false - } + if heightInfo.heightRange.Key != "" && !checkBounds(heightInfo.heightRange, keyHeight) { + return false } else { - if heightInfo.height != 0 { - if keyHeight != heightInfo.height { - return false - } + if heightInfo.height != 0 && keyHeight != heightInfo.height { + return false } } return true diff --git a/state/txindex/kv/kv.go b/state/txindex/kv/kv.go index 128da2a1b..7f7a236f2 100644 --- a/state/txindex/kv/kv.go +++ b/state/txindex/kv/kv.go @@ -239,20 +239,18 @@ func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*abci.TxResul } // if there is a height condition ("tx.height=3"), extract it - var height int64 - var heightIdx int + // var height int64 + // var heightIdx int var heightInfo HeightInfo if matchEvents { // If we are not matching events and tx.height = 3 occurs more than once, the later value will // overwrite the first one. For match.events it will create problems. conditions, heightInfo = dedupHeight(conditions) - height = heightInfo.height - heightIdx = heightInfo.heightEqIdx } else { - height, heightIdx = lookForHeight(conditions) + heightInfo.height, heightInfo.heightEqIdx = lookForHeight(conditions) } if matchEvents && !heightInfo.onlyHeightEq { - skipIndexes = append(skipIndexes, heightIdx) + skipIndexes = append(skipIndexes, heightInfo.heightEqIdx) } // extract ranges // if both upper and lower bounds exist, it's better to get them in order not @@ -300,7 +298,7 @@ func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*abci.TxResul } if !hashesInitialized { - filteredHashes = txi.match(ctx, c, startKeyForCondition(c, height), filteredHashes, true, matchEvents, heightInfo) + filteredHashes = txi.match(ctx, c, startKeyForCondition(c, heightInfo.height), filteredHashes, true, matchEvents, heightInfo) hashesInitialized = true // Ignore any remaining conditions if the first condition resulted @@ -309,7 +307,7 @@ func (txi *TxIndex) Search(ctx context.Context, q *query.Query) ([]*abci.TxResul break } } else { - filteredHashes = txi.match(ctx, c, startKeyForCondition(c, height), filteredHashes, false, matchEvents, heightInfo) + filteredHashes = txi.match(ctx, c, startKeyForCondition(c, heightInfo.height), filteredHashes, false, matchEvents, heightInfo) } } diff --git a/state/txindex/kv/utils.go b/state/txindex/kv/utils.go index 25467030e..046635344 100644 --- a/state/txindex/kv/utils.go +++ b/state/txindex/kv/utils.go @@ -100,22 +100,18 @@ func dedupHeight(conditions []query.Condition) (dedupConditions []query.Conditio // If we found a range make sure we set the hegiht idx to -1 as the height equality // will be removed heightInfo.heightEqIdx = -1 - heightInfo.heightEqIdx = 0 + heightInfo.height = 0 heightInfo.onlyHeightEq = false } return dedupConditions, heightInfo } func checkHeightConditions(heightInfo HeightInfo, keyHeight int64) bool { - if heightInfo.heightRange.Key != "" { - if !checkBounds(heightInfo.heightRange, keyHeight) { - return false - } + if heightInfo.heightRange.Key != "" && !checkBounds(heightInfo.heightRange, keyHeight) { + return false } else { - if heightInfo.height != 0 { - if keyHeight != heightInfo.height { - return false - } + if heightInfo.height != 0 && keyHeight != heightInfo.height { + return false } } return true