-
Notifications
You must be signed in to change notification settings - Fork 889
Expand file tree
/
Copy pathBatchWrite.cs
More file actions
280 lines (244 loc) · 9.68 KB
/
Copy pathBatchWrite.cs
File metadata and controls
280 lines (244 loc) · 9.68 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
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using Amazon.DynamoDBv2.DocumentModel;
using System.Globalization;
#if AWS_ASYNC_API
using System.Threading.Tasks;
#endif
using System.Threading;
namespace Amazon.DynamoDBv2.DataModel
{
/// <summary>
/// Represents a non-generic interface for writing/deleting a batch of items
/// in a single DynamoDB table
/// </summary>
public partial interface IBatchWrite
{
}
/// <summary>
/// Represents a generic interface for writing/deleting a batch of items
/// in a single DynamoDB table
/// </summary>
public interface IBatchWrite<T> : IBatchWrite
{
/// <summary>
/// Creates a MultiTableBatchWrite object that is a combination
/// of the current BatchWrite and the specified BatchWrites
/// </summary>
/// <param name="otherBatches">Other BatchWrite objects</param>
/// <returns>
/// MultiTableBatchWrite consisting of the multiple BatchWrite objects:
/// the current batch and the passed-in batches.
/// </returns>
IMultiTableBatchWrite Combine(params IBatchWrite[] otherBatches);
/// <summary>
/// Add a number of items to be put in the current batch operation
/// </summary>
/// <param name="values">Items to put</param>
void AddPutItems(IEnumerable<T> values);
/// <summary>
/// Add a single item to be put in the current batch operation
/// </summary>
/// <param name="item"></param>
void AddPutItem(T item);
/// <summary>
/// Add a number of items to be deleted in the current batch operation
/// </summary>
/// <param name="values">Items to be deleted</param>
void AddDeleteItems(IEnumerable<T> values);
/// <summary>
/// Add a single item to be deleted in the current batch operation.
/// </summary>
/// <param name="item">Item to be deleted</param>
void AddDeleteItem(T item);
/// <summary>
/// Add a single item to be deleted in the current batch operation.
/// Item is identified by its hash primary key.
/// </summary>
/// <param name="hashKey">Hash key of the item to delete</param>
void AddDeleteKey(object hashKey);
/// <summary>
/// Add a single item to be deleted in the current batch operation.
/// Item is identified by its hash-and-range primary key.
/// </summary>
/// <param name="hashKey">Hash key of the item to delete</param>
/// <param name="rangeKey">Range key of the item to delete</param>
void AddDeleteKey(object hashKey, object rangeKey);
}
/// <summary>
/// Represents a non-generic object for writing/deleting a batch of items
/// in a single DynamoDB table
/// </summary>
public abstract partial class BatchWrite : IBatchWrite
{
internal DocumentBatchWrite DocumentBatch { get; set; }
}
/// <summary>
/// Represents a strongly-typed object for writing/deleting a batch of items
/// in a single DynamoDB table
/// </summary>
public partial class BatchWrite<T> : BatchWrite, IBatchWrite<T>
{
private readonly DynamoDBContext _context;
private readonly DynamoDBFlatConfig _config;
private readonly ItemStorageConfig _storageConfig;
internal BatchWrite(DynamoDBContext context, DynamoDBFlatConfig config)
: this(context, typeof(T), config)
{
}
internal BatchWrite(DynamoDBContext context, Type valuesType, DynamoDBFlatConfig config)
{
_context = context;
_config = config;
_storageConfig = context.StorageConfigCache.GetConfig(valuesType, config);
if (_storageConfig.HasVersion)
{
if (!_config.SkipVersionCheck.GetValueOrDefault(false))
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
"Object {0} has a versioning field, which is not supported for this operation. To ignore versioning, use the DynamoDBContextConfig.SkipVersionCheck property.",
valuesType.Name));
}
Table table = _context.GetTargetTable(_storageConfig, _config);
// Table.CreateBatchWrite() returns the IDocumentBatchWrite interface.
// But since we rely on the internal behavior of DocumentBatchWrite, we instantiate it via the constructor.
DocumentBatch = new DocumentBatchWrite(table);
}
/// <inheritdoc/>
public IMultiTableBatchWrite Combine(params IBatchWrite[] otherBatches)
{
return new MultiTableBatchWrite(this, otherBatches);
}
/// <inheritdoc/>
public void AddPutItems(IEnumerable<T> values)
{
if (values == null) return;
foreach (T item in values)
{
AddPutItem(item);
}
}
/// <inheritdoc/>
public void AddPutItem(T item)
{
if (item == null) return;
ItemStorage storage = _context.ObjectToItemStorageHelper(item, _storageConfig, _config, keysOnly: false, ignoreNullValues: true);
if (storage == null) return;
DocumentBatch.AddDocumentToPut(storage.Document);
}
/// <inheritdoc/>
public void AddDeleteItems(IEnumerable<T> values)
{
if (values == null) return;
foreach (T item in values)
{
AddDeleteItem(item);
}
}
/// <inheritdoc/>
public void AddDeleteItem(T item)
{
if (item == null) return;
ItemStorage storage = _context.ObjectToItemStorageHelper(item, _storageConfig, _config, keysOnly: true, ignoreNullValues: true);
if (storage == null) return;
DocumentBatch.AddItemToDelete(storage.Document);
}
/// <inheritdoc/>
public void AddDeleteKey(object hashKey)
{
AddDeleteKey(hashKey, null);
}
/// <inheritdoc/>
public void AddDeleteKey(object hashKey, object rangeKey)
{
DocumentBatch.AddKeyToDelete(_context.MakeKey(hashKey, rangeKey, _storageConfig, _config));
}
private void ExecuteHelper()
{
DocumentBatch.ExecuteHelper();
}
#if AWS_ASYNC_API
private Task ExecuteHelperAsync(CancellationToken cancellationToken)
{
return DocumentBatch.ExecuteHelperAsync(cancellationToken);
}
#endif
}
/// <summary>
/// Interface for writing/deleting a batch of items in multiple DynamoDB tables,
/// using multiple strongly-typed BatchWrite objects
/// </summary>
public partial interface IMultiTableBatchWrite
{
/// <summary>
/// Add a BatchWrite object to the multi-table batch request
/// </summary>
/// <param name="batch">BatchWrite to add</param>
void AddBatch(IBatchWrite batch);
}
/// <summary>
/// Class for writing/deleting a batch of items in multiple DynamoDB tables,
/// using multiple strongly-typed BatchWrite objects
/// </summary>
public partial class MultiTableBatchWrite : IMultiTableBatchWrite
{
private List<IBatchWrite> allBatches = new();
/// <summary>
/// Constructs a MultiTableBatchWrite object from a number of
/// BatchWrite objects
/// </summary>
/// <param name="batches">Collection of BatchWrite objects</param>
public MultiTableBatchWrite(params IBatchWrite[] batches)
{
allBatches = new List<IBatchWrite>(batches);
}
internal MultiTableBatchWrite(IBatchWrite first, params IBatchWrite[] rest)
{
allBatches = new List<IBatchWrite>();
allBatches.Add(first);
allBatches.AddRange(rest);
}
/// <inheritdoc/>
public void AddBatch(IBatchWrite batch)
{
allBatches.Add(batch);
}
private void ExecuteHelper()
{
MultiTableDocumentBatchWrite superBatch = new MultiTableDocumentBatchWrite();
var errorMsg = $"All batches must be of type {nameof(BatchWrite)}";
foreach (var batch in allBatches)
{
var abstractBatch = batch as BatchWrite ?? throw new InvalidOperationException(errorMsg);
superBatch.AddBatch(abstractBatch.DocumentBatch);
}
superBatch.ExecuteHelper();
}
#if AWS_ASYNC_API
private Task ExecuteHelperAsync(CancellationToken cancellationToken)
{
MultiTableDocumentBatchWrite superBatch = new MultiTableDocumentBatchWrite();
var errorMsg = $"All batches must be of type {nameof(BatchWrite)}";
foreach (var batch in allBatches)
{
var abstractBatch = batch as BatchWrite ?? throw new InvalidOperationException(errorMsg);
superBatch.AddBatch(abstractBatch.DocumentBatch);
}
return superBatch.ExecuteHelperAsync(cancellationToken);
}
#endif
}
}