private async Task ItemOperations(CosmosContainer container, bool containerNotExist)
        {
            if (containerNotExist)
            {
                dynamic randomItem = new { id = "test", pk = "doesnotexist" };
                Stream  create     = jsonSerializer.ToStream <dynamic>(randomItem);
                this.VerifyNotFoundResponse(await container.CreateItemStreamAsync(new PartitionKey(randomItem.pk), create));

                var queryIterator = container.CreateItemQueryStream("select * from t where true", maxConcurrency: 2);
                this.VerifyQueryNotFoundResponse(await queryIterator.FetchNextSetAsync());

                var feedIterator = container.GetItemsStreamIterator();
                this.VerifyNotFoundResponse(await feedIterator.FetchNextSetAsync());

                dynamic randomUpsertItem = new { id = DoesNotExist, pk = DoesNotExist, status = 42 };
                Stream  upsert           = jsonSerializer.ToStream <dynamic>(randomUpsertItem);
                this.VerifyNotFoundResponse(await container.UpsertItemStreamAsync(
                                                partitionKey: new Cosmos.PartitionKey(randomUpsertItem.pk),
                                                streamPayload: upsert));
            }

            this.VerifyNotFoundResponse(await container.ReadItemStreamAsync(partitionKey: new Cosmos.PartitionKey(DoesNotExist), id: DoesNotExist));
            this.VerifyNotFoundResponse(await container.DeleteItemStreamAsync(partitionKey: new Cosmos.PartitionKey(DoesNotExist), id: DoesNotExist));

            dynamic randomReplaceItem = new { id = "test", pk = "doesnotexist", status = 42 };
            Stream  replace           = jsonSerializer.ToStream <dynamic>(randomReplaceItem);

            this.VerifyNotFoundResponse(await container.ReplaceItemStreamAsync(
                                            partitionKey: new Cosmos.PartitionKey(randomReplaceItem.pk),
                                            id: randomReplaceItem.id,
                                            streamPayload: replace));
        }
