11using System ;
22using System . Collections . Generic ;
3+ using System . Runtime . CompilerServices ;
34using System . Threading ;
45using System . Threading . Tasks ;
56using 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 ) ;
0 commit comments