示例#1
0
        public async ValueTask <bool> MoveNextAsync()
        {
            if (!await this.enumerator.MoveNextAsync())
            {
                return(false);
            }

            TryCatch <CrossFeedRangePage <Pagination.ReadFeedPage, ReadFeedState> > monadicInnerReadFeedPage = this.enumerator.Current;

            if (monadicInnerReadFeedPage.Failed)
            {
                this.Current = TryCatch <ReadFeedPage> .FromException(monadicInnerReadFeedPage.Exception);

                return(true);
            }

            CrossFeedRangePage <Pagination.ReadFeedPage, ReadFeedState> innerReadFeedPage = monadicInnerReadFeedPage.Result;
            CrossFeedRangeState <ReadFeedState> crossFeedRangeState = innerReadFeedPage.State;
            ReadFeedCrossFeedRangeState?        state = crossFeedRangeState != null ? new ReadFeedCrossFeedRangeState(crossFeedRangeState.Value) : (ReadFeedCrossFeedRangeState?)null;

            CosmosArray documents = CosmosQueryClientCore.ParseElementsFromRestStream(
                innerReadFeedPage.Page.Content,
                Documents.ResourceType.Document,
                cosmosSerializationOptions: null);
            ReadFeedPage page = new ReadFeedPage(
                documents,
                innerReadFeedPage.Page.RequestCharge,
                innerReadFeedPage.Page.ActivityId,
                state);

            this.Current = TryCatch <ReadFeedPage> .FromResult(page);

            return(true);
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ItemBenchmark"/> class.
        /// </summary>
        public EndToEnd()
        {
            if (string.IsNullOrEmpty(this.accountEndpoint) && string.IsNullOrEmpty(this.accountKey))
            {
                return;
            }

            CosmosClientBuilder clientBuilder = new CosmosClientBuilder(
                accountEndpoint: this.accountEndpoint,
                authKeyOrResourceToken: this.accountKey);

            this.client = clientBuilder.Build();
            Database          db = this.client.CreateDatabaseIfNotExistsAsync("BenchmarkDB").Result;
            ContainerResponse containerResponse = db.CreateContainerIfNotExistsAsync(
                id: "BenchmarkContainer",
                partitionKeyPath: "/id",
                throughput: 10000).Result;

            this.container = containerResponse;

            if (containerResponse.StatusCode == HttpStatusCode.Created)
            {
                string path = $"TestJsons/NutritionData.json";
                string json = TextFileConcatenation.ReadMultipartFile(path);
                json = JsonTestUtils.RandomSampleJson(json, seed: 42, maxNumberOfItems: 1000);

                CosmosArray cosmosArray = CosmosArray.Parse(json);
                foreach (CosmosElement document in cosmosArray)
                {
                    ItemResponse <CosmosElement> itemResponse = this.container.CreateItemAsync(document).Result;
                }
            }
        }
        public static (QueryResponse queryResponse, IList <ToDoItem> items) Create(
            string itemIdPrefix,
            string continuationToken,
            string collectionRid,
            int itemCount = 50)
        {
            // Use -1 to represent a split response
            if (itemCount == QueryResponseMessageFactory.SPLIT)
            {
                return(CreateSplitResponse(collectionRid), new List <ToDoItem>().AsReadOnly());
            }

            IList <ToDoItem> items               = ToDoItem.CreateItems(itemCount, itemIdPrefix);
            MemoryStream     memoryStream        = (MemoryStream)cosmosSerializer.ToStream <IList <ToDoItem> >(items);
            long             responseLengthBytes = memoryStream.Length;

            IJsonNavigator     jsonNavigator     = JsonNavigator.Create(memoryStream.ToArray());
            IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
            CosmosArray        cosmosArray       = CosmosArray.Create(jsonNavigator, jsonNavigatorNode);

            Headers headers = new Headers();

            headers.ContinuationToken = continuationToken;
            headers.ActivityId        = Guid.NewGuid().ToString();

            QueryResponse message = QueryResponse.CreateSuccess(
                result: cosmosArray,
                count: itemCount,
                responseHeaders: CosmosQueryResponseMessageHeaders.ConvertToQueryHeaders(headers, ResourceType.Document, collectionRid),
                responseLengthBytes: responseLengthBytes);

            return(message, items);
        }
        public void SqlArrayCreateScalarExpressionTest()
        {
            SqlArrayCreateScalarExpression inner = SqlArrayCreateScalarExpression.Create(
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(1)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(2)),
                SqlLiteralScalarExpression.Create(SqlNumberLiteral.Create(3)));

            AssertEvaluation(
                CosmosArray.Create(
                    new List <CosmosElement>()
            {
                CosmosNumber64.Create(1),
                CosmosNumber64.Create(2),
                CosmosNumber64.Create(3),
            }),
                inner);

            SqlArrayCreateScalarExpression outer = SqlArrayCreateScalarExpression.Create(inner);

            AssertEvaluation(
                CosmosArray.Create(
                    new List <CosmosElement>()
            {
                CosmosArray.Create(
                    new List <CosmosElement>()
                {
                    CosmosNumber64.Create(1),
                    CosmosNumber64.Create(2),
                    CosmosNumber64.Create(3),
                })
            }),
                outer);
        }
        public static CosmosElement ToCosmosElement(OrderByContinuationToken orderByContinuationToken)
        {
            CosmosElement        compositeContinuationToken = ParallelContinuationToken.ToCosmosElement(orderByContinuationToken.ParallelContinuationToken);
            List <CosmosElement> orderByItemsRaw            = new List <CosmosElement>();

            foreach (OrderByItem orderByItem in orderByContinuationToken.OrderByItems)
            {
                orderByItemsRaw.Add(OrderByItem.ToCosmosElement(orderByItem));
            }

            CosmosArray orderByItems = CosmosArray.Create(orderByItemsRaw);

            CosmosElement filter = orderByContinuationToken.Filter == null?CosmosNull.Create() : (CosmosElement)CosmosString.Create(orderByContinuationToken.Filter);

            CosmosObject cosmosObject = CosmosObject.Create(
                new Dictionary <string, CosmosElement>()
            {
                { PropertyNames.CompositeToken, compositeContinuationToken },
                { PropertyNames.OrderByItems, orderByItems },
                { PropertyNames.Rid, CosmosString.Create(orderByContinuationToken.Rid) },
                { PropertyNames.SkipCount, CosmosNumber64.Create(orderByContinuationToken.SkipCount) },
                { PropertyNames.Filter, filter },
            });

            return(cosmosObject);
        }
