public async Task ItemBatchNoResponseTest()
        {
            TransactionalBatchItemRequestOptions requestOptions = new TransactionalBatchItemRequestOptions()
            {
                EnableContentResponseOnWrite = false
            };

            string             pkId  = "TestBatchId";
            TransactionalBatch batch = this.container.CreateTransactionalBatch(new PartitionKey(pkId));

            int noResponseItemCount = 100;

            for (int i = 0; i < noResponseItemCount; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId);
                batch.CreateItem <ToDoActivity>(item, requestOptions: requestOptions);
            }

            TransactionalBatchResponse response = await batch.ExecuteAsync();

            Assert.AreEqual(100, response.Count);
            this.ValidateResponse(response, noResponseItemCount);

            pkId  = "TestBatchId2";
            batch = this.container.CreateTransactionalBatch(new PartitionKey(pkId));

            noResponseItemCount = 0;
            for (int i = 0; i < 10; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId);
                batch.CreateItem <ToDoActivity>(item, requestOptions: requestOptions);
                noResponseItemCount++;
                ToDoActivity item2 = ToDoActivity.CreateRandomToDoActivity(pk: pkId);
                item2.id = item.id;
                batch.ReplaceItem <ToDoActivity>(item2.id, item2, requestOptions);
                noResponseItemCount++;
            }

            int withBodyCount = 0;

            for (int i = 0; i < 5; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId);
                batch.CreateItem <ToDoActivity>(item);
                withBodyCount++;
                batch.ReadItem(item.id);
                withBodyCount++;
            }

            response = await batch.ExecuteAsync();

            Assert.AreEqual(noResponseItemCount + withBodyCount, response.Count);
            this.ValidateResponse(response, noResponseItemCount);
        }
Ejemplo n.º 2
0
        public async Task NoContentResponseTransactionBatchOverrideTest()
        {
            string             pkId  = "TestBatchId";
            TransactionalBatch batch = this.containerWithFlag.CreateTransactionalBatch(new PartitionKey(pkId));
            TransactionalBatchItemRequestOptions requestOptions = new TransactionalBatchItemRequestOptions()
            {
                EnableContentResponseOnWrite = false
            };

            int noResponseItemCount = 100;

            for (int i = 0; i < noResponseItemCount; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkId);
                batch.CreateItem <ToDoActivity>(item, requestOptions: requestOptions);
            }

            TransactionalBatchResponse response = await batch.ExecuteAsync();

            Assert.AreEqual(response.Count, 100);
            foreach (TransactionalBatchOperationResult itemResponse in response)
            {
                Assert.IsTrue(itemResponse.StatusCode == HttpStatusCode.Created);
                Assert.IsNull(itemResponse.ResourceStream);
            }
        }
        public async Task BatchOperationDiagnostic(bool disableDiagnostics)
        {
            string                pkValue   = "DiagnosticTestPk";
            TransactionalBatch    batch     = this.Container.CreateTransactionalBatch(new PartitionKey(pkValue));
            BatchCore             batchCore = (BatchCore)batch;
            List <PatchOperation> patch     = new List <PatchOperation>()
            {
                PatchOperation.Remove("/cost")
            };

            List <ToDoActivity> createItems = new List <ToDoActivity>();

            for (int i = 0; i < 50; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkValue);
                createItems.Add(item);
                batch.CreateItem <ToDoActivity>(item);
            }

            for (int i = 0; i < 20; i++)
            {
                batch.ReadItem(createItems[i].id);
                batchCore.PatchItem(createItems[i].id, patch);
            }

            TransactionalBatchRequestOptions requestOptions = disableDiagnostics ? RequestOptionDisableDiagnostic : null;
            TransactionalBatchResponse       response       = await batch.ExecuteAsync(requestOptions);

            Assert.IsNotNull(response);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(
                diagnostics: response.Diagnostics,
                disableDiagnostics: disableDiagnostics);
        }
