From 4bc2ca5f76e5aaf2718e8ea72a30d12b3f1304a0 Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Sun, 15 Dec 2024 21:09:41 +0800 Subject: [PATCH] refactor: update InMemoryStream --- src/WeihanLi.Common/Helpers/InMemoryStream.cs | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/WeihanLi.Common/Helpers/InMemoryStream.cs b/src/WeihanLi.Common/Helpers/InMemoryStream.cs index 4c265f14..67300510 100644 --- a/src/WeihanLi.Common/Helpers/InMemoryStream.cs +++ b/src/WeihanLi.Common/Helpers/InMemoryStream.cs @@ -1,3 +1,6 @@ +// Copyright (c) Weihan Li. All rights reserved. +// Licensed under the Apache license. + using System.Collections.Concurrent; using System.Runtime.CompilerServices; using WeihanLi.Common.Models; @@ -151,16 +154,28 @@ public Task>> GroupsAsync(CancellationTok public Task> InfoAsync(CancellationToken cancellationToken = default) { - var minMessage = _messages.MinBy(item => item.Id); - var maxMessage = _messages.MaxBy(item => item.Id); var streamInfo = new StreamInfo { - MinId = minMessage?.Id ?? default, - MinTimestamp = minMessage?.Timestamp ?? default, - MaxId = maxMessage?.Id ?? default, - MaxTimestamp = maxMessage?.Timestamp ?? default, + MinId = default, + MinTimestamp = default, + MaxId = default, + MaxTimestamp = default, Count = _messages.Count }; + + if (_messages.Count > 0) + { + var minMessage = _messages.MinBy(item => item.Id, _comparer); + var maxMessage = _messages.MaxBy(item => item.Id, _comparer); + ArgumentNullException.ThrowIfNull(minMessage); + ArgumentNullException.ThrowIfNull(maxMessage); + + streamInfo.MinId = minMessage.Id; + streamInfo.MinTimestamp = minMessage.Timestamp; + streamInfo.MaxId = maxMessage.Id; + streamInfo.MaxTimestamp = maxMessage.Timestamp; + } + return streamInfo.WrapTask(); }