Esempio n. 1
0
            public override bool Equals(object obj)
            {
                ToDoActivity input = obj as ToDoActivity;

                if (input == null)
                {
                    return(false);
                }

                return(string.Equals(this.id, input.id) &&
                       this.taskNum == input.taskNum &&
                       this.cost == input.cost &&
                       string.Equals(this.description, input.description) &&
                       string.Equals(this.status, input.status));
            }
Esempio n. 2
0
        public async Task TargetMultipleLogicalPartitionKeys()
        {
            int batchSize = 25;

            string pkToRead1 = "pkToRead1";
            string pkToRead2 = "pkToRead2";
            string otherPK   = "otherPK";

            for (int i = 0; i < batchSize; i++)
            {
                await this.Container.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead1));
            }

            for (int i = 0; i < batchSize; i++)
            {
                await this.Container.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead2));
            }

            for (int i = 0; i < batchSize; i++)
            {
                await this.Container.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: otherPK));
            }

            // Create one start state for each logical partition key.
            List <FeedRangeState <ReadFeedState> > feedRangeStates = new List <FeedRangeState <ReadFeedState> >();
            IReadOnlyList <string> partitionKeysToTarget           = new List <string>()
            {
                pkToRead1,
                pkToRead2
            };

            foreach (string partitionKeyToTarget in partitionKeysToTarget)
            {
                feedRangeStates.Add(
                    new FeedRangeState <ReadFeedState>(
                        (FeedRangeInternal)FeedRange.FromPartitionKey(
                            new Cosmos.PartitionKey(partitionKeyToTarget)),
                        ReadFeedState.Beginning()));
            }

            // Use the list composition property of the constructor to merge them in to a single state.
            ReadFeedCrossFeedRangeState multipleLogicalPartitionKeyState = new ReadFeedCrossFeedRangeState(feedRangeStates.ToImmutableArray());
            IAsyncEnumerable <TryCatch <ReadFeedPage> > asyncEnumerable  = this.Container.GetReadFeedAsyncEnumerable(multipleLogicalPartitionKeyState);

            (int totalCount, ReadFeedCrossFeedRangeState? _) = await DrainAllAsync(asyncEnumerable);

            Assert.AreEqual(2 * batchSize, totalCount);
        }
        public void ValidateSerializer()
        {
            CosmosDefaultJsonSerializer cosmosDefaultJsonSerializer = new CosmosDefaultJsonSerializer();

            using (Stream stream = cosmosDefaultJsonSerializer.ToStream <ToDoActivity>(toDoActivity))
            {
                Assert.IsNotNull(stream);
                ToDoActivity result = cosmosDefaultJsonSerializer.FromStream <ToDoActivity>(stream);
                Assert.IsNotNull(result);
                Assert.AreEqual(toDoActivity.id, result.id);
                Assert.AreEqual(toDoActivity.taskNum, result.taskNum);
                Assert.AreEqual(toDoActivity.cost, result.cost);
                Assert.AreEqual(toDoActivity.description, result.description);
                Assert.AreEqual(toDoActivity.status, result.status);
            }
        }
    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        var session = context.SynchronizedStorageSession.AzureTablePersistenceSession();

        var test1 = new ToDoActivity {
            PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString()
        };
        var test2 = new ToDoActivity {
            PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString()
        };
        var test3 = new ToDoActivity {
            PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString()
        };
        var test4 = new ToDoActivity {
            PartitionKey = session.PartitionKey, RowKey = Guid.NewGuid().ToString()
        };

        session.Batch.Add(TableOperation.Insert(test1));
        session.Batch.Add(TableOperation.Replace(test2));
        session.Batch.Add(TableOperation.InsertOrReplace(test3));
        session.Batch.Add(TableOperation.Delete(test4));

        return(Task.CompletedTask);
    }