示例#6
0
        public static (QueryResponseCore queryResponse, IList <ToDoItem> items) Create(
            string itemIdPrefix,
            string continuationToken,
            string collectionRid,
            int itemCount = 50)
        {
            // Use -1 to represent a split response
            if (itemCount == QueryResponseMessageFactory.SPLIT)
            {
                return(CreateSplitResponse(collectionRid), new List <ToDoItem>().AsReadOnly());
            }

            IList <ToDoItem> items               = ToDoItem.CreateItems(itemCount, itemIdPrefix);
            MemoryStream     memoryStream        = (MemoryStream)cosmosSerializer.ToStream <IList <ToDoItem> >(items);
            long             responseLengthBytes = memoryStream.Length;

            IJsonNavigator     jsonNavigator     = JsonNavigator.Create(memoryStream.ToArray());
            IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
            CosmosArray        cosmosArray       = CosmosArray.Create(jsonNavigator, jsonNavigatorNode);

            QueryResponseCore message = QueryResponseCore.CreateSuccess(
                result: cosmosArray,
                continuationToken: continuationToken,
                disallowContinuationTokenMessage: null,
                activityId: Guid.NewGuid().ToString(),
                requestCharge: 42,
                queryMetricsText: null,
                queryMetrics: new Dictionary <string, QueryMetrics>(),
                requestStatistics: null,
                responseLengthBytes: responseLengthBytes);

            return(message, items);
        }
示例#7
0
        public void ArrayOrderMatters()
        {
            CosmosArray cosmosArray         = (CosmosArray)Elements.ArrayWithItems;
            CosmosArray cosmosArrayReversed = CosmosArray.Create(cosmosArray.Reverse());

            Assert.AreNotEqual(cosmosArray, cosmosArrayReversed);
        }
