private async Task UpdateBatchAsync(ResourceAccessRuleSetCollection updatedRuleSets, IList <ClaimPermissions> results, IList <ClaimPermissions> batch) { var tasks = new List <Task <ClaimPermissions> >(); foreach (ClaimPermissions permissions in batch) { Dictionary <string, int> permissionsDictionary = BuildDictionaryOfIdsToIndices(permissions); bool hasUpdates = false; updatedRuleSets.RuleSets.ForEach(newRuleSet => { if (permissionsDictionary.TryGetValue(newRuleSet.Id, out int index)) { permissions.ResourceAccessRuleSets.RemoveAt(index); permissions.ResourceAccessRuleSets.Insert(index, newRuleSet); hasUpdates = true; } }); if (hasUpdates) { tasks.Add(this.UpdateAsync(permissions)); } else { results.Add(permissions); } } if (tasks.Count > 0) { results.AddRange(await Task.WhenAll(tasks).ConfigureAwait(false)); } }
private async Task <IEnumerable <ClaimPermissions> > UpdateRuleSetsAsync(IEnumerable <ClaimPermissions> permissionsSets, int maxParallelism) { ResourceAccessRuleSetCollection updatedRuleSets = await this.resourceAccessRuleSetStore.GetBatchAsync( permissionsSets .SelectMany(p => p.ResourceAccessRuleSets.Select(r => new IdWithETag(r.Id, r.ETag))) .Distinct()).ConfigureAwait(false); if (updatedRuleSets.RuleSets.Count > 0) { IList <ClaimPermissions> results = new List <ClaimPermissions>(); foreach (IList <ClaimPermissions> batch in permissionsSets.Buffer(maxParallelism)) { await this.UpdateBatchAsync(updatedRuleSets, results, batch).ConfigureAwait(false); } return(results); } return(permissionsSets); }
/// <inheritdoc/> public async Task <ResourceAccessRuleSetCollection> GetBatchAsync(IEnumerable <IdWithETag> ids, int maxParallelism = 5) { if (ids is null) { throw new ArgumentNullException(nameof(ids)); } var result = new ResourceAccessRuleSetCollection(); foreach (IList <IdWithETag> batch in ids.Buffer(maxParallelism)) { IList <Task <ResourceAccessRuleSet> > taskBatch = batch.Select(id => Task.Run(async() => { return(await this.DownloadBlobAsync(id.Id, id.ETag).ConfigureAwait(false)); })).ToList(); ResourceAccessRuleSet[] ruleSets = await Task.WhenAll(taskBatch).ConfigureAwait(false); result.RuleSets.AddRange(ruleSets.Where(r => r != null)); } return(result); }