-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathBatchCore.cs
255 lines (219 loc) · 9.48 KB
/
BatchCore.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
internal class BatchCore : TransactionalBatch
{
private readonly PartitionKey partitionKey;
private readonly ContainerCore container;
private List<ItemBatchOperation> operations;
/// <summary>
/// Initializes a new instance of the <see cref="BatchCore"/> class.
/// </summary>
/// <param name="container">Container that has items on which batch operations are to be performed.</param>
/// <param name="partitionKey">The partition key for all items in the batch. <see cref="PartitionKey"/>.</param>
internal BatchCore(
ContainerCore container,
PartitionKey partitionKey)
{
this.container = container;
this.partitionKey = partitionKey;
this.operations = new List<ItemBatchOperation>();
}
public override TransactionalBatch CreateItem<T>(
T item,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
this.operations.Add(new ItemBatchOperation<T>(
operationType: OperationType.Create,
operationIndex: this.operations.Count,
resource: item,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override TransactionalBatch CreateItemStream(
Stream streamPayload,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (streamPayload == null)
{
throw new ArgumentNullException(nameof(streamPayload));
}
this.operations.Add(new ItemBatchOperation(
operationType: OperationType.Create,
operationIndex: this.operations.Count,
resourceStream: streamPayload,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override TransactionalBatch ReadItem(
string id,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
this.operations.Add(new ItemBatchOperation(
operationType: OperationType.Read,
operationIndex: this.operations.Count,
id: id,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override TransactionalBatch UpsertItem<T>(
T item,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
this.operations.Add(new ItemBatchOperation<T>(
operationType: OperationType.Upsert,
operationIndex: this.operations.Count,
resource: item,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override TransactionalBatch UpsertItemStream(
Stream streamPayload,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (streamPayload == null)
{
throw new ArgumentNullException(nameof(streamPayload));
}
this.operations.Add(new ItemBatchOperation(
operationType: OperationType.Upsert,
operationIndex: this.operations.Count,
resourceStream: streamPayload,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override TransactionalBatch ReplaceItem<T>(
string id,
T item,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
if (item == null)
{
throw new ArgumentNullException(nameof(item));
}
this.operations.Add(new ItemBatchOperation<T>(
operationType: OperationType.Replace,
operationIndex: this.operations.Count,
id: id,
resource: item,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override TransactionalBatch ReplaceItemStream(
string id,
Stream streamPayload,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
if (streamPayload == null)
{
throw new ArgumentNullException(nameof(streamPayload));
}
this.operations.Add(new ItemBatchOperation(
operationType: OperationType.Replace,
operationIndex: this.operations.Count,
id: id,
resourceStream: streamPayload,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override TransactionalBatch DeleteItem(
string id,
TransactionalBatchItemRequestOptions requestOptions = null)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
this.operations.Add(new ItemBatchOperation(
operationType: OperationType.Delete,
operationIndex: this.operations.Count,
id: id,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
public override Task<TransactionalBatchResponse> ExecuteAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
return this.ExecuteAsync(
requestOptions: null,
cancellationToken: cancellationToken);
}
/// <summary>
/// Executes the batch at the Azure Cosmos service as an asynchronous operation.
/// </summary>
/// <param name="requestOptions">Options that apply to the batch. Used only for EPK routing.</param>
/// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
/// <returns>An awaitable <see cref="TransactionalBatchResponse"/> which contains the completion status and results of each operation.</returns>
public virtual Task<TransactionalBatchResponse> ExecuteAsync(
RequestOptions requestOptions,
CancellationToken cancellationToken = default(CancellationToken))
{
CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(requestOptions);
BatchExecutor executor = new BatchExecutor(
container: this.container,
partitionKey: this.partitionKey,
operations: this.operations,
batchOptions: requestOptions,
diagnosticsContext: diagnosticsContext);
this.operations = new List<ItemBatchOperation>();
return executor.ExecuteAsync(cancellationToken);
}
/// <summary>
/// Adds an operation to patch an item into the batch.
/// </summary>
/// <param name="id">The cosmos item id.</param>
/// <param name="patchStream">A <see cref="Stream"/> containing the patch specification.</param>
/// <param name="requestOptions">(Optional) The options for the item request. <see cref="TransactionalBatchItemRequestOptions"/>.</param>
/// <returns>The <see cref="TransactionalBatch"/> instance with the operation added.</returns>
public virtual TransactionalBatch PatchItemStream(
string id,
Stream patchStream,
TransactionalBatchItemRequestOptions requestOptions = null)
{
this.operations.Add(new ItemBatchOperation(
operationType: OperationType.Patch,
operationIndex: this.operations.Count,
id: id,
resourceStream: patchStream,
requestOptions: requestOptions,
containerCore: this.container));
return this;
}
}
}