Code cleanup

This commit is contained in:
Jasmina Malicevic
2022-12-22 16:17:57 +01:00
parent 69a8793fa4
commit f4a6e22db9
4 changed files with 25 additions and 35 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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)
}
}

View File

@@ -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