Skip to content
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

Set capacity for TruncatedCollection(IQueryable, int, boolean) overload #1183

Merged
2 changes: 1 addition & 1 deletion sample/BenchmarkServer/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ProductsController(ProductsContext context)
}

[HttpGet]
[EnableQuery]
[EnableQuery(PageSize = 3000)]
public IActionResult Get()
{
return Ok(products);
Expand Down
senioroman4uk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ namespace Microsoft.AspNetCore.OData.Query.Container
/// <typeparam name="T">The collection element type.</typeparam>
public class TruncatedCollection<T> : List<T>, ITruncatedCollection, IEnumerable<T>, ICountOptionCollection
{
// The default capacity of the list.
// https://github.com/microsoft/referencesource/blob/master/mscorlib/system/collections/generic/list.cs#L38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a major issue, I think this is .NET Framework's code. Might be more relevant to reference the current .NET's code: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs#L23

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

private const int DefaultCapacity = 4;
private const int MinPageSize = 1;

private bool _isTruncated;
Expand All @@ -29,8 +32,10 @@ public class TruncatedCollection<T> : List<T>, ITruncatedCollection, IEnumerable
/// <param name="source">The collection to be truncated.</param>
/// <param name="pageSize">The page size.</param>
public TruncatedCollection(IEnumerable<T> source, int pageSize)
: base(source.Take(checked(pageSize + 1)))
: base(checked(pageSize + 1))
{
var items = source.Take(Capacity);
AddRange(items);
Initialize(pageSize);
}

Expand All @@ -54,8 +59,10 @@ public TruncatedCollection(IQueryable<T> source, int pageSize) : this(source, pa
// NOTE: The queryable version calls Queryable.Take which actually gets translated to the backend query where as
// the enumerable version just enumerates and is inefficient.
public TruncatedCollection(IQueryable<T> source, int pageSize, bool parameterize)
: base(Take(source, pageSize, parameterize))
: base(checked(pageSize + 1))
senioroman4uk marked this conversation as resolved.
Show resolved Hide resolved
{
var items = Take(source, pageSize, parameterize);
AddRange(items);
Initialize(pageSize);
}

Expand All @@ -66,8 +73,18 @@ public TruncatedCollection(IQueryable<T> source, int pageSize, bool parameterize
/// <param name="pageSize">The page size.</param>
/// <param name="totalCount">The total count.</param>
public TruncatedCollection(IEnumerable<T> source, int pageSize, long? totalCount)
: base(pageSize > 0 ? source.Take(checked(pageSize + 1)) : source)
: base(pageSize > 0
? checked(pageSize + 1)
: (totalCount > 0 ? (totalCount < int.MaxValue ? (int)totalCount : int.MaxValue) : DefaultCapacity))
{
if (pageSize > 0)
{
AddRange(source.Take(Capacity));
senioroman4uk marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
AddRange(source);
}
senioroman4uk marked this conversation as resolved.
Show resolved Hide resolved
if (pageSize > 0)
{
Initialize(pageSize);
Expand All @@ -84,7 +101,9 @@ public TruncatedCollection(IEnumerable<T> source, int pageSize, long? totalCount
/// <param name="totalCount">The total count.</param>
// NOTE: The queryable version calls Queryable.Take which actually gets translated to the backend query where as
// the enumerable version just enumerates and is inefficient.
public TruncatedCollection(IQueryable<T> source, int pageSize, long? totalCount) : this(source, pageSize, totalCount, false)
[Obsolete("should not used should be removed in the next major version")]
senioroman4uk marked this conversation as resolved.
Show resolved Hide resolved
public TruncatedCollection(IQueryable<T> source, int pageSize, long? totalCount) : this(source, pageSize,
totalCount, false)
{
}

Expand All @@ -97,6 +116,7 @@ public TruncatedCollection(IQueryable<T> source, int pageSize, long? totalCount)
/// <param name="parameterize">Flag indicating whether constants should be parameterized</param>
// NOTE: The queryable version calls Queryable.Take which actually gets translated to the backend query where as
// the enumerable version just enumerates and is inefficient.
[Obsolete("should not be used should be removed in the next major version")]
public TruncatedCollection(IQueryable<T> source, int pageSize, long? totalCount, bool parameterize)
senioroman4uk marked this conversation as resolved.
Show resolved Hide resolved
: base(pageSize > 0 ? Take(source, pageSize, parameterize) : source)
{
Expand Down Expand Up @@ -152,4 +172,4 @@ public long? TotalCount
get { return _totalCount; }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//------------------------------------------------------------------------------

using System;
using Microsoft.AspNetCore.OData.Query.Container;
using Microsoft.AspNetCore.OData.Tests.Commons;
using System.Collections.Generic;
Expand Down Expand Up @@ -50,6 +51,7 @@ public void CtorTruncatedCollection_SetsProperties()
}

[Fact]
[Obsolete]
public void CtorTruncatedCollection_WithQueryable_SetsProperties()
{
// Arrange & Act
Expand Down
Loading