Skip to content

Commit 8310064

Browse files
borisahrensBoris Ahrens
andauthored
Iterate in IAsyncenumerable over DB results (#307)
* Iterate in IAsyncenumerable over DB results --------- Co-authored-by: Boris Ahrens <[email protected]>
1 parent 1c72d7e commit 8310064

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/MalwareSampleExchange.Console/Database/MongoMetadataHandler.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using Microsoft.Extensions.Hosting;
@@ -19,7 +20,7 @@ public interface ISampleMetadataHandler
1920
/// <param name="sampleSet"></param>
2021
/// <param name="token"></param>
2122
/// <returns></returns>
22-
Task<IEnumerable<ExportSample>> GetSamplesAsync(DateTime start, DateTime? end, string? sampleSet, CancellationToken token = default);
23+
IAsyncEnumerable<ExportSample> GetSamplesAsync(DateTime start, DateTime? end, string? sampleSet, CancellationToken token = default);
2324

2425
Task InsertSampleAsync(RequestExportSample sample, CancellationToken token = default);
2526
}
@@ -35,18 +36,25 @@ public MongoMetadataHandler(IOptions<MongoMetadataOptions> options)
3536
_mongoClient = new MongoClient(options.Value.ConnectionString);
3637
}
3738

38-
public async Task<IEnumerable<ExportSample>> GetSamplesAsync(DateTime start, DateTime? end, string? sampleSet, CancellationToken token = default)
39+
public async IAsyncEnumerable<ExportSample> GetSamplesAsync(DateTime start, DateTime? end, string? sampleSet, [EnumeratorCancellation] CancellationToken token = default)
3940
{
4041
var mongoDatabase = _mongoClient.GetDatabase(_options.DatabaseName);
4142
var sampleCollection = mongoDatabase.GetCollection<ExportSample>(_options.CollectionName);
4243
var list = end == null
4344
? await sampleCollection
44-
.FindAsync(sample => sample.SampleSet == sampleSet && sample.Imported >= start, cancellationToken: token)
45+
.FindAsync(sample => sample.SampleSet == sampleSet && sample.Imported >= start && sample.DoNotUseBefore <= DateTime.Now, cancellationToken: token)
4546
: await sampleCollection
46-
.FindAsync(sample => sample.SampleSet == sampleSet && sample.Imported >= start && sample.Imported <= end, cancellationToken: token);
47-
return list.ToList(cancellationToken: token);
48-
}
47+
.FindAsync(sample => sample.SampleSet == sampleSet && sample.Imported >= start && sample.Imported <= end && sample.DoNotUseBefore <= DateTime.Now, cancellationToken: token);
4948

49+
while (await list.MoveNextAsync(token))
50+
{
51+
foreach (var current in list.Current)
52+
{
53+
yield return current;
54+
}
55+
}
56+
}
57+
5058
public async Task InsertSampleAsync(RequestExportSample sample, CancellationToken token = default)
5159
{
5260
var mongoDatabase = _mongoClient.GetDatabase(_options.DatabaseName);

src/MalwareSampleExchange.Console/ListRequester/ListRequester.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ public async IAsyncEnumerable<Token> RequestList(string username, DateTime start
4343
{
4444
var includeFamilyName = _partnerProvider.Partners.Single(_ => _.Name == username).IncludeFamilyName;
4545
var sampleSet = _partnerProvider.Partners.SingleOrDefault(_ => _.Name == username)?.Sampleset;
46+
var samples = _sampleMetadataHandler.GetSamplesAsync(start, end, sampleSet, token);
4647

47-
var samples = await _sampleMetadataHandler.GetSamplesAsync(start, end, sampleSet, token);
48-
49-
foreach (var sample in samples.Where(sample => sample.DoNotUseBefore <= DateTime.Now))
48+
await foreach (var sample in samples)
5049
{
5150
var fileSize = sample.FileSize == 0
5251
? await _sampleStorageHandler.GetFileSizeAsync(sample.Sha256, token)

0 commit comments

Comments
 (0)