Esempio n. 1
0
        public async Task <BatchResult> PutAsync(IEnumerable <T> entities)
        {
            #region Preconditions

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

            #endregion

            var sw = Stopwatch.StartNew();

            var result = new BatchResult();

            // Batch in groups of 25
            foreach (var batch in entities.Batch(25))
            {
                await PutBatch(batch, result).ConfigureAwait(false);
            }

            result.ResponseTime = sw.Elapsed;

            return(result);
        }
Esempio 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);
        }
Esempio n. 3
0
        public async Task <BatchResult> PutAsync(IEnumerable <T> entities)
        {
            var sw = Stopwatch.StartNew();

            var result = new BatchResult();

            // Batch in groups of 25
            foreach (IEnumerable <T> batch in entities.Batch(25))
            {
                await PutBatch(batch, result).ConfigureAwait(false);
            }

            result.ResponseTime = sw.Elapsed;

            return(result);
        }
Esempio 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;
        }