Ejemplo n.º 4
0
        public async Task ExecuteAsync(Container collection)
        {
            EnsureArg.IsNotNull(collection, nameof(collection));

            // Detect if registry has been initialized
            var partitionKey = new PartitionKey(SearchParameterStatusWrapper.SearchParameterStatusPartitionKey);
            var query        = _queryFactory.Create <dynamic>(
                collection,
                new CosmosQueryContext(
                    new QueryDefinition($"SELECT TOP 1 * FROM c where c.{KnownDocumentProperties.PartitionKey} = '{SearchParameterStatusWrapper.SearchParameterStatusPartitionKey}'"),
                    new QueryRequestOptions {
                PartitionKey = partitionKey
            }));

            var results = await query.ExecuteNextAsync();

            if (!results.Any())
            {
                var statuses = await _filebasedRegistry.GetSearchParameterStatuses();

                foreach (var batch in statuses.TakeBatch(100))
                {
                    TransactionalBatch transaction = collection.CreateTransactionalBatch(partitionKey);

                    foreach (SearchParameterStatusWrapper status in batch.Select(x => x.ToSearchParameterStatusWrapper()))
                    {
                        transaction.CreateItem(status);
                    }

                    await transaction.ExecuteAsync();
                }
            }
        }
Ejemplo n.º 5
0
        private TransactionalBatch InsertAllDocuments(TransactionalBatch batch)
        {
            TransactionalBatch result = batch;

            foreach (var shop in CreateShopList())
            {
                result.CreateItem <Shop>(shop);
                Console.WriteLine($"Added shop to batch request: {shop.Name}");
            }
            return(result);
        }
        public async Task ExecuteAsync(Container container, CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(container, nameof(container));

            // Detect if registry has been initialized
            var partitionKey = new PartitionKey(SearchParameterStatusWrapper.SearchParameterStatusPartitionKey);
            var query        = _queryFactory.Create <dynamic>(
                container,
                new CosmosQueryContext(
                    new QueryDefinition($"SELECT TOP 1 * FROM c where c.{KnownDocumentProperties.PartitionKey} = '{SearchParameterStatusWrapper.SearchParameterStatusPartitionKey}'"),
                    new QueryRequestOptions {
                PartitionKey = partitionKey
            }));

            var results = await query.ExecuteNextAsync(cancellationToken);

            if (!results.Any())
            {
                var statuses = await _filebasedSearchParameterStatusDataStore.GetSearchParameterStatuses(cancellationToken);

                foreach (var status in statuses.Where(x => _configuration.InitialSortParameterUris.Contains(x.Uri.OriginalString)))
                {
                    status.SortStatus = SortParameterStatus.Enabled;
                }

                foreach (var batch in statuses.TakeBatch(100))
                {
                    TransactionalBatch transaction = container.CreateTransactionalBatch(partitionKey);

                    foreach (SearchParameterStatusWrapper status in batch.Select(x => x.ToSearchParameterStatusWrapper()))
                    {
                        transaction.CreateItem(status);
                    }

                    await transaction.ExecuteAsync(cancellationToken);
                }
            }
        }
        public async Task BatchOperationDiagnostic()
        {
            string             pkValue = "DiagnosticTestPk";
            TransactionalBatch batch   = this.Container.CreateTransactionalBatch(new PartitionKey(pkValue));

            List <ToDoActivity> createItems = new List <ToDoActivity>();

            for (int i = 0; i < 50; i++)
            {
                ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(pk: pkValue);
                createItems.Add(item);
                batch.CreateItem <ToDoActivity>(item);
            }

            for (int i = 0; i < 20; i++)
            {
                batch.ReadItem(createItems[i].id);
            }

            TransactionalBatchResponse response = await batch.ExecuteAsync();

            Assert.IsNotNull(response);
            CosmosDiagnosticsTests.VerifyPointDiagnostics(response.Diagnostics);
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        public async ValueTask CreateAsBatchAsync(
            IEnumerable <TItem> items,
            CancellationToken cancellationToken = default)
        {
            List <TItem> list = items.ToList();

            string partitionKey = GetPartitionKeyValue(list);

            Container container = await _containerProvider.GetContainerAsync();

            TransactionalBatch batch = container.CreateTransactionalBatch(new PartitionKey(partitionKey));

            foreach (TItem item in list)
            {
                batch.CreateItem(item);
            }

            using TransactionalBatchResponse response = await batch.ExecuteAsync(cancellationToken);

            if (!response.IsSuccessStatusCode)
            {
                throw new BatchOperationException <TItem>(response);
            }
        }
Ejemplo n.º 9
0
        public void Create <T>(T document) where T : IAggregate
        {
            var cosmosDocument = new CosmosDocument <T>(this._partitionKey, document);

            _batch.CreateItem(cosmosDocument);
        }
Ejemplo n.º 10
0
 public override void Apply(TransactionalBatch transactionalBatch, PartitionKeyPath partitionKeyPath)
 {
     transactionalBatch.CreateItem(item, options);
 }
 public void CreateItem <T>(T item, TransactionalBatchItemRequestOptions options = null)
 {
     Batch.CreateItem(item, options);
 }