-
Notifications
You must be signed in to change notification settings - Fork 773
Expand file tree
/
Copy pathTablePackageService.cs
More file actions
283 lines (241 loc) · 10.2 KB
/
TablePackageService.cs
File metadata and controls
283 lines (241 loc) · 10.2 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using BaGet.Core;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NuGet.Versioning;
namespace BaGet.Azure
{
/// <summary>
/// Stores the metadata of packages using Azure Table Storage.
/// </summary>
public partial class TablePackageService : IPackageService
{
private const string TableName = "Packages";
private const int MaxPreconditionFailures = 5;
private readonly TableOperationBuilder _operationBuilder;
private readonly CloudTable _table;
private readonly ILogger<TablePackageService> _logger;
public TablePackageService(
TableOperationBuilder operationBuilder,
CloudTableClient client,
ILogger<TablePackageService> logger)
{
_operationBuilder = operationBuilder ?? throw new ArgumentNullException(nameof(operationBuilder));
_table = client?.GetTableReference(TableName) ?? throw new ArgumentNullException(nameof(client));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<PackageAddResult> AddAsync(Package package, CancellationToken cancellationToken)
{
try
{
var operation = _operationBuilder.AddPackage(package);
await _table.ExecuteAsync(operation, cancellationToken);
}
catch (StorageException e) when (e.IsAlreadyExistsException())
{
return PackageAddResult.PackageAlreadyExists;
}
return PackageAddResult.Success;
}
public async Task<bool> AddDownloadAsync(
string id,
NuGetVersion version,
CancellationToken cancellationToken)
{
var attempt = 0;
while (true)
{
try
{
var operation = TableOperation.Retrieve<PackageDownloadsEntity>(
id.ToLowerInvariant(),
version.ToNormalizedString().ToLowerInvariant());
var result = await _table.ExecuteAsync(operation);
var entity = result.Result as PackageDownloadsEntity;
if (entity == null)
{
return false;
}
entity.Downloads += 1;
await _table.ExecuteAsync(TableOperation.Merge(entity), cancellationToken);
return true;
}
catch (StorageException e)
when (attempt < MaxPreconditionFailures && e.IsPreconditionFailedException())
{
_logger.LogWarning(
e,
$"Retrying due to precondition failure, attempt {{Attempt}} of {MaxPreconditionFailures}..",
attempt);
}
}
}
public async Task<bool> ExistsAsync(string id, CancellationToken cancellationToken)
{
var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, id.ToLowerInvariant());
var query = new TableQuery<PackageEntity>()
.Select(MinimalColumnSet)
.Where(filter)
.Take(1);
var result = await _table.ExecuteQuerySegmentedAsync(query, token: null, cancellationToken);
return result.Results.Any();
}
public async Task<bool> ExistsAsync(
string id,
NuGetVersion version,
CancellationToken cancellationToken)
{
var operation = TableOperation.Retrieve<PackageEntity>(
id.ToLowerInvariant(),
version.ToNormalizedString().ToLowerInvariant(),
MinimalColumnSet);
var result = await _table.ExecuteAsync(operation, cancellationToken);
var entity = result.Result as PackageEntity;
return entity != null;
}
public async Task<IReadOnlyList<Package>> FindAsync(string id, bool includeUnlisted, CancellationToken cancellationToken)
{
var filter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, id.ToLowerInvariant());
if (!includeUnlisted)
{
filter = TableQuery.CombineFilters(
filter,
TableOperators.And,
TableQuery.GenerateFilterConditionForBool(nameof(PackageEntity.Listed), QueryComparisons.Equal, true));
}
var query = new TableQuery<PackageEntity>().Where(filter);
var results = new List<Package>();
// Request 500 results at a time from the server.
TableContinuationToken token = null;
query.TakeCount = 500;
do
{
var segment = await _table.ExecuteQuerySegmentedAsync(query, token, cancellationToken);
token = segment.ContinuationToken;
// Write out the properties for each entity returned.
results.AddRange(segment.Results.Select(AsPackage));
}
while (token != null);
return results.OrderBy(p => p.Version).ToList();
}
public async Task<Package> FindOrNullAsync(
string id,
NuGetVersion version,
bool includeUnlisted,
CancellationToken cancellationToken)
{
var operation = TableOperation.Retrieve<PackageEntity>(
id.ToLowerInvariant(),
version.ToNormalizedString().ToLowerInvariant());
var result = await _table.ExecuteAsync(operation, cancellationToken);
var entity = result.Result as PackageEntity;
if (entity == null)
{
return null;
}
// Filter out the package if it's unlisted.
if (!includeUnlisted && !entity.Listed)
{
return null;
}
return AsPackage(entity);
}
public async Task<bool> HardDeletePackageAsync(string id, NuGetVersion version, CancellationToken cancellationToken)
{
return await TryUpdatePackageAsync(
_operationBuilder.HardDeletePackage(id, version),
cancellationToken);
}
public async Task<bool> RelistPackageAsync(string id, NuGetVersion version, CancellationToken cancellationToken)
{
return await TryUpdatePackageAsync(
_operationBuilder.RelistPackage(id, version),
cancellationToken);
}
public async Task<bool> UnlistPackageAsync(string id, NuGetVersion version, CancellationToken cancellationToken)
{
return await TryUpdatePackageAsync(
_operationBuilder.UnlistPackage(id, version),
cancellationToken);
}
private List<string> MinimalColumnSet => new List<string> { "PartitionKey" };
private async Task<bool> TryUpdatePackageAsync(TableOperation operation, CancellationToken cancellationToken)
{
try
{
await _table.ExecuteAsync(operation, cancellationToken);
}
catch (StorageException e) when (e.IsNotFoundException())
{
return false;
}
return true;
}
private Package AsPackage(PackageEntity entity)
{
var targetFrameworks = JsonConvert.DeserializeObject<List<string>>(entity.TargetFrameworks)
.Select(f => new TargetFramework { Moniker = f })
.ToList();
return new Package
{
Id = entity.Identifier,
NormalizedVersionString = entity.NormalizedVersion,
OriginalVersionString = entity.OriginalVersion,
Authors = JsonConvert.DeserializeObject<string[]>(entity.Authors),
Description = entity.Description,
Downloads = entity.Downloads,
HasReadme = entity.HasReadme,
HasEmbeddedIcon = entity.HasEmbeddedIcon,
IsPrerelease = entity.IsPrerelease,
Language = entity.Language,
Listed = entity.Listed,
MinClientVersion = entity.MinClientVersion,
Published = entity.Published,
RequireLicenseAcceptance = entity.RequireLicenseAcceptance,
SemVerLevel = (SemVerLevel)entity.SemVerLevel,
Summary = entity.Summary,
Title = entity.Title,
ReleaseNotes = entity.ReleaseNotes,
IconUrl = ParseUri(entity.IconUrl),
LicenseUrl = ParseUri(entity.LicenseUrl),
ProjectUrl = ParseUri(entity.ProjectUrl),
RepositoryUrl = ParseUri(entity.RepositoryUrl),
RepositoryType = entity.RepositoryType,
Tags = JsonConvert.DeserializeObject<string[]>(entity.Tags),
Dependencies = ParseDependencies(entity.Dependencies),
PackageTypes = ParsePackageTypes(entity.PackageTypes),
TargetFrameworks = targetFrameworks,
};
}
private Uri ParseUri(string input)
{
return string.IsNullOrEmpty(input) ? null : new Uri(input);
}
private List<PackageDependency> ParseDependencies(string input)
{
return JsonConvert.DeserializeObject<List<DependencyModel>>(input)
.Select(e => new PackageDependency
{
Id = e.Id,
VersionRange = e.VersionRange,
TargetFramework = e.TargetFramework,
})
.ToList();
}
private List<PackageType> ParsePackageTypes(string input)
{
return JsonConvert.DeserializeObject<List<PackageTypeModel>>(input)
.Select(e => new PackageType
{
Name = e.Name,
Version = e.Version
})
.ToList();
}
}
}