Example #1
0
        private static async Task ItemStreamFeed(CosmosContainer container)
        {
            int totalCount = 0;

            // SQL
            FeedIterator setIterator = container.GetItemsStreamIterator();

            while (setIterator.HasMoreResults)
            {
                int count = 0;
                using (CosmosResponseMessage response = await setIterator.FetchNextSetAsync())
                {
                    response.EnsureSuccessStatusCode();
                    count++;
                    using (StreamReader sr = new StreamReader(response.Content))
                        using (JsonTextReader jtr = new JsonTextReader(sr))
                        {
                            JsonSerializer jsonSerializer = new JsonSerializer();
                            dynamic        array          = jsonSerializer.Deserialize <dynamic>(jtr);
                            totalCount += array.Documents.Count;
                        }
                }
            }

            Assert("Expected two families", totalCount == 2);
        }
Example #2
0
        private static async Task QueryItemsInPartitionAsStreams(CosmosContainer container)
        {
            // SQL
            CosmosResultSetIterator setIterator = container.Items.CreateItemQueryAsStream("SELECT F.id, F.LastName, F.IsRegistered FROM Families F", partitionKey: "Anderson", maxItemCount: 1);

            int count = 0;

            while (setIterator.HasMoreResults)
            {
                using (CosmosResponseMessage response = await setIterator.FetchNextSetAsync())
                {
                    response.EnsureSuccessStatusCode();
                    count++;
                    using (StreamReader sr = new StreamReader(response.Content))
                        using (JsonTextReader jtr = new JsonTextReader(sr))
                        {
                            JsonSerializer jsonSerializer = new JsonSerializer();
                            dynamic        items          = jsonSerializer.Deserialize <dynamic>(jtr);
                            Assert("Expected one family", items.Documents.Count == 1);
                            dynamic item = items.Documents[0];
                            Assert($"Expected LastName: Anderson Actual: {item.LastName}", string.Equals("Anderson", item.LastName.ToString(), StringComparison.InvariantCulture));
                        }
                }
            }

            Assert("Expected 1 family", count == 1);
        }
        internal static CosmosQueryResponse <TInput> CreateCosmosQueryFeedResponse <TInput>(
            CosmosResponseMessage cosmosResponseMessage,
            CosmosJsonSerializer jsonSerializer)
        {
            using (cosmosResponseMessage)
            {
                // Throw the exception if the query failed.
                cosmosResponseMessage.EnsureSuccessStatusCode();

                string continuationToken = ChangeFeedResultSetStreamIterator.GetContinuationToken(cosmosResponseMessage);
                bool   hasMoreResults    = ChangeFeedResultSetStreamIterator.GetHasMoreResults(continuationToken, cosmosResponseMessage.Headers.ContentLengthAsLong);

                return(CosmosQueryResponse <TInput> .CreateResponse <TInput>(
                           stream : cosmosResponseMessage.Content,
                           jsonSerializer : jsonSerializer,
                           continuationToken : continuationToken,
                           hasMoreResults : hasMoreResults));
            }
        }
Example #4
0
        private async Task <long> GetRemainingWorkAsync(DocumentServiceLease existingLease, CancellationToken cancellationToken)
        {
            // Current lease schema maps Token to PKRangeId
            string       partitionKeyRangeId = existingLease.CurrentLeaseToken;
            FeedIterator iterator            = this.feedCreator(
                partitionKeyRangeId,
                existingLease.ContinuationToken,
                string.IsNullOrEmpty(existingLease.ContinuationToken));

            try
            {
                CosmosResponseMessage response = await iterator.FetchNextSetAsync(cancellationToken).ConfigureAwait(false);

                if (response.StatusCode != HttpStatusCode.NotModified)
                {
                    response.EnsureSuccessStatusCode();
                }

                long parsedLSNFromSessionToken = RemainingWorkEstimatorCore.TryConvertToNumber(ExtractLsnFromSessionToken(response.Headers[HttpConstants.HttpHeaders.SessionToken]));
                Collection <JObject> items     = RemainingWorkEstimatorCore.GetItemsFromResponse(response);
                long lastQueryLSN = items.Count > 0
                    ? RemainingWorkEstimatorCore.TryConvertToNumber(RemainingWorkEstimatorCore.GetFirstItemLSN(items)) - 1
                    : parsedLSNFromSessionToken;
                if (lastQueryLSN < 0)
                {
                    return(1);
                }

                long leaseTokenRemainingWork = parsedLSNFromSessionToken - lastQueryLSN;
                return(leaseTokenRemainingWork < 0 ? 0 : leaseTokenRemainingWork);
            }
            catch (Exception clientException)
            {
                DefaultTrace.TraceException(clientException);
                DefaultTrace.TraceWarning("GetEstimateWork > exception: lease token '{0}'", existingLease.CurrentLeaseToken);
                throw;
            }
        }