Esempio n. 5
0
        public async Task ChangeFeedIteratorCore_PartitionKey_OfT_ReadAll()
        {
            int totalCount    = 0;
            int firstRunTotal = 25;
            int batchSize     = 25;

            string pkToRead = "pkToRead";
            string otherPK  = "otherPK";

            ContainerInternal itemsCore = await this.InitializeContainerAsync();

            for (int i = 0; i < batchSize; i++)
            {
                await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead));
            }

            for (int i = 0; i < batchSize; i++)
            {
                await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: otherPK));
            }

            FeedIterator <ToDoActivity> feedIterator = itemsCore.GetChangeFeedIterator <ToDoActivity>(
                ChangeFeedStartFrom.Beginning(
                    new FeedRangePartitionKey(
                        new PartitionKey(pkToRead))),
                ChangeFeedMode.Incremental,
                new ChangeFeedRequestOptions()
            {
                PageSizeHint = 1,
            });
            string continuation = null;

            while (feedIterator.HasMoreResults)
            {
                try
                {
                    FeedResponse <ToDoActivity> feedResponse = await feedIterator.ReadNextAsync(this.cancellationToken);

                    totalCount += feedResponse.Count;
                    foreach (ToDoActivity toDoActivity in feedResponse)
                    {
                        Assert.AreEqual(pkToRead, toDoActivity.pk);
                    }

                    continuation = feedResponse.ContinuationToken;
                }
                catch (CosmosException cosmosException) when(cosmosException.StatusCode == HttpStatusCode.NotModified)
                {
                    continuation = cosmosException.Headers.ContinuationToken;
                    break;
                }
            }

            Assert.AreEqual(firstRunTotal, totalCount);

            int expectedFinalCount = 50;

            // Insert another batch of 25 and use the last FeedToken from the first cycle
            for (int i = 0; i < batchSize; i++)
            {
                await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead));
            }

            FeedIterator <ToDoActivity> setIteratorNew = itemsCore.GetChangeFeedIterator <ToDoActivity>(
                ChangeFeedStartFrom.ContinuationToken(continuation),
                ChangeFeedMode.Incremental);

            while (setIteratorNew.HasMoreResults)
            {
                try
                {
                    FeedResponse <ToDoActivity> feedResponse = await setIteratorNew.ReadNextAsync(this.cancellationToken);

                    totalCount += feedResponse.Count;
                    foreach (ToDoActivity toDoActivity in feedResponse)
                    {
                        Assert.AreEqual(pkToRead, toDoActivity.pk);
                    }
                }
                catch (CosmosException cosmosException) when(cosmosException.StatusCode == HttpStatusCode.NotModified)
                {
                    break;
                }
            }

            Assert.AreEqual(expectedFinalCount, totalCount);
        }
