コード例 #1
0
        /// <inheritdoc/>
        public async Task <string> InsertAsync(InfectionReport report, Region region, CancellationToken cancellationToken = default)
        {
            if (report == null)
            {
                throw new ArgumentNullException(nameof(report));
            }
            if (region == null)
            {
                throw new ArgumentNullException(nameof(region));
            }

            region = RegionHelper.AdjustToPrecision(region);

            // Get allowed region boundary
            RegionBoundary boundary = RegionHelper.GetRegionBoundary(region);

            var record = new InfectionReportRecord(report)
            {
                RegionBoundary = new RegionBoundary(boundary),
                PartitionKey   = InfectionReportRecord.GetPartitionKey(region)
            };

            ItemResponse <InfectionReportRecord> response = await this.Container
                                                            .CreateItemAsync <InfectionReportRecord>(
                record,
                new PartitionKey(record.PartitionKey),
                cancellationToken : cancellationToken
                );

            return(response.Resource.Id);
        }
コード例 #2
0
        public async Task InsertAsync(InfectionReport report, IEnumerable <Region> regions, CancellationToken cancellationToken = default)
        {
            // Validate inputs
            if (report == null)
            {
                throw new ArgumentNullException(nameof(report));
            }

            // Prepare records to insert (grouped by partition key)
            var recordGroups = regions.Select(
                r => new InfectionReportRecord(report)
            {
                RegionBoundary = new RegionBoundary(
                    RegionHelper.GetRegionBoundary(r)
                    ),
                PartitionKey = InfectionReportRecord.GetPartitionKey(r)
            }).GroupBy(r => r.PartitionKey);

            // Begin batch operation
            // All MatchMessageRecords will have same PartitionID in this batch
            var batches = recordGroups.Select(g => g.Aggregate(
                                                  this.Container.CreateTransactionalBatch(new PartitionKey(g.Key)),
                                                  (result, item) => result.CreateItem <InfectionReportRecord>(item)));

            // Execute transactions
            // TODO: make a single transaction.
            var responses = await Task.WhenAll(batches.Select(b => b.ExecuteAsync(cancellationToken)));

            var failed = responses.Where(r => !r.IsSuccessStatusCode);

            if (failed.Any())
            {
                throw new Exception(
                          String.Format(
                              "{0} out of {1} insertions failed. Cosmos bulk insert failed with HTTP Status Code {2}.",
                              responses.Count(),
                              failed.Count(),
                              failed.First().StatusCode.ToString()
                              )
                          );
            }
        }