/// <inheritdoc/>
        public async Task <InstanceQueryResponse> GetInstancesOfApplication(
            Dictionary <string, StringValues> queryParams,
            string continuationToken,
            int size)
        {
            InstanceQueryResponse queryResponse = new InstanceQueryResponse();

            FeedOptions feedOptions = new FeedOptions
            {
                EnableCrossPartitionQuery = true,
                MaxItemCount = size,
            };

            if (continuationToken != null)
            {
                feedOptions.RequestContinuation = continuationToken;
            }

            IQueryable <Instance> queryBuilder = _client.CreateDocumentQuery <Instance>(_collectionUri, feedOptions);

            try
            {
                queryBuilder = BuildQueryFromParameters(queryParams, queryBuilder);
            }
            catch (Exception e)
            {
                queryResponse.Exception = e.Message;
                return(queryResponse);
            }

            try
            {
                IDocumentQuery <Instance> documentQuery = queryBuilder.AsDocumentQuery();

                FeedResponse <Instance> feedResponse = await documentQuery.ExecuteNextAsync <Instance>();

                if (!feedResponse.Any())
                {
                    queryResponse.Count     = 0;
                    queryResponse.TotalHits = 0;

                    return(queryResponse);
                }

                string nextContinuationToken = feedResponse.ResponseContinuation;

                logger.LogInformation($"continuation token: {nextContinuationToken}");

                // this migth be expensive
                feedOptions.RequestContinuation = null;
                int totalHits = queryBuilder.Count();
                queryResponse.TotalHits = totalHits;

                List <Instance> instances = feedResponse.ToList <Instance>();

                PostProcess(instances);

                queryResponse.Instances         = instances;
                queryResponse.ContinuationToken = nextContinuationToken;
                queryResponse.Count             = instances.Count;
            }
            catch (Exception e)
            {
                logger.LogError("error: {e}");
                queryResponse.Exception = e.Message;
            }

            return(queryResponse);
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public async Task <InstanceQueryResponse> GetInstancesFromQuery(
            Dictionary <string, StringValues> queryParams,
            string continuationToken,
            int size)
        {
            InstanceQueryResponse queryResponse = new InstanceQueryResponse
            {
                Count     = 0,
                Instances = new List <Instance>()
            };

            while (queryResponse.Count < size)
            {
                FeedOptions feedOptions = new FeedOptions
                {
                    EnableCrossPartitionQuery = true,
                    MaxItemCount = size - queryResponse.Count,
                    ResponseContinuationTokenLimitInKb = 7
                };

                if (!string.IsNullOrEmpty(continuationToken))
                {
                    feedOptions.RequestContinuation = continuationToken;
                }

                IQueryable <Instance> queryBuilder = Client.CreateDocumentQuery <Instance>(CollectionUri, feedOptions);

                try
                {
                    queryBuilder = BuildQueryFromParameters(queryParams, queryBuilder)
                                   .Where(i => !i.Status.IsHardDeleted);
                }
                catch (Exception e)
                {
                    queryResponse.Exception = e.Message;
                    return(queryResponse);
                }

                try
                {
                    IDocumentQuery <Instance> documentQuery = queryBuilder.AsDocumentQuery();

                    FeedResponse <Instance> feedResponse = await documentQuery.ExecuteNextAsync <Instance>();

                    if (feedResponse.Count == 0 && !documentQuery.HasMoreResults)
                    {
                        queryResponse.ContinuationToken = string.Empty;
                        break;
                    }

                    List <Instance> instances = feedResponse.ToList();
                    await PostProcess(instances);

                    queryResponse.Instances.AddRange(instances);
                    queryResponse.Count += instances.Count;

                    if (string.IsNullOrEmpty(feedResponse.ResponseContinuation))
                    {
                        queryResponse.ContinuationToken = string.Empty;
                        break;
                    }

                    queryResponse.ContinuationToken = feedResponse.ResponseContinuation;
                    continuationToken = feedResponse.ResponseContinuation;
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Exception querying CosmosDB for instances");
                    queryResponse.Exception = e.Message;
                    break;
                }
            }

            return(queryResponse);
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public async Task <InstanceQueryResponse> GetInstancesFromQuery(
            Dictionary <string, StringValues> queryParams,
            string continuationToken,
            int size)
        {
            InstanceQueryResponse queryResponse = new InstanceQueryResponse
            {
                Count     = 0,
                Instances = new List <Instance>()
            };

            while (queryResponse.Count < size)
            {
                FeedOptions feedOptions = new FeedOptions
                {
                    EnableCrossPartitionQuery = true,
                    MaxItemCount = size - queryResponse.Count,
                };

                if (continuationToken != null)
                {
                    feedOptions.RequestContinuation = continuationToken;
                }

                IQueryable <Instance> queryBuilder = _client.CreateDocumentQuery <Instance>(_collectionUri, feedOptions);

                try
                {
                    queryBuilder = BuildQueryFromParameters(queryParams, queryBuilder);
                }
                catch (Exception e)
                {
                    queryResponse.Exception = e.Message;
                    return(queryResponse);
                }

                try
                {
                    IDocumentQuery <Instance> documentQuery = queryBuilder.AsDocumentQuery();

                    FeedResponse <Instance> feedResponse = await documentQuery.ExecuteNextAsync <Instance>();

                    if (feedResponse.Count == 0)
                    {
                        queryResponse.ContinuationToken = string.Empty;
                        break;
                    }

                    List <Instance> instances = feedResponse.ToList();
                    await PostProcess(instances);

                    queryResponse.Instances.AddRange(instances);
                    queryResponse.Count += instances.Count;

                    if (string.IsNullOrEmpty(feedResponse.ResponseContinuation))
                    {
                        queryResponse.ContinuationToken = string.Empty;
                        break;
                    }

                    queryResponse.ContinuationToken = feedResponse.ResponseContinuation;
                    continuationToken = feedResponse.ResponseContinuation;
                }
                catch (Exception e)
                {
                    _logger.LogError("error: {e}");
                    queryResponse.Exception = e.Message;
                    break;
                }
            }

            return(queryResponse);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public async Task <InstanceQueryResponse> GetInstancesFromQuery(
            Dictionary <string, StringValues> queryParams,
            string continuationToken,
            int size)
        {
            InstanceQueryResponse queryResponse = new InstanceQueryResponse
            {
                Count     = 0,
                Instances = new List <Instance>()
            };

            while (queryResponse.Count < size)
            {
                QueryRequestOptions options = new QueryRequestOptions()
                {
                    MaxBufferedItemCount = 0, MaxConcurrency = -1, MaxItemCount = size - queryResponse.Count, ResponseContinuationTokenLimitInKb = 7
                };

                string tokenValue = string.IsNullOrEmpty(continuationToken) ? null : continuationToken;
                IQueryable <Instance> queryBuilder = Container.GetItemLinqQueryable <Instance>(requestOptions: options, continuationToken: tokenValue);

                try
                {
                    queryBuilder = BuildQueryFromParameters(queryParams, queryBuilder);
                }
                catch (Exception e)
                {
                    queryResponse.Exception = e.Message;
                    return(queryResponse);
                }

                try
                {
                    var iterator = queryBuilder.ToFeedIterator();

                    FeedResponse <Instance> feedResponse = await iterator.ReadNextAsync();

                    if (feedResponse.Count == 0 && !iterator.HasMoreResults)
                    {
                        queryResponse.ContinuationToken = string.Empty;
                        break;
                    }

                    List <Instance> instances = feedResponse.ToList();
                    await PostProcess(instances);

                    queryResponse.Instances.AddRange(instances);
                    queryResponse.Count += instances.Count;

                    if (string.IsNullOrEmpty(feedResponse.ContinuationToken))
                    {
                        queryResponse.ContinuationToken = string.Empty;
                        break;
                    }

                    queryResponse.ContinuationToken = feedResponse.ContinuationToken;
                    continuationToken = feedResponse.ContinuationToken;
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Exception querying CosmosDB for instances");
                    queryResponse.Exception = e.Message;
                    break;
                }
            }

            return(queryResponse);
        }