Exemplo n.º 1
0
        public static BatchWriteItemResult FromJsonElement(JsonElement json)
        {
            var unprocessed = new List <TableRequests>();

            /*
             * if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode)) // Array
             * {
             *  foreach (var item in (JsonArray)consumedCapacityNode)
             *  {
             *      var unit = item.As<ConsumedCapacity>();
             *
             *      // TODO
             *  }
             * }
             */

            if (json.TryGetProperty("UnprocessedItems", out var unprocessedItemsEl))
            {
                foreach (JsonProperty batch in unprocessedItemsEl.EnumerateObject())
                {
                    unprocessed.Add(TableRequests.FromElementJson(batch.Name, batch.Value));
                }
            }

            return(new BatchWriteItemResult {
                UnprocessedItems = unprocessed
            });
        }
Exemplo n.º 2
0
        private async Task <BatchResult> PutBatch(IEnumerable <T> entities, BatchResult result)
        {
            #region Preconditions

            if (entities == null)
            {
                throw new ArgumentNullException(nameof(entities));
            }

            #endregion

            // Up to 25 items put or delete operations, with the request size not exceeding 1 MB.

            var putRequests = entities
                              .Select(e => (ItemRequest) new PutRequest(AttributeCollection.FromObject(e, metadata)))
                              .ToList();

            var tableBatch = new TableRequests(tableName, putRequests);

            await BatchWriteItem(tableBatch, result).ConfigureAwait(false);

            result.BatchCount++;

            return(result);
        }
Exemplo n.º 3
0
        public static BatchWriteItemResult FromJson(JsonObject json)
        {
            var unprocessed = new List <TableRequests>();

            if (json.TryGetValue("ConsumedCapacity", out var consumedCapacityNode)) // Array
            {
                foreach (var item in (JsonArray)consumedCapacityNode)
                {
                    var unit = DynamoDb.ConsumedCapacity.FromJson((JsonObject)item);

                    // TODO
                }
            }

            if (json.TryGetValue("UnprocessedItems", out var unprocessedItemsNode))
            {
                foreach (var batch in (JsonObject)unprocessedItemsNode)
                {
                    unprocessed.Add(TableRequests.FromJson(batch.Key, (JsonArray)batch.Value));
                }
            }

            return(new BatchWriteItemResult {
                UnprocessedItems = unprocessed
            });
        }
Exemplo n.º 4
0
        private async Task <BatchWriteItemResult> BatchWriteItem(TableRequests batch, BatchResult info)
        {
            #region Preconditions

            if (batch == null)
            {
                throw new ArgumentNullException(nameof(batch));
            }

            if (batch.Requests.Count > 25)
            {
                throw new ArgumentException("Must be 25 or less.", "batch.Items");
            }

            #endregion

            var       retryCount = 0;
            Exception lastError  = null;

            while (retryPolicy.ShouldRetry(retryCount))
            {
                try
                {
                    var result = await client.BatchWriteItem(batch).ConfigureAwait(false);

                    info.ItemCount += batch.Requests.Count;
                    info.RequestCount++;

                    // Recursively process any item
                    foreach (var unprocessedBatch in result.UnprocessedItems)
                    {
                        await Task.Delay(100).ConfigureAwait(false); // Slow down

                        info.ItemCount -= unprocessedBatch.Requests.Count;

                        result = await BatchWriteItem(unprocessedBatch, info).ConfigureAwait(false);
                    }

                    return(result);
                }
                catch (DynamoDbException ex) when(ex.IsTransient)
                {
                    lastError = ex;
                }

                retryCount++;

                await Task.Delay(retryPolicy.GetDelay(retryCount)).ConfigureAwait(false);
            }

            throw lastError;
        }