コード例 #1
0
        // </ItemOperationsWithValidPartitionKeyValue>

        /// <summary>
        ///  The function demonstrates migrating documents that were inserted without a value for partition key, and those inserted
        ///  pre-migration to other logical partitions, those with a value for partition key.
        /// </summary>
        // <MigratedItemsFromNonePartitionKeyToValidPartitionKeyValue>
        private static async Task MigratedItemsFromNonePartitionKeyToValidPartitionKeyValue(Container container)
        {
            // Pre-create a few items in the container to demo the migration
            const int ItemsToCreate = 4;

            // Insert a few items with no Partition Key
            for (int i = 0; i < ItemsToCreate; i++)
            {
                string itemid = Guid.NewGuid().ToString();
                DeviceInformationItem itemWithoutPK = GetDeviceWithNoPartitionKey(itemid);
                ItemResponse <DeviceInformationItem> createResponse = await container.CreateItemAsync <DeviceInformationItem>(
                    partitionKey : PartitionKey.None,
                    item : itemWithoutPK);
            }

            // Query items on the container that have no partition key value by supplying NonePartitionKeyValue
            // The operation is made in batches to not lose work in case of partial execution
            int             resultsFetched = 0;
            QueryDefinition sql            = new QueryDefinition("select * from r");

            using (FeedIterator <DeviceInformationItem> setIterator = container.GetItemQueryIterator <DeviceInformationItem>(
                       sql,
                       requestOptions: new QueryRequestOptions()
            {
                PartitionKey = PartitionKey.None,
                MaxItemCount = 2
            }))
            {
                while (setIterator.HasMoreResults)
                {
                    FeedResponse <DeviceInformationItem> queryResponse = await setIterator.ReadNextAsync();

                    resultsFetched += queryResponse.Count();

                    // For the items returned with NonePartitionKeyValue
                    IEnumerator <DeviceInformationItem> iter = queryResponse.GetEnumerator();
                    while (iter.MoveNext())
                    {
                        DeviceInformationItem item = iter.Current;
                        if (item.DeviceId != null)
                        {
                            // Using existing deviceID for partition key
                            item.PartitionKey = item.DeviceId;
                            Console.WriteLine("Migrating item {0} to Partition {1}", item.Id, item.DeviceId);
                            // Re-Insert into container with a partition key
                            // This could result in exception if the same item was inserted in a previous run of the program on existing container
                            // and the program stopped before the delete.
                            ItemResponse <DeviceInformationItem> createResponseWithPk = await container.CreateItemAsync <DeviceInformationItem>(
                                partitionKey : new PartitionKey(item.PartitionKey),
                                item : item);

                            // Deleting item from fixed container with CosmosContainerSettings.NonePartitionKeyValue.
                            ItemResponse <DeviceInformationItem> deleteResponseWithoutPk = await container.DeleteItemAsync <DeviceInformationItem>(
                                partitionKey : PartitionKey.None,
                                id : item.Id);
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Tries to the move to the next page in the document producer.
        /// </summary>
        /// <param name="token">The cancellation token.</param>
        /// <returns>Whether the operation was successful.</returns>
        private async Task <bool> MoveNextPage(CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            if (this.bufferedPages.Count == 0)
            {
                return(false);
            }

            if (this.itemsLeftInCurrentPage != 0)
            {
                throw new InvalidOperationException("Tried to move onto the next page before finishing the first page.");
            }

            FeedResponse <CosmosElement> feedResponse = await this.bufferedPages.TakeAsync(token);

            this.previousContinuationToken = this.currentContinuationToken;
            this.currentContinuationToken  = feedResponse.ResponseContinuation;
            this.currentPage            = feedResponse.GetEnumerator();
            this.itemsLeftInCurrentPage = feedResponse.Count;
            if (this.MoveNextDocumentWithinCurrentPage())
            {
                this.isAtBeginningOfPage = true;
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #3
0
        private void UpdateStates(FeedResponse <T> feedResponse)
        {
            this.PreviousResponseContinuation      = this.ResponseContinuation;
            this.ResponseContinuation              = feedResponse.ResponseContinuation;
            this.ItemsTillNextContinuationBoundary = feedResponse.Count;
            this.IsAtContinuationBoundary          = true;
            this.CurrentEnumerator         = feedResponse.GetEnumerator();
            this.RequestStatistics         = feedResponse.RequestStatistics;
            this.TotalResponseLengthBytes += feedResponse.ResponseLengthBytes;

            this.MoveNextInternal();
        }
コード例 #4
0
        //-MANAGEMENT-----------------------------------------------------------------------------------------------------------------
        private async Task <dynamic> Assert_Send_MessageToIotAndCheckIfTheMessageWasSavedInCosmos(string idToSeek, DocumentClient documentClient, Uri collectionToCosmosLink)
        {
            FeedResponse <dynamic> docs = await documentClient.ReadDocumentFeedAsync(collectionToCosmosLink, new FeedOptions { MaxItemCount = 10 });

            IEnumerator <dynamic> docEnumerator = docs.GetEnumerator();

            while (!docEnumerator.MoveNext())
            {
                if (docEnumerator.Current["id"] == idToSeek)
                {
                    break;
                }
            }
            return(JsonConvert.DeserializeObject(docEnumerator.Current.ToString()));
        }
コード例 #5
0
        private async Task <bool> ExecuteCollectionChangeFeedQueryNextResultAsync(CancellationToken cancelToken)
        {
            if ((_collectionChangeFeedQuery != null) && _collectionChangeFeedQuery.HasMoreResults && !cancelToken.IsCancellationRequested)
            {
                FeedResponse <T> collectionChangeFeedResponse = await _collectionChangeFeedQuery.ExecuteNextAsync <T>(cancelToken);

                _collectionPartitionKeyRangesCheckpoints[_collectionPartitionKeyRanges[_collectionPartitionKeyRangeIndex].Id] = collectionChangeFeedResponse.ResponseContinuation;

                _collectionChangeFeedEnumerator = collectionChangeFeedResponse.GetEnumerator();

                return(true);
            }

            return(false);
        }
コード例 #6
0
        private async Task <List <T> > FetchResults <T>(QueryDefinition queryDefinition)
        {
            List <T>         itemList     = new List <T>();
            FeedIterator <T> feedIterator = this.Container.GetItemQueryIterator <T>(queryDefinition);

            while (feedIterator.HasMoreResults)
            {
                FeedResponse <T> queryResponse = await feedIterator.ReadNextAsync();

                IEnumerator <T> iter = queryResponse.GetEnumerator();
                while (iter.MoveNext())
                {
                    itemList.Add(iter.Current);
                }
            }
            return(itemList);
        }
コード例 #7
0
        public async Task Execute(Experiment experiment, DocumentClient client, DocumentCollection collection, Uri documentCollectionUri, int taskId, ConfigurationOptions options)
        {
            try
            {
                IDocumentQuery <dynamic> query = client.CreateDocumentQuery(documentCollectionUri, options.QueryString,
                                                                            new FeedOptions
                {
                    PopulateQueryMetrics      = true,
                    MaxItemCount              = -1,
                    MaxDegreeOfParallelism    = -1,
                    EnableCrossPartitionQuery = true,
                    EnableScanInQuery         = true
                }).AsDocumentQuery();

                while (query.HasMoreResults)
                {
                    FeedResponse <dynamic> response = await query.ExecuteNextAsync <dynamic>();

                    experiment.UpdateRequestUnits(taskId, response.RequestCharge);

                    if (options.Record)
                    {
                        var enumerator = response.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            var current = enumerator.Current;
                            Console.WriteLine("Order {0}", current.ToString());
                        }
                    }
                }
                experiment.IncrementOperationCount();
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to read {0}. Exception was {1}", collection.SelfLink, e);
                if (e is DocumentClientException)
                {
                    DocumentClientException de = (DocumentClientException)e;
                    if (de.StatusCode == HttpStatusCode.Forbidden)
                    {
                        experiment.IncrementOperationCount();
                    }
                }
            }
        }
コード例 #8
0
        public async Task ItemLINQQueryTest()
        {
            //Creating items for query.
            IList <ToDoActivity> itemList = await ToDoActivity.CreateRandomItems(container : this.Container, pkCount : 2, perPKItemCount : 1, randomPartitionKey : true);

            IOrderedQueryable <ToDoActivity> linqQueryable = this.Container.GetItemLinqQueryable <ToDoActivity>();
            IQueryable <ToDoActivity>        queriable     = linqQueryable.Where(item => item.taskNum < 100);
            //V3 Asynchronous query execution with LINQ query generation sql text.
            FeedIterator <ToDoActivity> setIterator = this.Container.GetItemQueryIterator <ToDoActivity>(
                queriable.ToQueryDefinition(),
                requestOptions: new QueryRequestOptions()
            {
                MaxConcurrency = 2
            });

            int resultsFetched = 0;

            while (setIterator.HasMoreResults)
            {
                FeedResponse <ToDoActivity> queryResponse = await setIterator.ReadNextAsync();

                resultsFetched += queryResponse.Count();

                // For the items returned with NonePartitionKeyValue
                IEnumerator <ToDoActivity> iter = queryResponse.GetEnumerator();
                while (iter.MoveNext())
                {
                    ToDoActivity activity = iter.Current;
                    Assert.AreEqual(42, activity.taskNum);
                }
                Assert.AreEqual(2, resultsFetched);
            }

            //LINQ query execution without partition key.
            linqQueryable = this.Container.GetItemLinqQueryable <ToDoActivity>(allowSynchronousQueryExecution: true);
            queriable     = linqQueryable.Where(item => item.taskNum < 100);

            Assert.AreEqual(2, queriable.Count());
            Assert.AreEqual(itemList[0].id, queriable.ToList()[0].id);
            Assert.AreEqual(itemList[1].id, queriable.ToList()[1].id);

            //LINQ query execution with wrong partition key.
            linqQueryable = this.Container.GetItemLinqQueryable <ToDoActivity>(
                allowSynchronousQueryExecution: true,
                requestOptions: new QueryRequestOptions()
            {
                PartitionKey = new Cosmos.PartitionKey("test")
            });
            queriable = linqQueryable.Where(item => item.taskNum < 100);
            Assert.AreEqual(0, queriable.Count());

            //LINQ query execution with correct partition key.
            linqQueryable = this.Container.GetItemLinqQueryable <ToDoActivity>(
                allowSynchronousQueryExecution: true,
                requestOptions: new QueryRequestOptions {
                ConsistencyLevel = Cosmos.ConsistencyLevel.Eventual, PartitionKey = new Cosmos.PartitionKey(itemList[1].pk)
            });
            queriable = linqQueryable.Where(item => item.taskNum < 100);
            Assert.AreEqual(1, queriable.Count());
            Assert.AreEqual(itemList[1].id, queriable.ToList()[0].id);
        }
コード例 #9
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log,
            ExecutionContext context)
        {
            try
            {
                EnsureClient(context);
                string productCode = req.Query["productCode"];

                SqlQuerySpec querySpec = null;

                if (!String.IsNullOrWhiteSpace(productCode))
                {
                    querySpec = new SqlQuerySpec(
                        String.Concat(
                            "SELECT VALUE COUNT(1) FROM Items i WHERE i.productCode IN ('",
                            productCode,
                            "')"));
                }
                else
                {
                    querySpec = new SqlQuerySpec("SELECT VALUE COUNT(1)FROM Items i");
                }

                IDocumentQuery <dynamic> query = cosmosDbClient.CreateDocumentQuery(
                    collectionUri,
                    querySpec,
                    new FeedOptions()
                {
                    EnableCrossPartitionQuery = true,
                    PartitionKey           = null,
                    PopulateQueryMetrics   = true,
                    MaxItemCount           = 50,
                    MaxDegreeOfParallelism = 0,
                    MaxBufferedItemCount   = 100
                }).AsDocumentQuery();

                double totalRUs = 0;

                long count = 0;

                while (query.HasMoreResults)
                {
                    FeedResponse <dynamic> feedResponse = await query.ExecuteNextAsync();

                    Console.WriteLine(feedResponse.RequestCharge);
                    totalRUs += feedResponse.RequestCharge;
                    IReadOnlyDictionary <string, QueryMetrics> partitionIdToQueryMetrics = feedResponse.QueryMetrics;
                    foreach (KeyValuePair <string, QueryMetrics> kvp in partitionIdToQueryMetrics)
                    {
                        string       partitionId  = kvp.Key;
                        QueryMetrics queryMetrics = kvp.Value;
                        Console.WriteLine("{0}: {1}", partitionId, queryMetrics);
                    }

                    IEnumerator <dynamic> docEnumerator = feedResponse.GetEnumerator();
                    while (docEnumerator.MoveNext())
                    {
                        count += (long)docEnumerator.Current;
                    }
                }

                var responsePayload = new ResponseContract
                {
                    Count    = count,
                    TotalRUs = totalRUs,
                };

                log.LogInformation("Count: {0}, Total RUs: {1}", count, totalRUs);

                return(new OkObjectResult(JsonConvert.SerializeObject(responsePayload)));
            }
            catch (Exception error)
            {
                return(new ObjectResult(error.ToString())
                {
                    StatusCode = 500
                });
            }
        }