Skip to content

Commit 0f96370

Browse files
Remove morelinq (#1180)
* Remove morelinq * undo
1 parent 75cf70f commit 0f96370

File tree

8 files changed

+153
-16
lines changed

8 files changed

+153
-16
lines changed

Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
2626
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
2727
<PackageVersion Include="Moq" Version="4.20.72" />
28-
<PackageVersion Include="morelinq" Version="3.4.2" />
2928
<PackageVersion Include="Microsoft.SqlServer.TransactSql.ScriptDom" Version="161.9135.0" />
3029
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
3130
<PackageVersion Include="System.Drawing.Common" Version="6.0.0" />

performance/packages.lock.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,12 +2074,6 @@
20742074
"Castle.Core": "5.1.1"
20752075
}
20762076
},
2077-
"morelinq": {
2078-
"type": "CentralTransitive",
2079-
"requested": "[3.4.2, )",
2080-
"resolved": "3.4.2",
2081-
"contentHash": "nKdpt7Ai+xQO8PZ0YFTn13INGJcKO0Nx65kO/ut0zaDirRo6d7atedmW2l68YB3x7U4pOqTLdMfYsBys8KxA1Q=="
2082-
},
20832077
"Newtonsoft.Json": {
20842078
"type": "CentralTransitive",
20852079
"requested": "[13.0.3, )",
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
8+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Common
9+
{
10+
public static class IEnumerableExtensions
11+
{
12+
/// <summary>
13+
/// Batches the source sequence into sized buckets and applies a projection to each bucket.
14+
/// </summary>
15+
/// <typeparam name="TSource">Type of elements in <paramref name="source"/> sequence.</typeparam>
16+
/// <param name="source">The source sequence.</param>
17+
/// <param name="size">Size of buckets.</param>
18+
public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
19+
this IEnumerable<TSource> source, int size)
20+
{
21+
if (source == null)
22+
{
23+
throw new ArgumentNullException(nameof(source));
24+
}
25+
26+
if (size <= 0)
27+
{
28+
throw new ArgumentOutOfRangeException(nameof(size));
29+
}
30+
31+
TSource[] bucket = null;
32+
int count = 0;
33+
34+
foreach (TSource item in source)
35+
{
36+
if (bucket == null)
37+
{
38+
bucket = new TSource[size];
39+
}
40+
41+
bucket[count++] = item;
42+
if (count != size)
43+
{
44+
continue;
45+
}
46+
47+
yield return bucket;
48+
49+
bucket = null;
50+
count = 0;
51+
}
52+
53+
if (bucket != null && count > 0)
54+
{
55+
yield return bucket.Take(count).ToArray();
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Returns a specified number of contiguous elements from the end of
61+
/// a sequence.
62+
/// </summary>
63+
/// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
64+
/// <param name="source">The sequence to return the last element of.</param>
65+
/// <param name="count">The number of elements to return.</param>
66+
/// <returns>
67+
/// An <see cref="IEnumerable{T}"/> that contains the specified number of
68+
/// elements from the end of the input sequence.
69+
/// </returns>
70+
public static IEnumerable<TSource> TakeLast<TSource>(this IEnumerable<TSource> source, int count)
71+
{
72+
if (source == null)
73+
{
74+
throw new ArgumentNullException(nameof(source));
75+
}
76+
77+
return source.Skip(Math.Max(0, source.Count() - count));
78+
}
79+
}
80+
}

src/Microsoft.Azure.WebJobs.Extensions.Sql.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
<PackageReference Include="Microsoft.Data.SqlClient" />
2323
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
2424
<PackageReference Include="Microsoft.SqlServer.TransactSql.ScriptDom" />
25-
<PackageReference Include="morelinq" />
2625
<PackageReference Include="System.Runtime.Caching" />
2726
<PackageReference Include="Newtonsoft.Json" />
2827
<PackageReference Include="Microsoft.AspNetCore.Http" />

src/SqlAsyncCollector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.Data.SqlClient;
1515
using Microsoft.Extensions.Configuration;
1616
using Microsoft.Extensions.Logging;
17-
using MoreLinq;
1817
using Newtonsoft.Json;
1918
using Newtonsoft.Json.Serialization;
2019
using Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry;
@@ -23,6 +22,7 @@
2322
using static Microsoft.Azure.WebJobs.Extensions.Sql.SqlBindingConstants;
2423
using static Microsoft.Azure.WebJobs.Extensions.Sql.SqlBindingUtilities;
2524
using static Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry.Telemetry;
25+
using Microsoft.Azure.WebJobs.Extensions.Sql.Common;
2626

2727
namespace Microsoft.Azure.WebJobs.Extensions.Sql
2828
{

src/TriggerBinding/SqlTriggerScaleMonitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using static Microsoft.Azure.WebJobs.Extensions.Sql.Telemetry.Telemetry;
1010
using Microsoft.Azure.WebJobs.Host.Scale;
1111
using Microsoft.Extensions.Logging;
12-
using MoreLinq;
12+
using Microsoft.Azure.WebJobs.Extensions.Sql.Common;
1313

1414
namespace Microsoft.Azure.WebJobs.Extensions.Sql
1515
{

src/packages.lock.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@
115115
"resolved": "161.9135.0",
116116
"contentHash": "Ayubg3Qijaysyn/fJ0QMhv+ADTl5+nPcqE2KsvcIlMZGmSSRAKMxApVf947bLWNRmBIr2TlAS8cSA+cFQUZz1g=="
117117
},
118-
"morelinq": {
119-
"type": "Direct",
120-
"requested": "[3.4.2, )",
121-
"resolved": "3.4.2",
122-
"contentHash": "nKdpt7Ai+xQO8PZ0YFTn13INGJcKO0Nx65kO/ut0zaDirRo6d7atedmW2l68YB3x7U4pOqTLdMfYsBys8KxA1Q=="
123-
},
124118
"NETStandard.Library": {
125119
"type": "Direct",
126120
"requested": "[2.0.3, )",
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Microsoft.Azure.WebJobs.Extensions.Sql.Common;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using Xunit;
9+
10+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Unit
11+
{
12+
public class IEnumerableExtensionsTests
13+
{
14+
public static readonly TheoryData<int[], int> BatchData = new()
15+
{
16+
{ new int[] { 1, 2, 3, 4, 5 }, 1 }, // One by one
17+
{ new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, 3 }, // Bigger non-single batch
18+
{ new int[] { 1, 2, 3, 4, 5 } , 5 }, // All one batch
19+
{ new int[] { 1 }, 2 }, // Batch size greater than array
20+
};
21+
22+
[Theory]
23+
[MemberData(nameof(BatchData))]
24+
public void Batch(IEnumerable<int> array, int batchSize)
25+
{
26+
int totalCount = 0;
27+
foreach (IEnumerable<int> batch in array.Batch(batchSize))
28+
{
29+
int batchCount = batch.Count();
30+
totalCount += batchCount;
31+
Assert.True(batch.Count() <= batchSize);
32+
}
33+
Assert.Equal(totalCount, array.Count());
34+
}
35+
36+
[Fact]
37+
public void Batch_Invalid()
38+
{
39+
// Array must be non-null
40+
Assert.ThrowsAny<Exception>(() => IEnumerableExtensions.Batch<int>(null, 0).Count());
41+
42+
// Size must be >= 1
43+
Assert.ThrowsAny<Exception>(() => IEnumerableExtensions.Batch(new int[] { 1, 2, 3 }, 0).Count());
44+
Assert.ThrowsAny<Exception>(() => IEnumerableExtensions.Batch(new int[] { 1, 2, 3 }, -1).Count());
45+
}
46+
47+
public static readonly TheoryData<int[], int, int[]> TakeLastData = new()
48+
{
49+
{ new int[] { 1, 2, 3, 4, 5 }, 1 , new int[] { 5 } }, // Take only last number
50+
{ new int[] { 1, 2, 3, 4, 5 }, 3 , new int[] { 3, 4, 5 } }, // Take some middle set of numbers
51+
{ new int[] { 1, 2, 3, 4, 5 }, 6 , new int[] { 1, 2, 3, 4, 5 } }, // Take more than exists in array
52+
{ new int[] { 1, 2, 3, 4, 5 }, 0 , Array.Empty<int>() }, // No numbers
53+
{ new int[] { 1, 2, 3, 4, 5 }, 0 , Array.Empty<int>() }, // Negative numbers (returns empty)
54+
};
55+
56+
[Theory]
57+
[MemberData(nameof(TakeLastData))]
58+
public void TakeLast(IEnumerable<int> array, int takeCount, IEnumerable<int> expectedValues)
59+
{
60+
IEnumerable<int> taken = IEnumerableExtensions.TakeLast(array, takeCount);
61+
Assert.Equal(taken, expectedValues);
62+
}
63+
64+
[Fact]
65+
public void TakeLast_Invalid()
66+
{
67+
// IEnumerable must be non-null
68+
Assert.ThrowsAny<Exception>(() => { IEnumerableExtensions.TakeLast<int>(null, 0); });
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)