Esempio n. 6
0
        public async Task ChangeFeedIteratorCore_PartitionKey_ReadAll()
        {
            int totalCount    = 0;
            int firstRunTotal = 25;
            int batchSize     = 25;

            string pkToRead = "pkToRead";
            string otherPK  = "otherPK";

            ContainerInternal itemsCore = await this.InitializeContainerAsync();

            for (int i = 0; i < batchSize; i++)
            {
                await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead));
            }

            for (int i = 0; i < batchSize; i++)
            {
                await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: otherPK));
            }

            ChangeFeedIteratorCore feedIterator = itemsCore.GetChangeFeedStreamIterator(
                ChangeFeedStartFrom.Beginning(
                    FeedRange.FromPartitionKey(
                        new PartitionKey(pkToRead))),
                ChangeFeedMode.Incremental,
                new ChangeFeedRequestOptions()
            {
                PageSizeHint = 1,
            }) as ChangeFeedIteratorCore;
            string continuation = null;

            while (feedIterator.HasMoreResults)
            {
                using (ResponseMessage responseMessage =
                           await feedIterator.ReadNextAsync(this.cancellationToken))
                {
                    if (!responseMessage.IsSuccessStatusCode)
                    {
                        continuation = responseMessage.ContinuationToken;
                        break;
                    }

                    Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                    totalCount += response.Count;
                    foreach (ToDoActivity toDoActivity in response)
                    {
                        Assert.AreEqual(pkToRead, toDoActivity.pk);
                    }
                }
            }

            Assert.AreEqual(firstRunTotal, totalCount);

            int expectedFinalCount = 50;

            // Insert another batch of 25 and use the last FeedToken from the first cycle
            for (int i = 0; i < batchSize; i++)
            {
                await itemsCore.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead));
            }

            ChangeFeedIteratorCore setIteratorNew = itemsCore.GetChangeFeedStreamIterator(
                ChangeFeedStartFrom.ContinuationToken(continuation),
                ChangeFeedMode.Incremental) as ChangeFeedIteratorCore;

            while (setIteratorNew.HasMoreResults)
            {
                using (ResponseMessage responseMessage =
                           await setIteratorNew.ReadNextAsync(this.cancellationToken))
                {
                    if (!responseMessage.IsSuccessStatusCode)
                    {
                        break;
                    }

                    Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                    totalCount += response.Count;
                    foreach (ToDoActivity toDoActivity in response)
                    {
                        Assert.AreEqual(pkToRead, toDoActivity.pk);
                    }
                }
            }

            Assert.AreEqual(expectedFinalCount, totalCount);
        }