示例#8
0
        public void ARRAY_CONCAT()
        {
            // ARRAY_CONCAT(["apples"], ["strawberries"], ["bananas"])
            AssertEvaluation(
                CosmosArray.Create(CosmosString.Create("apples"), CosmosString.Create("strawberries"), CosmosString.Create("bananas")),
                SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayConcat,
                    JTokenToSqlScalarExpression.Convert(new JArray("apples")),
                    JTokenToSqlScalarExpression.Convert(new JArray("strawberries")),
                    JTokenToSqlScalarExpression.Convert(new JArray("bananas"))));

            // ARRAY_CONCAT("apples", ["strawberries"], ["bananas"])
            AssertEvaluation(
                Undefined,
                SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayConcat,
                    JTokenToSqlScalarExpression.Convert("apples"),
                    JTokenToSqlScalarExpression.Convert(new JArray("strawberries")),
                    JTokenToSqlScalarExpression.Convert(new JArray("bananas"))));

            // ARRAY_CONCAT(undefined, ["strawberries"], ["bananas"])
            AssertEvaluation(
                Undefined,
                SqlFunctionCallScalarExpression.CreateBuiltin(
                    SqlFunctionCallScalarExpression.Identifiers.ArrayConcat,
                    SqlLiteralScalarExpression.SqlUndefinedLiteralScalarExpression,
                    JTokenToSqlScalarExpression.Convert(new JArray("strawberries")),
                    JTokenToSqlScalarExpression.Convert(new JArray("bananas"))));
        }
