Skip to content

Commit 694e977

Browse files
borisahrensBoris Ahrens
andauthored
Use MongoDB linq AsyncEnumerable library (#308)
* Use Linq.AsyncEnumerable library for Mongo --------- Co-authored-by: Boris Ahrens <[email protected]>
1 parent 8310064 commit 694e977

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

src/MalwareSampleExchange.Console/Database/MongoMetadataHandler.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Runtime.CompilerServices;
43
using System.Threading;
54
using System.Threading.Tasks;
65
using Microsoft.Extensions.Hosting;
76
using Microsoft.Extensions.Options;
87
using MongoDB.Driver;
98
using MalwareSampleExchange.Console.Models;
9+
using MongoDB.Driver.Linq;
1010

1111
namespace MalwareSampleExchange.Console.Database;
1212

@@ -36,25 +36,18 @@ public MongoMetadataHandler(IOptions<MongoMetadataOptions> options)
3636
_mongoClient = new MongoClient(options.Value.ConnectionString);
3737
}
3838

39-
public async IAsyncEnumerable<ExportSample> GetSamplesAsync(DateTime start, DateTime? end, string? sampleSet, [EnumeratorCancellation] CancellationToken token = default)
39+
public IAsyncEnumerable<ExportSample> GetSamplesAsync(DateTime start, DateTime? end, string? sampleSet, CancellationToken token = default)
4040
{
4141
var mongoDatabase = _mongoClient.GetDatabase(_options.DatabaseName);
4242
var sampleCollection = mongoDatabase.GetCollection<ExportSample>(_options.CollectionName);
4343
var list = end == null
44-
? await sampleCollection
45-
.FindAsync(sample => sample.SampleSet == sampleSet && sample.Imported >= start && sample.DoNotUseBefore <= DateTime.Now, cancellationToken: token)
46-
: await sampleCollection
47-
.FindAsync(sample => sample.SampleSet == sampleSet && sample.Imported >= start && sample.Imported <= end && sample.DoNotUseBefore <= DateTime.Now, cancellationToken: token);
48-
49-
while (await list.MoveNextAsync(token))
50-
{
51-
foreach (var current in list.Current)
52-
{
53-
yield return current;
54-
}
55-
}
44+
? sampleCollection
45+
.Find(sample => sample.SampleSet == sampleSet && sample.Imported >= start && sample.DoNotUseBefore <= DateTime.Now).ToAsyncEnumerable(cancellationToken: token)
46+
: sampleCollection
47+
.Find(sample => sample.SampleSet == sampleSet && sample.Imported >= start && sample.Imported <= end && sample.DoNotUseBefore <= DateTime.Now).ToAsyncEnumerable(cancellationToken: token);
48+
return list;
5649
}
57-
50+
5851
public async Task InsertSampleAsync(RequestExportSample sample, CancellationToken token = default)
5952
{
6053
var mongoDatabase = _mongoClient.GetDatabase(_options.DatabaseName);

src/MalwareSampleExchange.Console/ListRequester/ListRequester.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Runtime.CompilerServices;
55
using System.Threading;
6-
using System.Threading.Tasks;
76
using JWT.Algorithms;
87
using JWT.Builder;
98
using Microsoft.Extensions.Logging;

src/MalwareSampleExchange.Console/MalwareSampleExchange.Console.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="JWT" Version="8.9.0" />
99
<PackageReference Include="AWSSDK.S3" Version="3.7.8.5" />
10+
<PackageReference Include="MongoDB.Driver.Linq.AsyncEnumerable" Version="2.15.4" />
1011
<PackageReference Include="Prometheus.Client.AspNetCore" Version="4.5.1" />
1112
<PackageReference Include="Prometheus.Client.HttpRequestDurations" Version="3.4.1" />
1213
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.3" />

0 commit comments

Comments
 (0)