Esempio n. 7
0
        public async Task ItemStreamContractVerifier()
        {
            string    PartitionKey = "/pk";
            Container container    = await this.database.CreateContainerAsync(
                new ContainerProperties(id : Guid.NewGuid().ToString(), partitionKeyPath : PartitionKey),
                cancellationToken : this.cancellationToken);

            int totalCount = 4;
            Dictionary <string, ToDoActivity> toDoActivities = new Dictionary <string, ToDoActivity>();

            // Create 3 constant items;
            for (int i = 0; i < totalCount; i++)
            {
                ToDoActivity toDoActivity = new ToDoActivity()
                {
                    id          = "toDoActivity" + i,
                    status      = "InProgress",
                    cost        = 9000 + i,
                    description = "Constant to do activity",
                    taskNum     = i
                };

                toDoActivities.Add(toDoActivity.id, toDoActivity);

                await container.CreateItemAsync <ToDoActivity>(toDoActivity);
            }

            List <FeedIterator> FeedIterators = new List <FeedIterator>();

            // The stream contract should return the same contract as read feed.
            // {
            //    "_rid": "containerRid",
            //    "Documents": [{
            //        "id": "03230",
            //        "_rid": "qHVdAImeKAQBAAAAAAAAAA==",
            //        "_self": "dbs\/qHVdAA==\/colls\/qHVdAImeKAQ=\/docs\/qHVdAImeKAQBAAAAAAAAAA==\/",
            //        "_etag": "\"410000b0-0000-0000-0000-597916b00000\"",
            //        "_attachments": "attachments\/",
            //        "_ts": 1501107886
            //    }],
            //    "_count": 1
            // }

            FeedIterator setIterator =
                container.GetItemQueryStreamIterator();

            FeedIterators.Add(setIterator);

            QueryRequestOptions options = new QueryRequestOptions()
            {
                MaxItemCount   = 4,
                MaxConcurrency = 1,
            };

            FeedIterator queryIterator = container.GetItemQueryStreamIterator(
                queryText: @"select * from t where t.id != """" ",
                requestOptions: options);

            FeedIterators.Add(queryIterator);
            foreach (FeedIterator iterator in FeedIterators)
            {
                int count = 0;
                while (iterator.HasMoreResults)
                {
                    ResponseMessage response = await iterator.ReadNextAsync(this.cancellationToken);

                    response.EnsureSuccessStatusCode();

                    using (StreamReader sr = new StreamReader(response.Content))
                    {
                        string jsonString = await sr.ReadToEndAsync();

                        Assert.IsNotNull(jsonString);
                        JObject jObject = JsonConvert.DeserializeObject <JObject>(jsonString);
                        Assert.IsNotNull(jObject["Documents"]);
                        Assert.IsNotNull(jObject["_rid"]);
                        Assert.IsNotNull(jObject["_count"]);
                        Assert.IsTrue(jObject["_count"].ToObject <int>() >= 0);
                        foreach (JObject item in jObject["Documents"])
                        {
                            count++;
                            Assert.IsNotNull(item["id"]);
                            ToDoActivity createdItem = toDoActivities[item["id"].ToString()];

                            Assert.AreEqual(createdItem.taskNum, item["taskNum"].ToObject <int>());
                            Assert.AreEqual(createdItem.cost, item["cost"].ToObject <double>());
                            Assert.AreEqual(createdItem.description, item["description"].ToString());
                            Assert.AreEqual(createdItem.status, item["status"].ToString());
                            Assert.IsNotNull(item["_rid"]);
                            Assert.IsNotNull(item["_self"]);
                            Assert.IsNotNull(item["_etag"]);
                            Assert.IsNotNull(item["_attachments"]);
                            Assert.IsNotNull(item["_ts"]);
                        }
                    }
                }

                Assert.AreEqual(totalCount, count);
            }
        }
Esempio n. 8
0
        public async Task ReadFeedIteratorCore_ReadOnlyPartitionKey()
        {
            int totalCount    = 0;
            int firstRunTotal = 25;
            int batchSize     = 25;

            string pkToRead = "pkToRead";
            string otherPK  = "otherPK";

            for (int i = 0; i < batchSize; i++)
            {
                await this.LargerContainer.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: pkToRead));
            }

            for (int i = 0; i < batchSize; i++)
            {
                await this.LargerContainer.CreateItemAsync(ToDoActivity.CreateRandomToDoActivity(pk: otherPK));
            }

            ContainerInternal itemsCore    = this.LargerContainer;
            FeedIterator      feedIterator = itemsCore.GetItemQueryStreamIterator(
                requestOptions: new QueryRequestOptions()
            {
                PartitionKey = new PartitionKey(pkToRead), MaxItemCount = 1
            });

            while (feedIterator.HasMoreResults)
            {
                using (ResponseMessage responseMessage =
                           await feedIterator.ReadNextAsync(this.cancellationToken))
                {
                    if (responseMessage.IsSuccessStatusCode)
                    {
                        Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                        totalCount += response.Count;
                        foreach (ToDoActivity toDoActivity in response)
                        {
                            Assert.AreEqual(pkToRead, toDoActivity.pk);
                        }
                    }
                }
            }

            Assert.AreEqual(firstRunTotal, totalCount);

            string continuationToken = null;

            totalCount = 0;
            do
            {
                feedIterator = itemsCore.GetItemQueryStreamIterator(
                    requestOptions: new QueryRequestOptions()
                {
                    PartitionKey = new PartitionKey(pkToRead),
                    MaxItemCount = 1,
                },
                    continuationToken: continuationToken);

                using (ResponseMessage responseMessage =
                           await feedIterator.ReadNextAsync(this.cancellationToken))
                {
                    responseMessage.EnsureSuccessStatusCode();

                    Collection <ToDoActivity> response = TestCommon.SerializerCore.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(responseMessage.Content).Data;
                    totalCount += response.Count;
                    foreach (ToDoActivity toDoActivity in response)
                    {
                        Assert.AreEqual(pkToRead, toDoActivity.pk);
                    }

                    continuationToken = responseMessage.ContinuationToken;
                }
            }while (continuationToken != null);

            Assert.AreEqual(firstRunTotal, totalCount);
        }
 public Customadapter(ToDoActivity toDoActivity, List <string> taskList, DbHelper dbHelper)
 {
     this.toDoActivity = toDoActivity;
     this.taskList     = taskList;
     this.dbHelper     = dbHelper;
 }