示例#9
0
        private static QueryResponseCore GetCosmosElementResponse(
            Guid clientQueryCorrelationId,
            QueryRequestOptions requestOptions,
            ResourceType resourceType,
            ResponseMessage cosmosResponseMessage,
            PartitionKeyRangeIdentity partitionKeyRangeIdentity,
            Action <QueryPageDiagnostics> queryPageDiagnostics)
        {
            using (cosmosResponseMessage)
            {
                QueryPageDiagnostics queryPage = new QueryPageDiagnostics(
                    clientQueryCorrelationId: clientQueryCorrelationId,
                    partitionKeyRangeId: partitionKeyRangeIdentity.PartitionKeyRangeId,
                    queryMetricText: cosmosResponseMessage.Headers.QueryMetricsText,
                    indexUtilizationText: cosmosResponseMessage.Headers[HttpConstants.HttpHeaders.IndexUtilization],
                    diagnosticsContext: cosmosResponseMessage.DiagnosticsContext);
                queryPageDiagnostics(queryPage);

                if (!cosmosResponseMessage.IsSuccessStatusCode)
                {
                    return(QueryResponseCore.CreateFailure(
                               statusCode: cosmosResponseMessage.StatusCode,
                               subStatusCodes: cosmosResponseMessage.Headers.SubStatusCode,
                               cosmosException: cosmosResponseMessage.CosmosException,
                               requestCharge: cosmosResponseMessage.Headers.RequestCharge,
                               activityId: cosmosResponseMessage.Headers.ActivityId));
                }

                if (!(cosmosResponseMessage.Content is MemoryStream memoryStream))
                {
                    memoryStream = new MemoryStream();
                    cosmosResponseMessage.Content.CopyTo(memoryStream);
                }

                long        responseLengthBytes = memoryStream.Length;
                CosmosArray cosmosArray         = CosmosQueryClientCore.ParseElementsFromRestStream(
                    memoryStream,
                    resourceType,
                    requestOptions.CosmosSerializationFormatOptions);

                CosmosQueryExecutionInfo cosmosQueryExecutionInfo;
                if (cosmosResponseMessage.Headers.TryGetValue(QueryExecutionInfoHeader, out string queryExecutionInfoString))
                {
                    cosmosQueryExecutionInfo = JsonConvert.DeserializeObject <CosmosQueryExecutionInfo>(queryExecutionInfoString);
                }
                else
                {
                    cosmosQueryExecutionInfo = default;
                }

                return(QueryResponseCore.CreateSuccess(
                           result: cosmosArray,
                           requestCharge: cosmosResponseMessage.Headers.RequestCharge,
                           activityId: cosmosResponseMessage.Headers.ActivityId,
                           responseLengthBytes: responseLengthBytes,
                           disallowContinuationTokenMessage: null,
                           continuationToken: cosmosResponseMessage.Headers.ContinuationToken,
                           cosmosQueryExecutionInfo: cosmosQueryExecutionInfo));
            }
        }
        internal static ReadFeedResponse <TInput> CreateResponse <TInput>(
            ResponseMessage responseMessage,
            CosmosSerializerCore serializerCore,
            Documents.ResourceType resourceType)
        {
            using (responseMessage)
            {
                // ReadFeed can return 304 on some scenarios (Change Feed for example)
                if (responseMessage.StatusCode != HttpStatusCode.NotModified)
                {
                    responseMessage.EnsureSuccessStatusCode();
                }

                CosmosArray cosmosArray = null;
                if (responseMessage.Content != null)
                {
                    cosmosArray = CosmosElementSerializer.ToCosmosElements(
                        responseMessage.Content,
                        resourceType,
                        null);
                }

                ReadFeedResponse <TInput> readFeedResponse = new ReadFeedResponse <TInput>(
                    httpStatusCode: responseMessage.StatusCode,
                    cosmosArray: cosmosArray,
                    serializerCore: serializerCore,
                    responseMessageHeaders: responseMessage.Headers,
                    diagnostics: responseMessage.Diagnostics);

                return(readFeedResponse);
            }
        }
        public void Materialize()
        {
            CosmosArray            lazilyDeserializedPeople = CosmosElement.CreateFromBuffer <CosmosArray>(LazyCosmosElementTests.bufferedSerializedPeople);
            IReadOnlyList <Person> materialziedPeople       = lazilyDeserializedPeople.Materialize <IReadOnlyList <Person> >();

            Assert.IsTrue(people.SequenceEqual(materialziedPeople));
        }
        public static QueryResponseCore CreateQueryResponse(
            IList <ToDoItem> items,
            bool isOrderByQuery,
            string continuationToken,
            string collectionRid)
        {
            MemoryStream memoryStream;
            string       json;

            if (isOrderByQuery)
            {
                memoryStream = SerializeForOrderByQuery(items);
                using (StreamReader sr = new StreamReader(SerializeForOrderByQuery(items)))
                {
                    json = sr.ReadToEnd();
                }
            }
            else
            {
                memoryStream = (MemoryStream)MockCosmosUtil.Serializer.ToStream <IList <ToDoItem> >(items);
            }

            long responseLengthBytes = memoryStream.Length;

            IJsonNavigator           jsonNavigator      = JsonNavigator.Create(memoryStream.ToArray());
            IJsonNavigatorNode       jsonNavigatorNode  = jsonNavigator.GetRootNode();
            CosmosArray              cosmosArray        = CosmosArray.Create(jsonNavigator, jsonNavigatorNode);
            CosmosDiagnosticsContext diagnosticsContext = new CosmosDiagnosticsContextCore();

            diagnosticsContext.AddDiagnosticsInternal(new PointOperationStatistics(
                                                          activityId: Guid.NewGuid().ToString(),
                                                          statusCode: HttpStatusCode.OK,
                                                          subStatusCode: SubStatusCodes.Unknown,
                                                          responseTimeUtc: DateTime.UtcNow,
                                                          requestCharge: 4,
                                                          errorMessage: null,
                                                          method: HttpMethod.Post,
                                                          requestUri: "http://localhost.com",
                                                          requestSessionToken: null,
                                                          responseSessionToken: null));
            IReadOnlyCollection <QueryPageDiagnostics> diagnostics = new List <QueryPageDiagnostics>()
            {
                new QueryPageDiagnostics(
                    Guid.NewGuid(),
                    "0",
                    "SomeQueryMetricText",
                    "SomeIndexUtilText",
                    diagnosticsContext)
            };

            QueryResponseCore message = QueryResponseCore.CreateSuccess(
                result: cosmosArray,
                requestCharge: 4,
                activityId: Guid.NewGuid().ToString(),
                responseLengthBytes: responseLengthBytes,
                disallowContinuationTokenMessage: null,
                continuationToken: continuationToken);

            return(message);
        }
        internal static ReadFeedResponse <TInput> CreateResponse <TInput>(
            ResponseMessage responseMessage,
            CosmosSerializerCore serializerCore,
            Documents.ResourceType resourceType)
        {
            using (responseMessage)
            {
                responseMessage.EnsureSuccessStatusCode();

                CosmosArray cosmosArray = null;
                if (responseMessage.Content != null)
                {
                    cosmosArray = CosmosElementSerializer.ToCosmosElements(
                        responseMessage.Content,
                        resourceType,
                        null);
                }

                ReadFeedResponse <TInput> readFeedResponse = new ReadFeedResponse <TInput>(
                    httpStatusCode: responseMessage.StatusCode,
                    cosmosArray: cosmosArray,
                    serializerCore: serializerCore,
                    responseMessageHeaders: responseMessage.Headers,
                    diagnostics: responseMessage.Diagnostics);

                return(readFeedResponse);
            }
        }
