Skip to content

Commit 1a7018a

Browse files
authored
Fixed inefficient Stream API call chains ending with count() (#15386)
* Fixed inefficient Stream API call chains ending with count() Signed-off-by: Dmitry Kryukov <[email protected]> * Refactored method minTermLength() as per @sandeshkr419's advice Signed-off-by: Dmitry Kryukov <[email protected]> * Added a line in CHANGELOG.md Signed-off-by: Dmitry Kryukov <[email protected]> --------- Signed-off-by: Dmitry Kryukov <[email protected]>
1 parent ec7b652 commit 1a7018a

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8686
- Fix wrong default value when setting `index.number_of_routing_shards` to null on index creation ([#16331](https://github.com/opensearch-project/OpenSearch/pull/16331))
8787
- Fix disk usage exceeds threshold cluster can't spin up issue ([#15258](https://github.com/opensearch-project/OpenSearch/pull/15258)))
8888

89+
- Fix inefficient Stream API call chains ending with count() ([#15386](https://github.com/opensearch-project/OpenSearch/pull/15386))
8990

9091
### Security
9192

modules/percolator/src/main/java/org/opensearch/percolator/QueryAnalyzer.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,25 @@ static Result selectBestResult(Result result1, Result result2) {
510510
}
511511

512512
private static int minTermLength(Set<QueryExtraction> extractions) {
513-
// In case there are only range extractions, then we return Integer.MIN_VALUE,
514-
// so that selectBestExtraction(...) we are likely to prefer the extractions that contains at least a single extraction
515-
if (extractions.stream().filter(queryExtraction -> queryExtraction.term != null).count() == 0
516-
&& extractions.stream().filter(queryExtraction -> queryExtraction.range != null).count() > 0) {
517-
return Integer.MIN_VALUE;
518-
}
519-
513+
boolean hasTerm = false;
514+
boolean hasRange = false;
520515
int min = Integer.MAX_VALUE;
516+
521517
for (QueryExtraction qt : extractions) {
522518
if (qt.term != null) {
519+
hasTerm = true;
523520
min = Math.min(min, qt.bytes().length);
524521
}
522+
if (qt.range != null) {
523+
hasRange = true;
524+
}
525525
}
526+
527+
// If there are no terms but there are ranges, return Integer.MIN_VALUE
528+
if (!hasTerm && hasRange) {
529+
return Integer.MIN_VALUE;
530+
}
531+
526532
return min;
527533
}
528534

0 commit comments

Comments
 (0)