Ejemplo n.º 1
0
        async Task ProcessChangesAsync(IReadOnlyCollection <Document> docs, CancellationToken cancellationToken)
        {
            Boolean   isSyntheticKey    = SourcePartitionKeys.Contains(",");
            Boolean   isNestedAttribute = SourcePartitionKeys.Contains("/");
            Container targetContainer   = destinationCollectionClient.GetContainer(config.DestDbName, config.DestCollectionName);

            containerToStoreDocuments = targetContainer;
            Document document;
            BulkOperations <Document> bulkOperations = new BulkOperations <Document>(docs.Count);

            foreach (Document doc in docs)
            {
                //Console.WriteLine($"\tDetected operation...");
                document = (SourcePartitionKeys != null & (!(SourcePartitionKeys.Equals("")))) ? MapPartitionKey(doc, isSyntheticKey, TargetPartitionKey, isNestedAttribute, SourcePartitionKeys) : document = doc;
                bulkOperations.Tasks.Add(containerToStoreDocuments.UpsertItemAsync(item: document, cancellationToken: cancellationToken).CaptureOperationResponse(document));
            }
            BulkOperationResponse <Document> bulkOperationResponse = await bulkOperations.ExecuteAsync();

            if (bulkOperationResponse.Failures.Count > 0 && containerClient != null)
            {
                Console.WriteLine($"\tFailed docs!");
                WriteFailedDocsToBlob("FailedImportDocs", containerClient, bulkOperationResponse);
            }
            LogMetrics(bulkOperationResponse);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Bulk Upsert Task execution
        /// </summary>
        /// <param name="items"></param>
        /// <param name="partitionKey"></param>
        /// <returns></returns>
        public async Task <BulkOperationResponse <T> > UpsertArrayAsync(IList <T> items, string partitionKey = DefaultPartitionKey)
        {
            BulkOperations <T> bulkOperations = new BulkOperations <T>(items.Count);

            if (partitionKey == DefaultPartitionKey)
            {
                foreach (T document in items)
                {
                    bulkOperations.Tasks.Add(container.UpsertItemAsync(document).CaptureOperationResponse(document));
                }
            }
            else
            {
                foreach (T document in items)
                {
                    bulkOperations.Tasks.Add(container.UpsertItemAsync <T>(document, new PartitionKey(partitionKey)).CaptureOperationResponse(document));
                }
            }

            // Execute all tasks
            return(await bulkOperations.ExecuteAsync());
        }
        private async Task ProcessChangesAsync(IReadOnlyCollection <DocumentMetadata> docs, CancellationToken cancellationToken)
        {
            try
            {
                Boolean   isSyntheticKey    = this.SourcePartitionKeys.Contains(",");
                Boolean   isNestedAttribute = this.SourcePartitionKeys.Contains("/");
                Container targetContainer   = this.destinationCollectionClient.GetContainer(this.config.DestDbName, this.config.DestCollectionName);
                this.containerToStoreDocuments = targetContainer;
                DocumentMetadata document;
                BulkOperations <DocumentMetadata> bulkOperations = new BulkOperations <DocumentMetadata>(docs.Count);
                foreach (DocumentMetadata doc in docs)
                {
                    document = (this.SourcePartitionKeys != null & this.TargetPartitionKey != null) ?
                               MapPartitionKey(doc, isSyntheticKey, this.TargetPartitionKey, isNestedAttribute, this.SourcePartitionKeys) :
                               document = doc;
                    if (this.config.OnlyInsertMissingItems)
                    {
                        bulkOperations.Tasks.Add(this.containerToStoreDocuments.CreateItemAsync(
                                                     item: document,
                                                     cancellationToken: cancellationToken).CaptureOperationResponse(document, ignoreConflicts: true));
                    }
                    else
                    {
                        bulkOperations.Tasks.Add(this.containerToStoreDocuments.UpsertItemAsync(
                                                     item: document,
                                                     cancellationToken: cancellationToken).CaptureOperationResponse(document, ignoreConflicts: true));
                    }
                }
                BulkOperationResponse <DocumentMetadata> bulkOperationResponse = await bulkOperations.ExecuteAsync().ConfigureAwait(false);

                if (bulkOperationResponse.Failures.Count > 0 && this.deadletterClient != null)
                {
                    await this.WriteFailedDocsToBlob("FailedImportDocs", this.deadletterClient, bulkOperationResponse)
                    .ConfigureAwait(false);
                }
                TelemetryHelper.Singleton.LogMetrics(bulkOperationResponse);
            }
            catch (Exception error)
            {
                TelemetryHelper.Singleton.LogError(
                    "Processing changes in change feed processor {0} failed: {1}",
                    this.processorName,
                    error);

                throw;
            }
        }