diff options
author | ngc224 <ngc224@yandex-team.com> | 2025-01-14 11:24:26 +0300 |
---|---|---|
committer | ngc224 <ngc224@yandex-team.com> | 2025-01-14 12:04:33 +0300 |
commit | c84f9bf19d66e2e3d96a52f6f2181676ebca8a52 (patch) | |
tree | 56f350523be0552bebd86c273d9b455ee85d3720 /library/cpp/yt/string/format-inl.h | |
parent | 8e2a62c296736bedbbabeeb8152488d17d6452a1 (diff) | |
download | ydb-c84f9bf19d66e2e3d96a52f6f2181676ebca8a52.tar.gz |
Squeeze contiguous sequences of blocks in logging
commit_hash:5dda6f5ef324f03c99af3dae32ebf4a059f02bd3
Diffstat (limited to 'library/cpp/yt/string/format-inl.h')
-rw-r--r-- | library/cpp/yt/string/format-inl.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/library/cpp/yt/string/format-inl.h b/library/cpp/yt/string/format-inl.h index d286f3afbb..83d6198674 100644 --- a/library/cpp/yt/string/format-inl.h +++ b/library/cpp/yt/string/format-inl.h @@ -232,6 +232,43 @@ void FormatKeyValueRange(TStringBuilderBase* builder, const TRange& range, const //////////////////////////////////////////////////////////////////////////////// +template <class TRange, class TValueGetter, class TIntervalFormatter> +void FormatCompactIntervalRange( + TStringBuilderBase* builder, + const TRange& range, + const TValueGetter& valueGetter, + const TIntervalFormatter& intervalFormatter) +{ + if (range.begin() == range.end()) { + builder->AppendString("[]"); + return; + } + + builder->AppendChar('['); + + auto first = range.begin(); + auto last = first; + auto current = first + 1; + + while (current != range.end()) { + if (valueGetter(current) != valueGetter(last) + 1) { + bool firstInterval = first == range.begin(); + intervalFormatter(builder, first, last, valueGetter, firstInterval); + first = current; + } + + last = current; + ++current; + } + + bool firstInterval = first == range.begin(); + intervalFormatter(builder, first, last, valueGetter, firstInterval); + + builder->AppendChar(']'); +} + +//////////////////////////////////////////////////////////////////////////////// + template <class R> concept CFormattableRange = NDetail::CKnownRange<R> && @@ -303,6 +340,59 @@ TFormatterWrapper<TFormatter> MakeFormatterWrapper( }; } +template <class TRange, class TValueGetter, class TIntervalFormatter> +auto TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>::begin() const + -> TBegin +{ + return RangeBegin; +} + +template <class TRange, class TValueGetter, class TIntervalFormatter> +auto TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>::end() const + -> TEnd +{ + return RangeEnd; +} + +template <class TRange> +auto TDefaultValueGetter<TRange>::operator()(const TIterator& iterator) const + -> typename std::iterator_traits<TIterator>::value_type +{ + return *iterator; +} + +template <class TRange, class TValueGetter> +void TDefaultIntervalFormatter<TRange, TValueGetter>::operator()( + TStringBuilderBase* builder, + const TIterator& first, + const TIterator& last, + const TValueGetter& valueGetter, + bool firstInterval) const +{ + if (!firstInterval) { + builder->AppendString(DefaultJoinToStringDelimiter); + } + + if (first == last) { + builder->AppendFormat("%v", valueGetter(first)); + } else { + builder->AppendFormat("%v-%v", valueGetter(first), valueGetter(last)); + } +} + +template <class TRange, class TValueGetter, class TIntervalFormatter> +TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter> MakeCompactIntervalView( + const TRange& range, + TValueGetter&& valueGetter, + TIntervalFormatter&& intervalFormatter) +{ + return TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>{ + range.begin(), + range.end(), + std::forward<TValueGetter>(valueGetter), + std::forward<TIntervalFormatter>(intervalFormatter)}; +} + template <class... TArgs> TLazyMultiValueFormatter<TArgs...>::TLazyMultiValueFormatter( TStringBuf format, @@ -671,6 +761,27 @@ void FormatValue( const TFormatterWrapper<TFormatter>& wrapper, TStringBuf spec); +// TCompactIntervalView + +template <class TRange, class TValueGetter, class TIntervalFormatter> +concept CCompactIntervalView = requires ( + TRange range, + TValueGetter valueGetter, + TIntervalFormatter intervalFormatter) +{ + { valueGetter(range.begin()) } -> std::integral; + { + intervalFormatter(nullptr, range.begin(), range.begin(), valueGetter, true) + } -> std::same_as<void>; +}; + +template <class TRange, class TValueGetter, class TIntervalFormatter> + requires CCompactIntervalView<TRange, TValueGetter, TIntervalFormatter> +void FormatValue( + TStringBuilderBase* builder, + const TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>& compactIntervalView, + TStringBuf spec); + // TLazyMultiValueFormatter template <class... TArgs> void FormatValue( @@ -773,6 +884,21 @@ void FormatValue( NYT::FormatRange(builder, formattableView, formattableView.Formatter, formattableView.Limit); } +// TCompactIntervalView +template <class TRange, class TValueGetter, class TIntervalFormatter> + requires CCompactIntervalView<TRange, TValueGetter, TIntervalFormatter> +void FormatValue( + TStringBuilderBase* builder, + const TCompactIntervalView<TRange, TValueGetter, TIntervalFormatter>& compactIntervalView, + TStringBuf /*spec*/) +{ + NYT::FormatCompactIntervalRange( + builder, + compactIntervalView, + compactIntervalView.ValueGetter, + compactIntervalView.IntervalFormatter); +} + // TFormatterWrapper template <class TFormatter> void FormatValue( |