Skip to content

Commit

Permalink
Merge branch 'first-version' of https://github.com/codibre/dotnet-enu…
Browse files Browse the repository at this point in the history
…merable into first-version
  • Loading branch information
Farenheith committed Nov 9, 2024
2 parents 9657592 + 7c2418b commit f8dd6c6
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 85 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ jobs:
- uses: actions/checkout@v2
# Install the .NET SDK indicated in the global.json file
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
7.x.x
8.x.x
9.x.x
- name: Install dependencies
run: npm ci
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ jobs:
node-version: "lts/*"
# Install the .NET SDK indicated in the global.json file
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
7.x.x
8.x.x
9.x.x
- name: install
run: npm ci
- name: Lint
Expand Down
18 changes: 6 additions & 12 deletions .github/workflows/semantic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ jobs:
- uses: actions/checkout@v4
# Install the .NET SDK indicated in the global.json file
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
7.x.x
8.x.x
9.x.x
- name: Prepare Node
uses: actions/setup-node@v4
Expand All @@ -31,14 +36,6 @@ jobs:
- name: Build
run: npm run build

- name: Prepare Db
run: npm run prepare-test-db

# - name: Docker build
# run: npm run docker:build
# - name: Docker run
# run: npm run docker:run

- name: Test
uses: paambaati/[email protected]
env:
Expand All @@ -56,6 +53,3 @@ jobs:
CI: true
- run: npx semantic-release --ci
- run: git push

- name: Unload Db
run: npm run unload-test-db
13 changes: 6 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ jobs:
node-version: "lts/*"
# Install the .NET SDK indicated in the global.json file
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
7.x.x
8.x.x
9.x.x
- name: Prepare Node
uses: actions/setup-node@v4
Expand All @@ -28,9 +33,6 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Prepare Db
run: npm run prepare-test-db

# - name: Docker build
# run: npm run docker:build
# - name: Docker run
Expand All @@ -43,6 +45,3 @@ jobs:
with:
coverageCommand: dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./coverage/lcov.info
coverageLocations: ${{github.workspace}}/test/Codibre.GrpcSqlProxy.Test/coverage/lcov.info:lcov

