Example #1
0
        private async Task <int> ExecuteBatchedChangesAsync(IReadOnlyList <StateEntry> stateEntries,
                                                            CancellationToken cancellationToken = default(CancellationToken))
        {
            var tableGroups   = stateEntries.GroupBy(s => s.EntityType.AzureTableStorage().Table);
            var allBatchTasks = new List <Task <IList <TableResult> > >();

            IList <TableResult>[] results;

            try
            {
                foreach (var tableGroup in tableGroups)
                {
                    var table           = new AtsTable(tableGroup.Key);
                    var partitionGroups = tableGroup.GroupBy(s =>
                    {
                        var property = s.EntityType.GetPropertyByColumnName("PartitionKey");
                        return(s[property]);
                    }
                                                             );
                    foreach (var partitionGroup in partitionGroups)
                    {
                        var request = new TableBatchRequest(table);
                        foreach (var operation in partitionGroup
                                 .Select(entry => CreateRequest(table, entry))
                                 .Where(operation => operation != null)
                                 )
                        {
                            request.Add(operation);
                            if (request.Count >= MaxBatchOperations)
                            {
                                allBatchTasks.Add(Connection.ExecuteRequestAsync(request, Logger, cancellationToken));
                                request = new TableBatchRequest(table);
                            }
                        }
                        if (request.Count != 0)
                        {
                            allBatchTasks.Add(Connection.ExecuteRequestAsync(request, Logger, cancellationToken));
                        }
                    }
                }

                results = await Task.WhenAll(allBatchTasks).WithCurrentCulture();
            }
            catch (StorageException exception)
            {
                var handled = HandleStorageException(exception, stateEntries);
                if (handled != null)
                {
                    throw handled;
                }
                throw;
            }
            return(results.Sum(r => r == null ? 0 : r.Count(t => t.HttpStatusCode < (int)HttpStatusCode.BadRequest)));
        }
Example #2
0
        private int ExecuteBatchedChanges(IReadOnlyList <StateEntry> stateEntries)
        {
            var tableGroups = stateEntries.GroupBy(s => s.EntityType.AzureTableStorage().Table);
            var results     = new List <IList <TableResult> >();

            try
            {
                foreach (var tableGroup in tableGroups)
                {
                    var table           = new AtsTable(tableGroup.Key);
                    var partitionGroups = tableGroup.GroupBy(s =>
                    {
                        var property = s.EntityType.GetPropertyByColumnName("PartitionKey");
                        return(s[property]);
                    }
                                                             );
                    foreach (var partitionGroup in partitionGroups)
                    {
                        var request = new TableBatchRequest(table);
                        foreach (var operation in partitionGroup
                                 .Select(entry => CreateRequest(table, entry))
                                 .Where(operation => operation != null)
                                 )
                        {
                            request.Add(operation);
                            if (request.Count >= MaxBatchOperations)
                            {
                                results.Add(Connection.ExecuteRequest(request, Logger));
                                request = new TableBatchRequest(table);
                            }
                        }
                        if (request.Count != 0)
                        {
                            results.Add(Connection.ExecuteRequest(request, Logger));
                        }
                    }
                }
            }
            catch (StorageException exception)
            {
                var handled = HandleStorageException(exception, stateEntries);
                if (handled != null)
                {
                    throw handled;
                }
                throw;
            }
            return(results.Sum(r => r == null ? 0 : r.Count(t => t.HttpStatusCode < (int)HttpStatusCode.BadRequest)));
        }