-
Notifications
You must be signed in to change notification settings - Fork 6
/
ICosmosWriter.cs
317 lines (302 loc) · 15.8 KB
/
ICosmosWriter.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
namespace Atc.Cosmos
{
/// <summary>
/// Represents a writer that can write Cosmos resources.
/// </summary>
/// <typeparam name="T">
/// The type of <see cref="ICosmosResource"/>
/// to be written by this writer.
/// </typeparam>
public interface ICosmosWriter<T>
where T : class, ICosmosResource
{
/// <summary>
/// Creates a new <typeparamref name="T"/> resource in Cosmos.
/// </summary>
/// <remarks>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.Conflict"/>
/// will be thrown if a resource already exists.
/// </remarks>
/// <param name="document">The resource to be created.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the created <typeparamref name="T"/> resource.</returns>
Task<T> CreateAsync(
T document,
CancellationToken cancellationToken = default);
/// <summary>
/// Creates a new <typeparamref name="T"/> resource in Cosmos.
/// </summary>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.Conflict"/>
/// will be thrown if a resource already exists.
/// </remarks>
/// <param name="document">The resource to be created.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task CreateWithNoResponseAsync(
T document,
CancellationToken cancellationToken = default);
/// <summary>
/// Writes a <typeparamref name="T"/> resource to Cosmos, using upsert behavior.
/// </summary>
/// <param name="document">The resource to be written.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the written <typeparamref name="T"/> resource.</returns>
Task<T> WriteAsync(
T document,
CancellationToken cancellationToken = default);
/// <summary>
/// Writes a <typeparamref name="T"/> resource to Cosmos, using upsert behavior.
/// </summary>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// </remarks>
/// <param name="document">The resource to be written.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task WriteWithNoResponseAsync(
T document,
CancellationToken cancellationToken = default);
/// <summary>
/// Replaces a <typeparamref name="T"/> resource in Cosmos.
/// </summary>
/// <remarks>
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.NotFound"/>
/// will be thrown if the resource does not already exist in Cosmos.
/// </para>
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the resource has been updated since it was read
/// (using the <see cref="ICosmosResource.ETag"/> to match the version).
/// </para>
/// </remarks>
/// <param name="document">The resource to be created.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the updated <typeparamref name="T"/> resource.</returns>
Task<T> ReplaceAsync(
T document,
CancellationToken cancellationToken = default);
/// <summary>
/// Replaces a <typeparamref name="T"/> resource in Cosmos.
/// </summary>
/// <remarks>
/// This is optimal for workloads where the returned resource is not used.
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.NotFound"/>
/// will be thrown if the resource does not already exist in Cosmos.
/// </para>
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the resource has been updated since it was read
/// (using the <see cref="ICosmosResource.ETag"/> to match the version).
/// </para>
/// </remarks>
/// <param name="document">The resource to be created.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task ReplaceWithNoResponseAsync(
T document,
CancellationToken cancellationToken = default);
/// <summary>
/// Deletes the specified <typeparamref name="T"/> resource from Cosmos.
/// </summary>
/// <remarks>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.NotFound"/>
/// will be thrown if the resource does not already exist in Cosmos.
/// </remarks>
/// <param name="documentId">Id of the resource.</param>
/// <param name="partitionKey">Partition key of the resource.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public Task DeleteAsync(
string documentId,
string partitionKey,
CancellationToken cancellationToken = default);
/// <summary>
/// Tries to delete the specified <typeparamref name="T"/> resource from Cosmos.
/// </summary>
/// <remarks>
/// When trying to delete a non existing resource, False is returned.
/// </remarks>
/// <param name="documentId">Id of the resource.</param>
/// <param name="partitionKey">Partition key of the resource.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>True if resource was deleted otherwise False.</returns>
public Task<bool> TryDeleteAsync(
string documentId,
string partitionKey,
CancellationToken cancellationToken = default);
#if PREVIEW
/// <summary>
/// Preview Feature DeleteAllItemsByPartitionKey.<br/>
/// Deletes all resources in the Container with the specified <see cref="PartitionKey"/>.
/// Starts an asynchronous Cosmos DB background operation which deletes all resources in the Container with the specified value.
/// The asynchronous Cosmos DB background operation runs using a percentage of user RUs.
/// </summary>
/// <remarks>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.BadRequest"/>
/// will be thrown if the DeleteAllItemsByPartitionKey feature is not enabled.
/// </remarks>
/// <param name="partitionKey">Partition key of the resource.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public Task DeletePartitionAsync(
string partitionKey,
CancellationToken cancellationToken = default);
#endif
/// <summary>
/// Updates a <typeparamref name="T"/> resource that is read from the configured
/// Cosmos collection.
/// </summary>
/// <remarks>
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.NotFound"/>
/// will be thrown if the resource does not already exist in Cosmos.
/// </para>
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the resource is being updated simultanious by another thread
/// and the <paramref name="retries"/> has run out.
/// </para>
/// </remarks>
/// <param name="documentId">Id of the resource.</param>
/// <param name="partitionKey">Partition key of the resource.</param>
/// <param name="updateDocument">Function for applying updates to the resource.</param>
/// <param name="retries">Number of retries when a conflict occurs.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the updated <typeparamref name="T"/> resource.</returns>
Task<T> UpdateAsync(
string documentId,
string partitionKey,
Func<T, Task> updateDocument,
int retries = 0,
CancellationToken cancellationToken = default);
/// <summary>
/// Updates a <typeparamref name="T"/> resource that is read from the configured
/// Cosmos collection.
/// </summary>
/// <remarks>
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.NotFound"/>
/// will be thrown if the resource does not already exist in Cosmos.
/// </para>
/// <para>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the resource is being updated simultanious by another thread
/// and the <paramref name="retries"/> has run out.
/// </para>
/// </remarks>
/// <param name="documentId">Id of the resource.</param>
/// <param name="partitionKey">Partition key of the resource.</param>
/// <param name="updateDocument">Function for applying updates to the resource.</param>
/// <param name="retries">Number of retries when a conflict occurs.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the updated <typeparamref name="T"/> resource.</returns>
Task<T> UpdateAsync(
string documentId,
string partitionKey,
Action<T> updateDocument,
int retries = 0,
CancellationToken cancellationToken = default);
/// <summary>
/// Updates a <typeparamref name="T"/> resource that is read from the configured
/// Cosmos collection, or creates it if it does not exist.
/// </summary>
/// <remarks>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the resource is being updated simultanious by another thread
/// and the <paramref name="retries"/> has run out.
/// </remarks>
/// <param name="getDefaultDocument">Function for creating the default resource. The returned resource need to have the DocumentId and PartitionKey set.</param>
/// <param name="updateDocument">Function for applying updates to the resource.</param>
/// <param name="retries">Number of retries when a conflict occurs.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the updated <typeparamref name="T"/> resource.</returns>
Task<T> UpdateOrCreateAsync(
Func<T> getDefaultDocument,
Func<T, Task> updateDocument,
int retries = 0,
CancellationToken cancellationToken = default);
/// <summary>
/// Updates a <typeparamref name="T"/> resource that is read from the configured
/// Cosmos collection, or creates it if it does not exist.
/// </summary>
/// <remarks>
/// A <see cref="CosmosException"/>
/// with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the resource is being updated simultanious by another thread
/// and the <paramref name="retries"/> has run out.
/// </remarks>
/// <param name="getDefaultDocument">Function for creating the default resource. The returned resource need to have the DocumentId and PartitionKey set.</param>
/// <param name="updateDocument">Function for applying updates to the resource.</param>
/// <param name="retries">Number of retries when a conflict occurs.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the updated <typeparamref name="T"/> resource.</returns>
Task<T> UpdateOrCreateAsync(
Func<T> getDefaultDocument,
Action<T> updateDocument,
int retries = 0,
CancellationToken cancellationToken = default);
/// <summary>
/// Partially update a <typeparamref name="T"/> resource in Cosmos.
/// </summary>
/// <param name="documentId">Id of the resource.</param>
/// <param name="partitionKey">Partition key of the resource.</param>
/// <param name="patchOperations">Represents a list of operations to be sequentially applied to the referred Cosmos resource.</param>
/// <param name="filterPredicate">
/// Condition to be checked before the patch operations in the Azure Cosmos DB service are applied.
/// A <see cref="CosmosException"/> with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the condition is not met.
/// <example><code>FROM c WHERE c.taskNum = 3</code></example>
/// </param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> containing the modified <typeparamref name="T"/> resource.</returns>
Task<T> PatchAsync(
string documentId,
string partitionKey,
IReadOnlyList<PatchOperation> patchOperations,
string? filterPredicate = null,
CancellationToken cancellationToken = default);
/// <summary>
/// Partially update a <typeparamref name="T"/> resource in Cosmos.
/// </summary>
/// <param name="documentId">Id of the resource.</param>
/// <param name="partitionKey">Partition key of the resource.</param>
/// <param name="patchOperations">Represents a list of operations to be sequentially applied to the referred Cosmos resource.</param>
/// <param name="filterPredicate">
/// Condition to be checked before the patch operations in the Azure Cosmos DB service are applied.
/// A <see cref="CosmosException"/> with StatusCode <see cref="HttpStatusCode.PreconditionFailed"/>
/// will be thrown if the condition is not met.
/// <example><code>FROM c WHERE c.taskNum = 3</code></example>
/// </param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> used.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task PatchWithNoResponseAsync(
string documentId,
string partitionKey,
IReadOnlyList<PatchOperation> patchOperations,
string? filterPredicate = null,
CancellationToken cancellationToken = default);
}
}