Esempio n. 2
0
        private static async Task <SalesOrder> CreateItemsAsync()
        {
            Console.WriteLine("\n1.1 - Creating items");

            // Create a SalesOrder object. This object has nested properties and various types including numbers, DateTimes and strings.
            // This can be saved as JSON as is without converting into rows/columns.
            SalesOrder salesOrder = GetSalesOrderSample("SalesOrder1");
            ItemResponse <SalesOrder> response = await container.CreateItemAsync(salesOrder, new PartitionKey(salesOrder.AccountNumber));

            SalesOrder salesOrder1 = response;

            Console.WriteLine($"\n1.1.1 - Item created {salesOrder1.Id}");

            // As your app evolves, let's say your object has a new schema. You can insert SalesOrderV2 objects without any
            // changes to the database tier.
            SalesOrder2 newSalesOrder            = GetSalesOrderV2Sample("SalesOrder2");
            ItemResponse <SalesOrder2> response2 = await container.CreateItemAsync(newSalesOrder, new PartitionKey(newSalesOrder.AccountNumber));

            SalesOrder2 salesOrder2 = response2;

            Console.WriteLine($"\n1.1.2 - Item created {salesOrder2.Id}");

            // For better performance create a SalesOrder object from a stream.
            SalesOrder salesOrderV3 = GetSalesOrderSample("SalesOrderV3");

            using (Stream stream = Program.ToStream <SalesOrder>(salesOrderV3))
            {
                using (CosmosResponseMessage responseMessage = await container.CreateItemStreamAsync(new PartitionKey(salesOrderV3.AccountNumber), stream))
                {
                    // Item stream operations do not throw exceptions for better performance
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        SalesOrder streamResponse = FromStream <SalesOrder>(responseMessage.Content);
                        Console.WriteLine($"\n1.1.2 - Item created {streamResponse.Id}");
                    }
                    else
                    {
                        Console.WriteLine($"Create item from stream failed. Status code: {responseMessage.StatusCode} Message: {responseMessage.ErrorMessage}");
                    }
                }
            }

            return(salesOrder);
        }
        private async Task VerifyItemOperations(
            Cosmos.PartitionKey partitionKey,
            string partitionKeySerialized,
            dynamic testItem,
            ItemRequestOptions requestOptions = null)
        {
            CosmosResponseMessage response       = null;
            HttpStatusCode        httpStatusCode = HttpStatusCode.OK;
            int         testHandlerHitCount      = 0;
            TestHandler testHandler = new TestHandler((request, cancellationToken) =>
            {
                Assert.IsTrue(request.RequestUri.OriginalString.StartsWith(@"dbs/testdb/colls/testcontainer"));
                Assert.AreEqual(requestOptions, request.RequestOptions);
                Assert.AreEqual(ResourceType.Document, request.ResourceType);
                Assert.IsNotNull(request.Headers.PartitionKey);
                Assert.AreEqual(partitionKeySerialized, request.Headers.PartitionKey);
                testHandlerHitCount++;
                response         = new CosmosResponseMessage(httpStatusCode, request, errorMessage: null);
                response.Content = request.Content;
                return(Task.FromResult(response));
            });

            CosmosClient client = MockCosmosUtil.CreateMockCosmosClient(
                (builder) => builder.AddCustomHandlers(testHandler));

            CosmosContainer container = client.GetDatabase("testdb")
                                        .GetContainer("testcontainer");

            ItemResponse <dynamic> itemResponse = await container.CreateItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.ReadItemAsync <dynamic>(
                partitionKey : partitionKey,
                id : testItem.id,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.UpsertItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.ReplaceItemAsync <dynamic>(
                id : testItem.id,
                item : testItem,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            itemResponse = await container.DeleteItemAsync <dynamic>(
                partitionKey : partitionKey,
                id : testItem.id,
                requestOptions : requestOptions);

            Assert.IsNotNull(itemResponse);
            Assert.AreEqual(httpStatusCode, itemResponse.StatusCode);

            Assert.AreEqual(5, testHandlerHitCount, "An operation did not make it to the handler");

            CosmosJsonSerializerCore jsonSerializer = new CosmosJsonSerializerCore();

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (CosmosResponseMessage streamResponse = await container.CreateItemStreamAsync(
                           partitionKey: partitionKey,
                           streamPayload: itemStream,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (CosmosResponseMessage streamResponse = await container.ReadItemStreamAsync(
                           partitionKey: partitionKey,
                           id: testItem.id,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (CosmosResponseMessage streamResponse = await container.UpsertItemStreamAsync(
                           partitionKey: partitionKey,
                           streamPayload: itemStream,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (CosmosResponseMessage streamResponse = await container.ReplaceItemStreamAsync(
                           partitionKey: partitionKey,
                           id: testItem.id,
                           streamPayload: itemStream,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                using (CosmosResponseMessage streamResponse = await container.DeleteItemStreamAsync(
                           partitionKey: partitionKey,
                           id: testItem.id,
                           requestOptions: requestOptions))
                {
                    Assert.IsNotNull(streamResponse);
                    Assert.AreEqual(httpStatusCode, streamResponse.StatusCode);
                }
            }

            Assert.AreEqual(10, testHandlerHitCount, "A stream operation did not make it to the handler");
        }
Esempio n. 4
0
        public async Task <dynamic> StartImport(IList <NuFoodItem> list)
        {
            if (_cosmosDatabase == null)
            {
                await CreateDatabase();
            }

            if (_cosmosContainer == null)
            {
                await CreateContainer();
            }

            var semaphoreSlim = new SemaphoreSlim(
                initialCount: 10,
                maxCount: 10
                );

            try
            {
                var itemsBucket = new List <KeyValuePair <PartitionKey, Stream> >();
                foreach (var item in list)
                {
                    var stream = new MemoryStream();
                    await JsonSerializer.SerializeAsync <NuFoodItem>(stream, item);

                    itemsBucket.Add(new KeyValuePair <PartitionKey, Stream>(new PartitionKey(item.type), stream));
                }

                var parallelJobs = new List <Task>();
                foreach (var(key, value) in itemsBucket)
                {
                    parallelJobs.Add(
                        Task.Run(async() => {
                        await semaphoreSlim.WaitAsync();

                        try
                        {
                            await Task.Delay(TimeSpan.FromMilliseconds(200));

                            Response response   = await _cosmosContainer.CreateItemStreamAsync(value, key);
                            string retryAfterMs = null;
                            response.Headers.TryGetValue("x-ms-retry-after-ms", out retryAfterMs);

                            _logger.LogInformation($"Response: {response.Status}");

                            if (retryAfterMs != null)
                            {
                                _logger.LogInformation($"Delaying by additional: {retryAfterMs}");
                                await Task.Delay(TimeSpan.FromMilliseconds(double.Parse(retryAfterMs)));
                                await _cosmosContainer.CreateItemStreamAsync(value, key);
                            }
                        }
                        finally
                        {
                            semaphoreSlim.Release();
                        }
                    })
                        );
                }
                await Task.WhenAll(parallelJobs);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return("import");
        }
        private async Task VerifyItemNullPartitionKeyExpectations(
            dynamic testItem,
            ItemRequestOptions requestOptions = null)
        {
            TestHandler testHandler = new TestHandler((request, cancellationToken) =>
            {
                Assert.IsNotNull(request.Headers.PartitionKey);
                Assert.AreEqual(Documents.Routing.PartitionKeyInternal.Undefined.ToString(), request.Headers.PartitionKey.ToString());

                return(Task.FromResult(new CosmosResponseMessage(HttpStatusCode.OK)));
            });

            CosmosClient client = MockCosmosUtil.CreateMockCosmosClient(
                (cosmosClientBuilder) => cosmosClientBuilder.AddCustomHandlers(testHandler));

            CosmosContainer container = client.GetDatabase("testdb")
                                        .GetContainer("testcontainer");

            await container.CreateItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
            {
                await container.ReadItemAsync <dynamic>(
                    partitionKey: null,
                    id: testItem.id,
                    requestOptions: requestOptions);
            }, "ReadItemAsync should throw ArgumentNullException without the correct request option set.");

            await container.UpsertItemAsync <dynamic>(
                item : testItem,
                requestOptions : requestOptions);

            await container.ReplaceItemAsync <dynamic>(
                id : testItem.id,
                item : testItem,
                requestOptions : requestOptions);

            await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
            {
                await container.DeleteItemAsync <dynamic>(
                    partitionKey: null,
                    id: testItem.id,
                    requestOptions: requestOptions);
            }, "DeleteItemAsync should throw ArgumentNullException without the correct request option set.");

            CosmosJsonSerializerCore jsonSerializer = new CosmosJsonSerializerCore();

            using (Stream itemStream = jsonSerializer.ToStream <dynamic>(testItem))
            {
                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.CreateItemStreamAsync(
                        partitionKey: null,
                        streamPayload: itemStream,
                        requestOptions: requestOptions);
                }, "CreateItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.ReadItemStreamAsync(
                        partitionKey: null,
                        id: testItem.id,
                        requestOptions: requestOptions);
                }, "ReadItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.UpsertItemStreamAsync(
                        partitionKey: null,
                        streamPayload: itemStream,
                        requestOptions: requestOptions);
                }, "UpsertItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.ReplaceItemStreamAsync(
                        partitionKey: null,
                        id: testItem.id,
                        streamPayload: itemStream,
                        requestOptions: requestOptions);
                }, "ReplaceItemAsync should throw ArgumentNullException without the correct request option set.");

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(async() =>
                {
                    await container.DeleteItemStreamAsync(
                        partitionKey: null,
                        id: testItem.id,
                        requestOptions: requestOptions);
                }, "DeleteItemAsync should throw ArgumentNullException without the correct request option set.");
            }
        }