示例#14
0
        private static async Task <int> DrainWithUntilNotModifiedWithContinuationTokens(
            IDocumentContainer documentContainer,
            CrossPartitionChangeFeedAsyncEnumerator enumerator)
        {
            List <CosmosElement> globalChanges = new List <CosmosElement>();

            while (true)
            {
                if (!await enumerator.MoveNextAsync())
                {
                    throw new InvalidOperationException();
                }

                Assert.IsTrue(enumerator.Current.Succeeded);

                if (!(enumerator.Current.Result is ChangeFeedSuccessPage changeFeedSuccessPage))
                {
                    break;
                }

                CosmosArray changes = GetChanges(changeFeedSuccessPage.Content);
                globalChanges.AddRange(changes);

                CosmosElement continuationToken = ((ChangeFeedStateContinuation)enumerator.Current.Result.State).ContinuationToken;

                enumerator = CrossPartitionChangeFeedAsyncEnumerator.MonadicCreate(
                    documentContainer,
                    new ChangeFeedRequestOptions(),
                    ChangeFeedStartFrom.ContinuationToken(continuationToken.ToString()),
                    cancellationToken: default).Result;
            }

            return(globalChanges.Count);
        }
示例#15
0
            public CosmosElement Visit(CosmosArray cosmosArray, CosmosElement indexer)
            {
                if (!(indexer is CosmosNumber indexerAsNumber))
                {
                    return(Undefined);
                }

                if (!indexerAsNumber.Value.IsInteger)
                {
                    throw new ArgumentException("Number index must be a non negative integer.");
                }

                long numberIndexValue = Number64.ToLong(indexerAsNumber.Value);

                if (numberIndexValue < 0)
                {
                    throw new ArgumentException("Number index must be a non negative integer.");
                }

                if (numberIndexValue >= cosmosArray.Count)
                {
                    return(Undefined);
                }

                return(cosmosArray[(int)numberIndexValue]);
            }
        private static async Task <int> DrainWithUntilNotModifiedWithContinuationTokens(
            IDocumentContainer documentContainer,
            CrossPartitionChangeFeedAsyncEnumerator enumerator)
        {
            List <CosmosElement> globalChanges = new List <CosmosElement>();

            while (true)
            {
                if (!await enumerator.MoveNextAsync())
                {
                    throw new InvalidOperationException();
                }

                Assert.IsTrue(enumerator.Current.Succeeded);

                if (!(enumerator.Current.Result.Page is ChangeFeedSuccessPage changeFeedSuccessPage))
                {
                    break;
                }

                CosmosArray changes = GetChanges(changeFeedSuccessPage.Content);
                globalChanges.AddRange(changes);

                enumerator = CrossPartitionChangeFeedAsyncEnumerator.Create(
                    documentContainer,
                    enumerator.Current.Result.State,
                    ChangeFeedPaginationOptions.Default,
                    cancellationToken: default);
            }

            return(globalChanges.Count);
        }
            private static Tuple <CosmosElement, string> ApplyPath(CosmosElement document, IEnumerable <Either <long, string> > tokens)
            {
                CosmosObject documentAsObject = (CosmosObject)document;

                if (!documentAsObject.TryGetValue <CosmosString>("_rid", out CosmosString ridToken))
                {
                    throw new ArgumentException("Document did not have a '_rid' field.");
                }

                string rid = ridToken.Value;

                // First token in optional
                Either <long, string> firstToken = tokens.First();

                firstToken.Match(
                    (long longToken) =>
                {
                    throw new InvalidOperationException();
                },
                    (string stringToken) =>
                {
                    if (documentAsObject.TryGetValue(stringToken, out CosmosElement value))
                    {
                        document = value;
                    }
                });

                foreach (Either <long, string> token in tokens.Skip(1))
                {
                    if (document == null)
                    {
                        break;
                    }

                    token.Match(
                        (long arrayIndex) =>
                    {
                        CosmosArray arraySubDocument = (CosmosArray)document;
                        if (arrayIndex >= arraySubDocument.Count)
                        {
                            document = null;
                        }
                        else
                        {
                            document = arraySubDocument[(int)arrayIndex];
                        }
                    },
                        (string propertyName) =>
                    {
                        CosmosObject objectSubDocument = (CosmosObject)document;
                        if (!objectSubDocument.TryGetValue(propertyName, out document))
                        {
                            document = null;
                        }
                    });
                }

                return(new Tuple <CosmosElement, string>(document, rid));
            }
        public void TestQuickNavigation()
        {
            CosmosArray lazilyDeserializedPeople = CosmosElement.CreateFromBuffer(LazyCosmosElementTests.bufferedSerializedPeople) as CosmosArray;
            LazilyDeserializedPerson lazilyDeserializedFirstPerson = new LazilyDeserializedPerson(lazilyDeserializedPeople[0] as CosmosObject);

            Assert.AreEqual(people.First().Name, lazilyDeserializedFirstPerson.Name);
            Assert.AreEqual(people.First().Age, lazilyDeserializedFirstPerson.Age);
        }
