-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
DefaultRepository.cs
65 lines (57 loc) · 2.21 KB
/
DefaultRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.
// ReSharper disable once CheckNamespace
namespace Microsoft.Azure.CosmosRepository;
/// <inheritdoc/>
internal sealed partial class DefaultRepository<TItem>(
IOptionsMonitor<RepositoryOptions> optionsMonitor,
ICosmosContainerProvider<TItem> containerProvider,
ILogger<DefaultRepository<TItem>> logger,
ICosmosQueryableProcessor cosmosQueryableProcessor,
IRepositoryExpressionProvider repositoryExpressionProvider,
ISpecificationEvaluator specificationEvaluator) : IRepository<TItem>
where TItem : IItem
{
private (bool OptimizeBandwidth, ItemRequestOptions Options) RequestOptions =>
(optionsMonitor.CurrentValue.OptimizeBandwidth, new ItemRequestOptions
{
EnableContentResponseOnWrite = !optionsMonitor.CurrentValue.OptimizeBandwidth
});
private static void TryLogDebugDetails(ILogger logger, Func<string> getMessage)
{
// ReSharper disable once ConstantConditionalAccessQualifier
if (logger?.IsEnabled(LogLevel.Debug) ?? false)
{
logger.LogDebug("{Msg}", getMessage());
}
}
private static async Task<(List<TItem> items, double charge, string? continuationToken)> GetAllItemsAsync(
IQueryable<TItem> query,
int pageSize,
CancellationToken cancellationToken = default)
{
string? continuationToken = null;
List<TItem> results = [];
var readItemsCount = 0;
double charge = 0;
using var iterator = query.ToFeedIterator();
while (readItemsCount < pageSize && iterator.HasMoreResults)
{
FeedResponse<TItem> next =
await iterator.ReadNextAsync(cancellationToken)
.ConfigureAwait(false);
foreach (TItem result in next)
{
if (readItemsCount == pageSize)
{
break;
}
results.Add(result);
readItemsCount++;
}
charge += next.RequestCharge;
continuationToken = next.ContinuationToken;
}
return (results, charge, continuationToken);
}
}