-
Notifications
You must be signed in to change notification settings - Fork 178
Fix/Improve TruncatedCollectionOfT #1492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
WanjohiSammy
wants to merge
8
commits into
main
Choose a base branch
from
fix/improve-truncatedcollection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4ad1d87
trying optimize TruncatedCollectionOfT and add support for IAsyncEnum…
WanjohiSammy a3b8eb7
refactor
WanjohiSammy 359c2f9
Add some tests
WanjohiSammy dec2126
Refactor
WanjohiSammy e54873d
Try handling IAsyncEnumerable
WanjohiSammy f2ef421
Update src/Microsoft.AspNetCore.OData/Query/Container/TruncatedCollec…
WanjohiSammy 9a885d9
some refactor
WanjohiSammy 35a0ea9
Merge branch 'fix/improve-truncatedcollection' of https://github.com/…
WanjohiSammy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Microsoft.AspNetCore.OData/Query/Container/TruncatedAsyncEnumerableOfT.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="TruncatedAsyncEnumerableOfT.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.OData.Query.Container; | ||
|
||
public class TruncatedAsyncEnumerable<T> : IAsyncEnumerable<T> | ||
{ | ||
private readonly IAsyncEnumerable<T> _source; | ||
private readonly int _pageSize; | ||
private readonly TruncationState _state; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="TruncatedAsyncEnumerable{T}"/> class, which provides an | ||
/// asynchronous enumerable that limits the number of items returned per page and tracks truncation state. | ||
/// </summary> | ||
/// <param name="source">The source asynchronous enumerable to be paginated and truncated.</param> | ||
/// <param name="pageSize">The maximum number of items to include in each page. Must be greater than zero.</param> | ||
/// <param name="state">The truncation state object used to track whether the enumeration was truncated.</param> | ||
public TruncatedAsyncEnumerable(IAsyncEnumerable<T> source, int pageSize, TruncationState state) | ||
{ | ||
_source = source; | ||
_pageSize = pageSize; | ||
_state = state; | ||
} | ||
|
||
/// <summary> | ||
/// Returns an asynchronous enumerator that iterates through the items in the source collection, up to a specified page size. | ||
/// </summary> | ||
/// <remarks>The enumerator yields items from the source collection until the specified page size is reached. | ||
/// If the number of items exceeds the page size, the enumeration is truncated, and the state is updated to true. Otherwise, the state is updated to false.</remarks> | ||
/// <param name="cancellationToken">A token to monitor for cancellation requests. If the token is canceled, the enumeration is stopped.</param> | ||
/// <returns>An asynchronous enumerator that yields items from the source collection, up to the specified page size.</returns> | ||
public async IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) | ||
{ | ||
int count = 0; | ||
await foreach (var item in _source.WithCancellation(cancellationToken)) | ||
{ | ||
if (count < _pageSize) | ||
{ | ||
yield return item; | ||
count++; | ||
} | ||
else | ||
{ | ||
// More items exist than pageSize, so mark as truncated and stop yielding. | ||
_state.IsTruncated = true; | ||
yield break; | ||
} | ||
} | ||
|
||
// If we didn't hit the limit, not truncated. | ||
_state.IsTruncated = false; | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Used to track the truncation state of an async enumerable. | ||
/// </summary> | ||
public class TruncationState | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why have this a separate class instead of a boolean field of the |
||
{ | ||
public bool IsTruncated { get; set; } | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.