示例#19
0
        private T GetAndAssertArrayValue <T>(CosmosArray cosmosArray, int index)
            where T : CosmosElement
        {
            T boolValue1LazyValue = cosmosArray[index] as T;

            Assert.IsNotNull(boolValue1LazyValue, $"Array value at position {index} is not {typeof(T).Name}.");
            return(boolValue1LazyValue);
        }
 public static ChangeFeedPage CreatePageWithChanges(
     CosmosArray documents,
     double requestCharge,
     string activityId,
     ChangeFeedCrossFeedRangeState state,
     ImmutableDictionary <string, string> additionalHeaders)
 {
     return(new ChangeFeedPage(documents, notModified: false, requestCharge, activityId, state, additionalHeaders));
 }
 private static void VisitCosmosArrayEnumerable(CosmosArray cosmosArray, IJsonWriter jsonWriter)
 {
     jsonWriter.WriteArrayStart();
     foreach (CosmosElement arrayItem in cosmosArray)
     {
         LazyCosmosElementTests.VisitCosmosElementIndexer(arrayItem, jsonWriter);
     }
     jsonWriter.WriteArrayEnd();
 }
示例#22
0
        public async Task TestDistinct_CosmosElementContinuationTokenAsync()
        {
            async Task ImplemenationAsync(Container container, IReadOnlyList <CosmosObject> documents)
            {
                // Run the ordered distinct query through the continuation api, should result in the same set
                // since the previous hash is passed in the continuation token.
                foreach (string query in new string[]
                {
                    "SELECT {0} VALUE c.age FROM c ORDER BY c.age",
                    "SELECT {0} VALUE c.name FROM c ORDER BY c.name",
                    "SELECT {0} VALUE c.name from c",
                    "SELECT {0} VALUE c.age from c",
                    "SELECT {0} VALUE c.mixedTypeField from c",
                    "SELECT {0} TOP 2147483647 VALUE c.city from c",
                    "SELECT {0} VALUE c.age from c ORDER BY c.name",
                })
                {
                    string               queryWithoutDistinct         = string.Format(query, "");
                    MockDistinctMap      documentsSeen                = new MockDistinctMap();
                    List <CosmosElement> documentsFromWithoutDistinct = await QueryTestsBase.RunQueryCombinationsAsync(
                        container,
                        queryWithoutDistinct,
                        new QueryRequestOptions()
                    {
                        MaxConcurrency = 10,
                        MaxItemCount   = 100,
                    },
                        QueryDrainingMode.HoldState | QueryDrainingMode.CosmosElementContinuationToken);

                    documentsFromWithoutDistinct = documentsFromWithoutDistinct
                                                   .Where(document => documentsSeen.Add(document, out UInt128 hash))
                                                   .ToList();

                    foreach (int pageSize in new int[] { 1, 10, 100 })
                    {
                        string queryWithDistinct = string.Format(query, "DISTINCT");
                        List <CosmosElement> documentsFromWithDistinct = await QueryTestsBase.RunQueryCombinationsAsync(
                            container,
                            queryWithDistinct,
                            new QueryRequestOptions()
                        {
                            MaxConcurrency = 10,
                            MaxItemCount   = pageSize
                        },
                            QueryDrainingMode.HoldState | QueryDrainingMode.CosmosElementContinuationToken);

                        Assert.AreEqual(
                            expected: CosmosArray.Create(documentsFromWithDistinct),
                            actual: CosmosArray.Create(documentsFromWithoutDistinct),
                            message: $"Documents didn't match for {queryWithDistinct} on a Partitioned container");
                    }
                }
            }

            await this.TestQueryDistinctBaseAsync(ImplemenationAsync);
        }
 private static void VisitCosmosArrayIndexer(CosmosArray cosmosArray, IJsonWriter jsonWriter)
 {
     jsonWriter.WriteArrayStart();
     for (int i = 0; i < cosmosArray.Count; i++)
     {
         CosmosElement arrayItem = cosmosArray[i];
         LazyCosmosElementTests.VisitCosmosElementIndexer(arrayItem, jsonWriter);
     }
     jsonWriter.WriteArrayEnd();
 }