- name: Unload Db
run: npm run unload-test-db
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ bin
obj
appsettings.json
coverage
node_modules
node_modules
BenchmarkDotNet.Artifacts
7 changes: 7 additions & 0 deletions Codibre.EnumerableExtensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{86E3225D-3
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codibre.EnumerableExtensions.Branching.Test", "test\Codibre.EnumerableExtensions.Branching.Test\Codibre.EnumerableExtensions.Branching.Test.csproj", "{FA00C287-9BF8-4E3A-B107-CE7E4EAD15B6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codibre.EnumerableExtensions.Branching.Benchmark", "test\Codibre.EnumerableExtensions.Branching.Benchmark\Codibre.EnumerableExtensions.Branching.Benchmark.csproj", "{492A817A-228A-4625-A3F0-FFE492F3D5B4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,9 +30,14 @@ Global
{FA00C287-9BF8-4E3A-B107-CE7E4EAD15B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA00C287-9BF8-4E3A-B107-CE7E4EAD15B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA00C287-9BF8-4E3A-B107-CE7E4EAD15B6}.Release|Any CPU.Build.0 = Release|Any CPU
{492A817A-228A-4625-A3F0-FFE492F3D5B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{492A817A-228A-4625-A3F0-FFE492F3D5B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{492A817A-228A-4625-A3F0-FFE492F3D5B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{492A817A-228A-4625-A3F0-FFE492F3D5B4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{48406767-8859-46F6-9592-1FD21F94ABA6} = {3316A7AD-74AF-49D1-8782-ADFBDF723572}
{FA00C287-9BF8-4E3A-B107-CE7E4EAD15B6} = {86E3225D-3E86-42C2-AA6D-C9EA1B7380B4}
{492A817A-228A-4625-A3F0-FFE492F3D5B4} = {86E3225D-3E86-42C2-AA6D-C9EA1B7380B4}
EndGlobalSection
EndGlobal
57 changes: 29 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
{
"name": "dotnet-enumerable",
"version": "0.0.0",
"description": "Grpc Proxy for Sql Server",
"scripts": {
"build": "dotnet build",
"start": "dotnet start",
"prepare": "husky",
"lint:stage:fix": "lint-staged --relative",
"lint:fix": "dotnet format",
"lint": "dotnet format --verify-no-changes",
"restore": "dotnet restore",
"test": "dotnet test",
"test:coverage": "dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./coverage/lcov.info"
},
"devDependencies": {
"@types/lint-staged": "^13.3.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.10",
"run-container": "^2.0.12"
},
"lint-staged": {
"*.cs": "dotnet format --include"
},
"keywords": [],
"author": "",
"license": "ISC"
}
{
"name": "dotnet-enumerable",
"version": "0.0.0",
"description": "Grpc Proxy for Sql Server",
"scripts": {
"build": "dotnet build",
"start": "dotnet start",
"prepare": "husky",
"lint:stage:fix": "lint-staged --relative",
"lint:fix": "dotnet format",
"lint": "dotnet format --verify-no-changes",
"restore": "dotnet restore",
"test": "dotnet test",
"test:coverage": "dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=lcov /p:CoverletOutput=./coverage/lcov.info",
"benchmark": "dotnet run --project test/Codibre.EnumerableExtensions.Branching.Benchmark/Codibre.EnumerableExtensions.Branching.Benchmark.csproj -c Release"
},
"devDependencies": {
"@types/lint-staged": "^13.3.0",
"husky": "^9.0.11",
"lint-staged": "^15.2.10",
"run-container": "^2.0.12"
},
"lint-staged": {
"*.cs": "dotnet format --include"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -7,5 +7,11 @@
namespace Codibre.EnumerableExtensions.Branching;
public class AsyncBranchingBuilder<T>(IAsyncEnumerable<T> source) : BaseBranchingBuilder<T>
{
protected override IAsyncEnumerable<T> Source { get; } = source;
protected override async ValueTask<Func<IAsyncEnumerable<T>>> GetBranchedEnumerableFactory()
{
var enumerator = source.GetAsyncEnumerator();
LinkedNode<T>? node = await enumerator.MoveNextAsync() ? new(enumerator.Current) : null;
BranchContext context = new();
return () => node.GetBranchedIterable(enumerator, context);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -10,19 +10,17 @@ public abstract class BaseBranchingBuilder<T>
{
private readonly List<Func<IAsyncEnumerable<T>, Task>> _branches = [];

protected abstract IAsyncEnumerable<T> Source { get; }

internal BaseBranchingBuilder<T> Add(Func<IAsyncEnumerable<T>, Task> branch)
{
_branches.Add(branch);
return this;
}

protected abstract ValueTask<Func<IAsyncEnumerable<T>>> GetBranchedEnumerableFactory();

public async Task Run()
{
var enumerator = Source.GetAsyncEnumerator();
LinkedNode<T>? node = await enumerator.MoveNextAsync() ? new(enumerator.Current) : null;
BranchContext context = new();
await Task.WhenAll(_branches.Select((x, index) => Task.Run(() => x(node.GetBranchedAsyncIterable(enumerator, context)))).ToArray());
var get = await GetBranchedEnumerableFactory();
await Task.WhenAll(_branches.Select((x, index) => x(get())));
}
}
4 changes: 2 additions & 2 deletions src/Codibre.EnumerableExtensions.Branching/BranchResult.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Codibre.EnumerableExtensions.Branching;

public class BranchResult<R>
public record BranchResult<R>
{
private bool _set = false;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -8,5 +8,10 @@ namespace Codibre.EnumerableExtensions.Branching;

public class BranchingBuilder<T>(IEnumerable<T> source) : BaseBranchingBuilder<T>
{
protected override IAsyncEnumerable<T> Source { get; } = source.ToAsyncEnumerable();
protected override ValueTask<Func<IAsyncEnumerable<T>>> GetBranchedEnumerableFactory()
{
var enumerator = source.GetEnumerator();
LinkedNode<T>? node = enumerator.MoveNext() ? new(enumerator.Current) : null;
return ValueTask.FromResult(() => node.GetBranchedIterable(enumerator));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ public static class EnumerableExtensions
{
public static AsyncBranchingBuilder<T> Branch<T>(this IAsyncEnumerable<T> enumerable) => new(enumerable);
public static BranchingBuilder<T> Branch<T>(this IEnumerable<T> enumerable) => new(enumerable);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Codibre.EnumerableExtensions.Branching.Internal;
namespace Codibre.EnumerableExtensions.Branching.Internal;

internal class BranchContext()
internal record BranchContext()
{
public Task? Next { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
Expand All @@ -8,7 +8,7 @@ namespace Codibre.EnumerableExtensions.Branching.Internal;

internal static class BranchingHelper
{
internal static async IAsyncEnumerable<T> GetBranchedAsyncIterable<T>(this LinkedNode<T>? node, IAsyncEnumerator<T> enumerator, BranchContext context)
internal static async IAsyncEnumerable<T> GetBranchedIterable<T>(this LinkedNode<T>? node, IEnumerator<T> enumerator)
{
while (node is not null)
{
Expand All @@ -17,26 +17,43 @@ internal static async IAsyncEnumerable<T> GetBranchedAsyncIterable<T>(this Linke
{
lock (enumerator)
{
if (node.Next is null) context.Next ??= GetNext(enumerator, node, context);
if (node.Next is null && enumerator.MoveNext()) node.Next = new(enumerator.Current);
}
if (context.Next is not null) await context.Next;
}
node = node.Next;
await Task.Yield();
}
}

internal static Task GetNext<T>(IAsyncEnumerator<T> enumerator, LinkedNode<T> node, BranchContext context)
=> Task.Run(async () =>
internal static async IAsyncEnumerable<T> GetBranchedIterable<T>(this LinkedNode<T>? node, IAsyncEnumerator<T> enumerator, BranchContext context)
{
while (node is not null)
{
try
{
if (node.Next is null && await enumerator.MoveNextAsync()) node.Next = new(enumerator.Current);
context.Next = null;
}
finally
yield return node.Value;
if (node.Next is null)
{
context.Next = null;
var next = Task.CompletedTask;
lock (enumerator)
{
if (node.Next is null) next = context.Next ??= GetNext(enumerator, node, context);
}
await next;
}
});
node = node.Next;
await Task.Yield();
}
}

internal static async Task GetNext<T>(IAsyncEnumerator<T> enumerator, LinkedNode<T> node, BranchContext context)
{
await Task.Yield();
try
{
if (await enumerator.MoveNextAsync()) node.Next = new(enumerator.Current);
}
finally
{
context.Next = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Codibre.EnumerableExtensions.Branching.Internal;
namespace Codibre.EnumerableExtensions.Branching.Internal;

internal record LinkedNode<T>(T Value)
{
public LinkedNode<T>? Next { get; set; }
}
}
Loading

0 comments on commit f8dd6c6

Please sign in to comment.