From a4b280633026bc26f8ce3b71796b2319256da04b Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 10 May 2024 16:18:41 +0200 Subject: [PATCH] wip --- lib/logstorage/stats_count_uniq.go | 6 +++++- lib/logstorage/stats_uniq_values.go | 7 ++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/logstorage/stats_count_uniq.go b/lib/logstorage/stats_count_uniq.go index c0a6e0fe4..014139595 100644 --- a/lib/logstorage/stats_count_uniq.go +++ b/lib/logstorage/stats_count_uniq.go @@ -369,11 +369,15 @@ func (sup *statsCountUniqProcessor) mergeState(sfp statsProcessor) { func (sup *statsCountUniqProcessor) finalizeStats() string { n := uint64(len(sup.m)) + if limit := sup.su.limit; limit > 0 && n > limit { + n = limit + } return strconv.FormatUint(n, 10) } func (sup *statsCountUniqProcessor) limitReached() bool { - return sup.su.limit > 0 && uint64(len(sup.m)) >= sup.su.limit + limit := sup.su.limit + return limit > 0 && uint64(len(sup.m)) >= limit } func parseStatsCountUniq(lex *lexer) (*statsCountUniq, error) { diff --git a/lib/logstorage/stats_uniq_values.go b/lib/logstorage/stats_uniq_values.go index 1da95e1ea..f240a77e5 100644 --- a/lib/logstorage/stats_uniq_values.go +++ b/lib/logstorage/stats_uniq_values.go @@ -209,6 +209,10 @@ func (sup *statsUniqValuesProcessor) finalizeStats() string { } slices.SortFunc(items, compareValues) + if limit := sup.su.limit; limit > 0 && uint64(len(items)) > limit { + items = items[:limit] + } + // Marshal items into JSON array. // Pre-allocate buffer for serialized items. @@ -232,7 +236,8 @@ func (sup *statsUniqValuesProcessor) finalizeStats() string { } func (sup *statsUniqValuesProcessor) limitReached() bool { - return sup.su.limit > 0 && uint64(len(sup.m)) >= sup.su.limit + limit := sup.su.limit + return limit > 0 && uint64(len(sup.m)) >= limit } func compareValues(a, b string) int {