示例#24
0
        private QueryResponseCore GetCosmosElementResponse(
            QueryRequestOptions requestOptions,
            ResourceType resourceType,
            ResponseMessage cosmosResponseMessage,
            PartitionKeyRangeIdentity partitionKeyRangeIdentity,
            SchedulingStopwatch schedulingStopwatch)
        {
            using (cosmosResponseMessage)
            {
                QueryPageDiagnostics diagnostics = new QueryPageDiagnostics(
                    partitionKeyRangeId: partitionKeyRangeIdentity.PartitionKeyRangeId,
                    queryMetricText: cosmosResponseMessage.Headers.QueryMetricsText,
                    indexUtilizationText: cosmosResponseMessage.Headers[HttpConstants.HttpHeaders.IndexUtilization],
                    requestDiagnostics: cosmosResponseMessage.Diagnostics,
                    schedulingStopwatch: schedulingStopwatch);

                IReadOnlyCollection <QueryPageDiagnostics> pageDiagnostics = new List <QueryPageDiagnostics>()
                {
                    diagnostics
                };
                if (!cosmosResponseMessage.IsSuccessStatusCode)
                {
                    return(QueryResponseCore.CreateFailure(
                               statusCode: cosmosResponseMessage.StatusCode,
                               subStatusCodes: cosmosResponseMessage.Headers.SubStatusCode,
                               errorMessage: cosmosResponseMessage.ErrorMessage,
                               requestCharge: cosmosResponseMessage.Headers.RequestCharge,
                               activityId: cosmosResponseMessage.Headers.ActivityId,
                               diagnostics: pageDiagnostics));
                }

                MemoryStream memoryStream = cosmosResponseMessage.Content as MemoryStream;
                if (memoryStream == null)
                {
                    memoryStream = new MemoryStream();
                    cosmosResponseMessage.Content.CopyTo(memoryStream);
                }

                long        responseLengthBytes = memoryStream.Length;
                CosmosArray cosmosArray         = CosmosElementSerializer.ToCosmosElements(
                    memoryStream,
                    resourceType,
                    requestOptions.CosmosSerializationFormatOptions);

                int itemCount = cosmosArray.Count;
                return(QueryResponseCore.CreateSuccess(
                           result: cosmosArray,
                           requestCharge: cosmosResponseMessage.Headers.RequestCharge,
                           activityId: cosmosResponseMessage.Headers.ActivityId,
                           diagnostics: pageDiagnostics,
                           responseLengthBytes: cosmosResponseMessage.Headers.ContentLengthAsLong,
                           disallowContinuationTokenMessage: null,
                           continuationToken: cosmosResponseMessage.Headers.ContinuationToken));
            }
        }
        public static (QueryResponseCore queryResponse, IList <ToDoItem> items) Create(
            string itemIdPrefix,
            string continuationToken,
            string collectionRid,
            int itemCount = 50)
        {
            // Use -1 to represent a split response
            if (itemCount == QueryResponseMessageFactory.SPLIT)
            {
                return(CreateSplitResponse(collectionRid), new List <ToDoItem>().AsReadOnly());
            }

            IList <ToDoItem> items               = ToDoItem.CreateItems(itemCount, itemIdPrefix);
            MemoryStream     memoryStream        = (MemoryStream)MockCosmosUtil.Serializer.ToStream <IList <ToDoItem> >(items);
            long             responseLengthBytes = memoryStream.Length;

            IJsonNavigator     jsonNavigator     = JsonNavigator.Create(memoryStream.ToArray());
            IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
            CosmosArray        cosmosArray       = CosmosArray.Create(jsonNavigator, jsonNavigatorNode);

            double requestCharge = 42;
            string activityId    = Guid.NewGuid().ToString();
            CosmosDiagnosticsContext diagnosticsContext = new CosmosDiagnosticsContextCore();

            diagnosticsContext.AddDiagnosticsInternal(new PointOperationStatistics(
                                                          activityId: Guid.NewGuid().ToString(),
                                                          statusCode: HttpStatusCode.OK,
                                                          subStatusCode: SubStatusCodes.Unknown,
                                                          responseTimeUtc: DateTime.UtcNow,
                                                          requestCharge: requestCharge,
                                                          errorMessage: null,
                                                          method: HttpMethod.Post,
                                                          requestUri: "http://localhost.com",
                                                          requestSessionToken: null,
                                                          responseSessionToken: null));
            IReadOnlyCollection <QueryPageDiagnostics> diagnostics = new List <QueryPageDiagnostics>()
            {
                new QueryPageDiagnostics(
                    Guid.NewGuid(),
                    "0",
                    "SomeQueryMetricText",
                    "SomeIndexUtilText",
                    diagnosticsContext)
            };

            QueryResponseCore message = QueryResponseCore.CreateSuccess(
                result: cosmosArray,
                continuationToken: continuationToken,
                disallowContinuationTokenMessage: null,
                activityId: activityId,
                requestCharge: requestCharge,
                responseLengthBytes: responseLengthBytes);

            return(message, items);
        }
 public ReadFeedPage(
     CosmosArray documents,
     double requestCharge,
     string activityId,
     ReadFeedCrossFeedRangeState?state)
 {
     this.Documents     = documents ?? throw new ArgumentNullException(nameof(documents));
     this.RequestCharge = requestCharge < 0 ? throw new ArgumentOutOfRangeException(nameof(requestCharge)) : requestCharge;
     this.ActivityId    = activityId ?? throw new ArgumentNullException(nameof(activityId));
     this.State         = state;
 }
