private async Task CallUntilCompletionAsync(BatchWriteItemRequest request, Dictionary <string, Dictionary <Key, Document> > documentMap, IAmazonDynamoDB client, CancellationToken cancellationToken)
#endif
{
do
{
var result = await client.BatchWriteItemAsync(request, cancellationToken).ConfigureAwait(false);
request.RequestItems = result.UnprocessedItems;
Dictionary <Key, Document> unprocessedDocuments = new Dictionary <Key, Document>(keyComparer);
foreach (var unprocessedItems in result.UnprocessedItems)
{
string tableName = unprocessedItems.Key;
Table table = tableMap[tableName];
Dictionary <Key, Document> tableDocumentMap = documentMap[tableName];
foreach (var writeRequest in unprocessedItems.Value)
{
if (writeRequest.PutRequest != null)
{
var doc = table.FromAttributeMap(writeRequest.PutRequest.Item);
var key = table.MakeKey(doc);
Document document = null;
if (tableDocumentMap.TryGetValue(key, out document))
{
// Remove unprocessed requests from the document map
// and copy them to unprocessed documents.
unprocessedDocuments.Add(key, document);
tableDocumentMap.Remove(key);
}
}
}
// Commit the remaining documents in the document map
foreach (var document in tableDocumentMap.Values)
{
document.CommitChanges();
}
// Replace existing documents with just the unprocessed documents
documentMap[tableName] = unprocessedDocuments;
}
} while (request.RequestItems.Count > 0);
// Commit any remaining documents in document map.
// This would only happen if we are not able to match the items sent in the request
// with the items returned back as unprocessed items.
foreach (var tableDocumentMap in documentMap.Values)
{
foreach (var document in tableDocumentMap.Values)
{
document.CommitChanges();
}
}
}