/
TransactionalBatchOperationResult.cs
283 lines (244 loc) · 10.1 KB
/
TransactionalBatchOperationResult.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.IO;
using System.Net;
using Microsoft.Azure.Cosmos.Serialization.HybridRow;
using Microsoft.Azure.Cosmos.Serialization.HybridRow.IO;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;
/// <summary>
/// Represents a result for a specific operation that was part of a <see cref="TransactionalBatch"/> request.
/// </summary>
public class TransactionalBatchOperationResult
{
internal TransactionalBatchOperationResult(
HttpStatusCode statusCode)
{
this.StatusCode = statusCode;
}
internal TransactionalBatchOperationResult(TransactionalBatchOperationResult other)
{
this.StatusCode = other.StatusCode;
this.SubStatusCode = other.SubStatusCode;
this.ETag = other.ETag;
this.ResourceStream = other.ResourceStream;
this.RequestCharge = other.RequestCharge;
this.RetryAfter = other.RetryAfter;
this.SessionToken = other.SessionToken;
this.ActivityId = other.ActivityId;
}
/// <summary>
/// Initializes a new instance of the <see cref="TransactionalBatchOperationResult"/> class.
/// </summary>
protected TransactionalBatchOperationResult()
{
}
/// <summary>
/// Gets the completion status of the operation.
/// </summary>
public virtual HttpStatusCode StatusCode { get; private set; }
/// <summary>
/// Gets a value indicating whether the current operation completed successfully.
/// </summary>
public virtual bool IsSuccessStatusCode
{
get
{
int statusCodeInt = (int)this.StatusCode;
return statusCodeInt >= 200 && statusCodeInt <= 299;
}
}
/// <summary>
/// Gets the entity tag associated with the resource.
/// </summary>
/// <value>
/// The entity tag associated with the resource.
/// </value>
/// <remarks>
/// ETags are used for concurrency checking when updating resources.
/// </remarks>
public virtual string ETag { get; internal set; }
/// <summary>
/// Gets the content of the resource.
/// </summary>
/// <value>
/// The content of the resource as a Stream.
/// </value>
public virtual Stream ResourceStream { get; internal set; }
/// <summary>
/// In case the operation is rate limited, indicates the time post which a retry can be attempted.
/// </summary>
public virtual TimeSpan RetryAfter { get; internal set; }
/// <summary>
/// Request charge in request units for the operation.
/// </summary>
internal virtual double RequestCharge { get; set; }
/// <summary>
/// Gets detail on the completion status of the operation.
/// </summary>
internal virtual SubStatusCodes SubStatusCode { get; set; }
/// <summary>
/// Gets the SessionToken assigned to this result, if any.
/// </summary>
internal virtual string SessionToken { get; set; }
/// <summary>
/// A string containing the partition key range id assigned to this result.
/// </summary>
internal virtual string PartitionKeyRangeId { get; set; }
/// <summary>
/// ActivityId related to the operation
/// </summary>
internal virtual string ActivityId { get; set; }
internal ITrace Trace { get; set; }
internal static Result ReadOperationResult(ReadOnlyMemory<byte> input, out TransactionalBatchOperationResult batchOperationResult)
{
RowBuffer row = new RowBuffer(input.Length);
if (!row.ReadFrom(
input.Span,
HybridRowVersion.V1,
BatchSchemaProvider.BatchLayoutResolver))
{
batchOperationResult = null;
return Result.Failure;
}
RowReader reader = new RowReader(ref row);
Result result = TransactionalBatchOperationResult.ReadOperationResult(ref reader, out batchOperationResult);
if (result != Result.Success)
{
return result;
}
// Ensure the mandatory fields were populated
if (batchOperationResult.StatusCode == default)
{
return Result.Failure;
}
return Result.Success;
}
private static Result ReadOperationResult(ref RowReader reader, out TransactionalBatchOperationResult batchOperationResult)
{
batchOperationResult = new TransactionalBatchOperationResult();
while (reader.Read())
{
Result r;
switch (reader.Path)
{
case "statusCode":
r = reader.ReadInt32(out int statusCode);
if (r != Result.Success)
{
return r;
}
batchOperationResult.StatusCode = (HttpStatusCode)statusCode;
break;
case "subStatusCode":
r = reader.ReadInt32(out int subStatusCode);
if (r != Result.Success)
{
return r;
}
batchOperationResult.SubStatusCode = (SubStatusCodes)subStatusCode;
break;
case "eTag":
r = reader.ReadString(out string eTag);
if (r != Result.Success)
{
return r;
}
batchOperationResult.ETag = eTag;
break;
case "resourceBody":
r = reader.ReadBinary(out byte[] resourceBody);
if (r != Result.Success)
{
return r;
}
batchOperationResult.ResourceStream = new MemoryStream(
buffer: resourceBody, index: 0, count: resourceBody.Length, writable: false, publiclyVisible: true);
break;
case "requestCharge":
r = reader.ReadFloat64(out double requestCharge);
if (r != Result.Success)
{
return r;
}
// Round request charge to 2 decimals on the operation results
// similar to how we round them for the full response.
batchOperationResult.RequestCharge = Math.Round(requestCharge, 2);
break;
case "retryAfterMilliseconds":
r = reader.ReadUInt32(out uint retryAfterMilliseconds);
if (r != Result.Success)
{
return r;
}
batchOperationResult.RetryAfter = TimeSpan.FromMilliseconds(retryAfterMilliseconds);
break;
}
}
return Result.Success;
}
internal ResponseMessage ToResponseMessage(ContainerInternal cosmosContainerCore = null)
{
Headers headers = new Headers()
{
SubStatusCode = this.SubStatusCode,
ETag = this.ETag,
RetryAfter = this.RetryAfter,
RequestCharge = this.RequestCharge,
Session = this.SessionToken,
PartitionKeyRangeId = this.PartitionKeyRangeId,
ActivityId = this.ActivityId,
};
// Need this information in Open telemetry hence adding this information in the request
RequestMessage requestMessage = new ()
{
ContainerId = cosmosContainerCore?.Id,
DatabaseId = cosmosContainerCore?.Database?.Id,
Trace = null
};
ResponseMessage responseMessage = new ResponseMessage(
statusCode: this.StatusCode,
requestMessage: requestMessage,
headers: headers,
cosmosException: null,
trace: this.Trace ?? NoOpTrace.Singleton)
{
Content = this.ResourceStream
};
return responseMessage;
}
}
/// <summary>
/// Represents a result for a specific operation that is part of a batch.
/// </summary>
/// <typeparam name="T">The type of the Resource which this class wraps.</typeparam>
#pragma warning disable SA1402 // File may only contain a single type
public class TransactionalBatchOperationResult<T> : TransactionalBatchOperationResult
#pragma warning restore SA1402 // File may only contain a single type
{
/// <summary>
/// Initializes a new instance of the <see cref="TransactionalBatchOperationResult{T}"/> class.
/// </summary>
/// <param name="result">BatchOperationResult with stream resource.</param>
/// <param name="resource">Deserialized resource.</param>
internal TransactionalBatchOperationResult(TransactionalBatchOperationResult result, T resource)
: base(result)
{
this.Resource = resource;
}
/// <summary>
/// Initializes a new instance of the <see cref="TransactionalBatchOperationResult{T}"/> class.
/// </summary>
protected TransactionalBatchOperationResult()
{
}
/// <summary>
/// Gets the content of the resource.
/// </summary>
public virtual T Resource { get; set; }
}
}