示例#27
0
        public async ValueTask <bool> MoveNextAsync()
        {
            this.cancellationToken.ThrowIfCancellationRequested();

            if (!await this.crossPartitionEnumerator.MoveNextAsync())
            {
                this.Current = default;
                return(false);
            }

            TryCatch <CrossPartitionPage <ReadFeedPage, ReadFeedState> > monadicCrossPartitionPage = this.crossPartitionEnumerator.Current;

            if (monadicCrossPartitionPage.Failed)
            {
                this.Current = TryCatch <ReadFeedPage> .FromException(monadicCrossPartitionPage.Exception);

                return(true);
            }

            CrossPartitionPage <ReadFeedPage, ReadFeedState> crossPartitionPage = monadicCrossPartitionPage.Result;
            ReadFeedPage backendPage = crossPartitionPage.Page;
            CrossPartitionState <ReadFeedState> crossPartitionState = crossPartitionPage.State;
            ReadFeedState state;

            if (crossPartitionState != null)
            {
                IReadOnlyList <(FeedRangeInternal, ReadFeedState)> rangesAndStates = crossPartitionState.Value;
                List <CosmosElement> changeFeedContinuationTokens = new List <CosmosElement>();
                foreach ((FeedRangeInternal range, ReadFeedState readFeedState) in rangesAndStates)
                {
                    this.cancellationToken.ThrowIfCancellationRequested();
                    ReadFeedContinuationToken readFeedContinuationToken = new ReadFeedContinuationToken(
                        range,
                        readFeedState);

                    CosmosElement cosmosElementChangeFeedContinuationToken = ReadFeedContinuationToken.ToCosmosElement(readFeedContinuationToken);

                    changeFeedContinuationTokens.Add(cosmosElementChangeFeedContinuationToken);
                }

                CosmosArray cosmosElementTokens = CosmosArray.Create(changeFeedContinuationTokens);
                state = new ReadFeedState(cosmosElementTokens);
            }
            else
            {
                state = null;
            }

            ReadFeedPage compositePage = new ReadFeedPage(backendPage.Content, backendPage.RequestCharge, backendPage.ActivityId, backendPage.Diagnostics, state);

            this.Current = TryCatch <ReadFeedPage> .FromResult(compositePage);

            return(true);
        }
        public CosmosElement ToCosmosElement()
        {
            List <CosmosElement> elements = new List <CosmosElement>();

            foreach (FeedRangeState <ReadFeedState> readFeedFeedRangeState in this.FeedRangeStates.Span)
            {
                elements.Add(ReadFeedFeedRangeStateSerializer.ToCosmosElement(readFeedFeedRangeState));
            }

            return(CosmosArray.Create(elements));
        }
示例#29
0
        public bool Visit(CosmosArray cosmosArray)
        {
            foreach (CosmosElement item in cosmosArray)
            {
                if (item.Accept(this))
                {
                    return(true);
                }
            }

            return(false);
        }
        internal IEnumerable <T> FromFeedResponseStream <T>(
            Stream stream,
            ResourceType resourceType)
        {
            CosmosArray cosmosArray = CosmosElementSerializer.ToCosmosElements(
                stream,
                resourceType);

            return(CosmosElementSerializer.GetResources <T>(
                       cosmosArray: cosmosArray,
                       serializerCore: this));
        }