Ejemplo n.º 1
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);
        }
        public void LargeArrayBinaryJsonTest()
        {
            StringBuilder builder = new StringBuilder((1 << 24) + 50);

            builder.Append('[');
            for (int x = 1 << 24; x < (1 << 24) + 3355450; ++x)
            {
                builder.Append(x);
                builder.Append(',');
            }
            builder.Append("\"string_one\"");
            builder.Append(',');
            builder.Append("\"string_two\"");
            builder.Append(',');
            builder.Append("\"string_two\"");
            builder.Append(']');

            string json = builder.ToString();

            byte[]             binaryJson  = JsonTestUtils.ConvertTextToBinary(json);
            IJsonNavigator     navigator   = JsonNavigator.Create(binaryJson);
            int                count       = navigator.GetArrayItemCount(navigator.GetRootNode());
            IJsonNavigatorNode node        = navigator.GetArrayItemAt(navigator.GetRootNode(), count - 1);
            string             stringValue = navigator.GetStringValue(node);

            Assert.AreEqual("string_two", stringValue);
        }
        public static CosmosElement CreateFromBuffer(ReadOnlyMemory <byte> buffer)
        {
            IJsonNavigator     jsonNavigator     = JsonNavigator.Create(buffer);
            IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();

            return(CosmosElement.Dispatch(jsonNavigator, jsonNavigatorNode));
        }
        private async Task RunQueryAsync(Container container)
        {
            QueryRequestOptions requestOptions = new QueryRequestOptions()
            {
                MaxConcurrency = this.MaxConcurrency,
                MaxItemCount   = this.MaxItemCount,
                CosmosSerializationFormatOptions = new CosmosSerializationFormatOptions(
                    contentSerializationFormat: this.contentSerialization,
                    createCustomNavigator: (content) => JsonNavigator.Create(content),
                    createCustomWriter: () => JsonWriter.Create(this.contentSerialization == "JsonText" ? JsonSerializationFormat.Text : JsonSerializationFormat.Binary))
            };

            if (this.useStronglyTypedIterator)
            {
                using (FeedIterator <States> iterator = container.GetItemQueryIterator <States>(
                           queryText: this.query,
                           requestOptions: requestOptions))
                {
                    await this.GetIteratorResponse(iterator);
                }
            }
            else
            {
                using (FeedIterator <dynamic> distinctQueryIterator = container.GetItemQueryIterator <dynamic>(
                           queryText: this.query,
                           requestOptions: requestOptions))
                {
                    await this.GetIteratorResponse(distinctQueryIterator);
                }
            }
        }
Ejemplo n.º 5
0
            public static TryCatch <TCosmosElement> CreateFromBuffer <TCosmosElement>(ReadOnlyMemory <byte> buffer)
                where TCosmosElement : CosmosElement
            {
                if (buffer.IsEmpty)
                {
                    TryCatch <TCosmosElement> .FromException(
                        new ArgumentException($"{nameof(buffer)} must not be empty."));
                }

                CosmosElement unTypedCosmosElement;

                try
                {
                    IJsonNavigator     jsonNavigator     = JsonNavigator.Create(buffer);
                    IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
                    unTypedCosmosElement = CosmosElement.Dispatch(jsonNavigator, jsonNavigatorNode);
                }
                catch (JsonParseException jpe)
                {
                    return(TryCatch <TCosmosElement> .FromException(jpe));
                }

                if (!(unTypedCosmosElement is TCosmosElement typedCosmosElement))
                {
                    return(TryCatch <TCosmosElement> .FromException(
                               new CosmosElementWrongTypeException(
                                   message : $"buffer was incorrect cosmos element type: {unTypedCosmosElement.GetType()} when {typeof(TCosmosElement)} was requested.")));
                }

                return(TryCatch <TCosmosElement> .FromResult(typedCosmosElement));
            }
Ejemplo n.º 6
0
        public INavigator CreateNavigator(object data, Type pathType)
        {
            if (!pathType.GetInterfaces().Contains(typeof(IPath)))
            {
                throw new Exception("'" + pathType + "' doesn't implement '" + typeof(IPath) + "'");
            }

            INavigator navigator;

            if (pathType == typeof(XmlPath))
            {
                navigator = new XmlNavigator(data);
            }
            else if (pathType == typeof(JsonPath))
            {
                navigator = new JsonNavigator(data);
            }
            else if (pathType == typeof(PocoPath))
            {
                navigator = new PocoNavigator(data);
            }
            else
            {
                navigator = null;
            }

            return(navigator);
        }
        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);
        }
Ejemplo n.º 8
0
        public static CosmosElement Create(byte[] buffer)
        {
            IJsonNavigator     jsonNavigator     = JsonNavigator.Create(buffer);
            IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();

            return(CosmosElement.Dispatch(jsonNavigator, jsonNavigatorNode));
        }
        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);
        }
Ejemplo n.º 10
0
        private static IJsonNavigator CreateNavigatorFromContent(ReadOnlyMemory <byte> content, JsonSerializationFormatOptions jsonSerializationFormatOptions)
        {
            IJsonNavigator jsonNavigator;

            if (jsonSerializationFormatOptions != null)
            {
                if (jsonSerializationFormatOptions is JsonSerializationFormatOptions.CustomJsonSerializationFormatOptions customOptions)
                {
                    jsonNavigator = customOptions.createNavigator(content);
                    if (jsonNavigator == null)
                    {
                        throw new InvalidOperationException("The CosmosSerializationOptions did not return a JSON navigator.");
                    }
                }
                else if (jsonSerializationFormatOptions is JsonSerializationFormatOptions.NativelySupportedJsonSerializationFormatOptions)
                {
                    jsonNavigator = JsonNavigator.Create(content);
                }
                else
                {
                    throw new ArgumentOutOfRangeException($"Unknown {nameof(JsonSerializationFormatOptions)} type: {jsonSerializationFormatOptions.GetType()}");
                }
            }
            else
            {
                jsonNavigator = JsonNavigator.Create(content);
            }

            return(jsonNavigator);
        }
Ejemplo n.º 11
0
        public void SelectEnumerable_WithEnumerableSymbolAndSeperatorSymbol_Expected_PipeDelimited()
        {
            IPath namePath = new JsonPath("().", "().");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = string.Join("|", JsonNavigator.SelectEnumerable(namePath).Select(o => o.ToString().Trim()));
            const string expected = @"""Name"": ""Dev2""|""Motto"": ""Eat lots of cake""|""Departments"": [
  {
    ""Name"": ""Dev"",
    ""Employees"": [
      {
        ""Name"": ""Brendon"",
        ""Surename"": ""Page""
      },
      {
        ""Name"": ""Jayd"",
        ""Surename"": ""Page""
      }
    ]
  },
  {
    ""Name"": ""Accounts"",
    ""Employees"": [
      {
        ""Name"": ""Bob"",
        ""Surename"": ""Soap""
      },
      {
        ""Name"": ""Joe"",
        ""Surename"": ""Pants""
      }
    ]
  }
]|""Contractors"": [
  {
    ""Name"": ""Roofs Inc."",
    ""PhoneNumber"": ""123""
  },
  {
    ""Name"": ""Glass Inc."",
    ""PhoneNumber"": ""1234""
  },
  {
    ""Name"": ""Doors Inc."",
    ""PhoneNumber"": ""1235""
  },
  {
    ""Name"": ""Cakes Inc."",
    ""PhoneNumber"": ""1236""
  }
]|""PrimitiveRecordset"": [
  ""\r\n        RandomData\r\n    "",
  ""\r\n        RandomData1\r\n    ""
]";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 12
0
        private static void VerifyNavigator(
            string input,
            bool performExtraChecks = true)
        {
            CultureInfo defaultCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;

            CultureInfo[] cultureInfoList = new CultureInfo[]
            {
                defaultCultureInfo,
                System.Globalization.CultureInfo.GetCultureInfo("fr-FR")
            };

            try
            {
                foreach (CultureInfo cultureInfo in cultureInfoList)
                {
                    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;

                    IJsonReader jsonReader       = JsonReader.Create(Encoding.UTF8.GetBytes(input));
                    JsonToken[] tokensFromReader = JsonNavigatorTests.GetTokensWithReader(jsonReader);

                    // Test text
                    IJsonNavigator     textNavigator           = JsonNavigator.Create(Encoding.UTF8.GetBytes(input));
                    IJsonNavigatorNode textRootNode            = textNavigator.GetRootNode();
                    JsonToken[]        tokensFromTextNavigator = JsonNavigatorTests.GetTokensFromNode(textRootNode, textNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromTextNavigator.SequenceEqual(tokensFromReader));

                    // Test binary
                    byte[]             binaryInput               = JsonTestUtils.ConvertTextToBinary(input);
                    IJsonNavigator     binaryNavigator           = JsonNavigator.Create(binaryInput);
                    IJsonNavigatorNode binaryRootNode            = binaryNavigator.GetRootNode();
                    JsonToken[]        tokensFromBinaryNavigator = JsonNavigatorTests.GetTokensFromNode(binaryRootNode, binaryNavigator, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigator.SequenceEqual(tokensFromReader));

                    // Test binary + user string encoding
                    JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(capacity: 4096);
                    byte[] binaryWithUserStringEncodingInput  = JsonTestUtils.ConvertTextToBinary(input, jsonStringDictionary);
                    if (jsonStringDictionary.TryGetStringAtIndex(index: 0, value: out _))
                    {
                        Assert.IsFalse(binaryWithUserStringEncodingInput.SequenceEqual(binaryInput), "Binary should be different with user string encoding");
                    }

                    IJsonNavigator     binaryNavigatorWithUserStringEncoding           = JsonNavigator.Create(binaryInput, jsonStringDictionary);
                    IJsonNavigatorNode binaryRootNodeWithUserStringEncoding            = binaryNavigatorWithUserStringEncoding.GetRootNode();
                    JsonToken[]        tokensFromBinaryNavigatorWithUserStringEncoding = JsonNavigatorTests.GetTokensFromNode(binaryRootNodeWithUserStringEncoding, binaryNavigatorWithUserStringEncoding, performExtraChecks);

                    Assert.IsTrue(tokensFromBinaryNavigatorWithUserStringEncoding.SequenceEqual(tokensFromReader));
                }
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = defaultCultureInfo;
            }
        }
        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);
        }
Ejemplo n.º 14
0
        public void SelectEnumerableValuesUsingEnumerablePathFromJson_WherePathMapsThroughNestedEnumerables_Expected_EnumerableValue()
        {
            IPath path = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim()));
            const string expected = "Brendon|Jayd|Bob|Joe";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 15
0
        public void SelectEnumerableValuesUsingScalarPathFromJson_Expected_EnumerableValue()
        {
            IPath path = new JsonPath("Motto", "Motto");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim()));
            const string expected = "Eat lots of cake";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 16
0
        public void SelectEnumerableValuesUsingEnumerablePathFromJson_Expected_EnumerableValue()
        {
            IPath path = new JsonPath("Departments().Name", "Departments.Name");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim()));
            const string expected = "Dev|Accounts";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 17
0
        public void SelectScalarValue_WithEnumerablePathFromJson_WherePathMapsToPrimitiveEnumerable_Expected_ScalarValue()
        {
            IPath namePath = new JsonPath("PrimitiveRecordset()", "PrimitiveRecordset");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = JsonNavigator.SelectScalar(namePath).ToString().Trim();
            const string expected = "RandomData1";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 18
0
        public void SelectScalarValue_WithEnumerablePathFromJson_Expected_ScalarValue()
        {
            IPath namePath = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = JsonNavigator.SelectScalar(namePath).ToString();
            const string expected = "Joe";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 19
0
        public void SelectScalarValue_WithScalarPathFromJson_Expected_ScalarValue()
        {
            IPath namePath = new JsonPath("Name", "Name");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = JsonNavigator.SelectScalar(namePath).ToString();
            const string expected = "Dev2";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 20
0
        public void SelectEnumerableValuesUsingEnumerablePathFromJson_WherePathMapsToPrimitiveEnumerable_Expected_EnumerableValue()
        {
            IPath path = new JsonPath("PrimitiveRecordset()", "PrimitiveRecordset");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = string.Join("|", JsonNavigator.SelectEnumerable(path).Select(o => o.ToString().Trim()));
            const string expected = "RandomData|RandomData1";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 21
0
        public void SelectScalarValueUsingScalarPathFromJson_Expected_ScalarValue()
        {
            string testData = Given();

            IPath namePath = new JsonPath("Name", "Name");

            JsonNavigator JsonNavigator = new JsonNavigator(testData);

            string actual   = JsonNavigator.SelectScalar(namePath).ToString();
            string expected = "Dev2";

            Assert.AreEqual(expected, actual);
        }
        private static void ExecuteNavigateMicroBenchmark(
            BenchmarkPayload payload,
            BenchmarkSerializationFormat benchmarkSerializationFormat)
        {
            IJsonNavigator jsonNavigator = benchmarkSerializationFormat switch
            {
                BenchmarkSerializationFormat.Text => JsonNavigator.Create(payload.Text),
                BenchmarkSerializationFormat.Binary => JsonNavigator.Create(payload.Binary),
                _ => throw new ArgumentOutOfRangeException($"Unknown {nameof(BenchmarkSerializationFormat)}: '{benchmarkSerializationFormat}'."),
            };

            Utils.DrainNavigator(jsonNavigator);
        }
Ejemplo n.º 23
0
        public void SelectScalarValueUsingEnumerablePathFromJson_Expected_ScalarValue()
        {
            string testData = Given();

            IPath namePath = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name");

            JsonNavigator JsonNavigator = new JsonNavigator(testData);

            string actual   = JsonNavigator.SelectScalar(namePath).ToString();
            string expected = "Joe";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 24
0
        public void SelectScalarValue_WithEnumerableSymbolAndSeperatorSymbol_Expected_PrimitiveRecordset()
        {
            IPath namePath = new JsonPath("().", "().");

            var JsonNavigator = new JsonNavigator(testData);

            var          actual   = JsonNavigator.SelectScalar(namePath).ToString();
            const string expected = @"""PrimitiveRecordset"": [
  ""\r\n        RandomData\r\n    "",
  ""\r\n        RandomData1\r\n    ""
]";

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 25
0
        internal async Task <PartitionKey> GetPartitionKeyValueFromStreamAsync(
            Stream stream,
            CancellationToken cancellation = default(CancellationToken))
        {
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream is needs to be seekable", nameof(stream));
            }

            try
            {
                stream.Position = 0;

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

                // TODO: Avoid copy
                IJsonNavigator     jsonNavigator     = JsonNavigator.Create(memoryStream.ToArray());
                IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
                CosmosObject       pathTraversal     = CosmosObject.Create(jsonNavigator, jsonNavigatorNode);

                string[] tokens = await this.GetPartitionKeyPathTokensAsync(cancellation);

                for (int i = 0; i < tokens.Length - 1; i++)
                {
                    pathTraversal = pathTraversal[tokens[i]] as CosmosObject;
                    if (pathTraversal == null)
                    {
                        return(PartitionKey.None);
                    }
                }

                CosmosElement partitionKeyValue = pathTraversal[tokens[tokens.Length - 1]];
                if (partitionKeyValue == null)
                {
                    return(PartitionKey.None);
                }

                return(this.CosmosElementToPartitionKeyObject(partitionKeyValue));
            }
            finally
            {
                // MemoryStream casting leverage might change position
                stream.Position = 0;
            }
        }
        public override async Task <PartitionKey> GetPartitionKeyValueFromStreamAsync(
            Stream stream,
            CancellationToken cancellation = default(CancellationToken))
        {
            if (!stream.CanSeek)
            {
                throw new ArgumentException("Stream needs to be seekable", nameof(stream));
            }

            try
            {
                stream.Position = 0;

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

                // TODO: Avoid copy
                IJsonNavigator     jsonNavigator     = JsonNavigator.Create(memoryStream.ToArray());
                IJsonNavigatorNode jsonNavigatorNode = jsonNavigator.GetRootNode();
                CosmosObject       pathTraversal     = CosmosObject.Create(jsonNavigator, jsonNavigatorNode);

                IReadOnlyList <IReadOnlyList <string> > tokenslist = await this.GetPartitionKeyPathTokensAsync(cancellation);

                List <CosmosElement> cosmosElementList = new List <CosmosElement>(tokenslist.Count);

                foreach (IReadOnlyList <string> tokenList in tokenslist)
                {
                    CosmosElement element;
                    if (ContainerCore.TryParseTokenListForElement(pathTraversal, tokenList, out element))
                    {
                        cosmosElementList.Add(element);
                    }
                    else
                    {
                        cosmosElementList.Add(null);
                    }
                }

                return(ContainerCore.CosmosElementToPartitionKeyObject(cosmosElementList));
            }
            finally
            {
                // MemoryStream casting leverage might change position
                stream.Position = 0;
            }
        }
Ejemplo n.º 27
0
        public async Task ItemQueryStreamSerializationSetting()
        {
            IList <ToDoActivity> deleteList = await this.CreateRandomItems(101, randomPartitionKey : true);

            CosmosSqlQueryDefinition   sql     = new CosmosSqlQueryDefinition("SELECT * FROM toDoActivity t ORDER BY t.taskNum");
            CosmosSerializationOptions options = new CosmosSerializationOptions(
                ContentSerializationFormat.CosmosBinary.ToString(),
                (content) => JsonNavigator.Create(content),
                () => JsonWriter.Create(JsonSerializationFormat.Binary));

            CosmosQueryRequestOptions requestOptions = new CosmosQueryRequestOptions()
            {
                CosmosSerializationOptions = options
            };

            List <ToDoActivity> resultList      = new List <ToDoActivity>();
            double totalRequstCharge            = 0;
            CosmosResultSetIterator setIterator =
                this.Container.Items.CreateItemQueryAsStream(sql, maxConcurrency: 5, maxItemCount: 5, requestOptions: requestOptions);

            while (setIterator.HasMoreResults)
            {
                using (CosmosQueryResponse iter = await setIterator.FetchNextSetAsync())
                {
                    Assert.IsTrue(iter.IsSuccess);
                    Assert.IsNull(iter.ErrorMessage);
                    Assert.IsTrue(iter.Count <= 5);
                    totalRequstCharge += iter.RequestCharge;
                    IJsonReader reader     = JsonReader.Create(iter.Content);
                    IJsonWriter textWriter = JsonWriter.Create(JsonSerializationFormat.Text);
                    textWriter.WriteAll(reader);
                    string json = Encoding.UTF8.GetString(textWriter.GetResult());
                    Assert.IsNotNull(json);
                    ToDoActivity[] responseActivities = JsonConvert.DeserializeObject <ToDoActivity[]>(json);
                    resultList.AddRange(responseActivities);
                }
            }

            Assert.AreEqual(deleteList.Count, resultList.Count);
            Assert.IsTrue(totalRequstCharge > 0);

            List <ToDoActivity> verifiedOrderBy = deleteList.OrderBy(x => x.taskNum).ToList();

            for (int i = 0; i < verifiedOrderBy.Count(); i++)
            {
                Assert.AreEqual(verifiedOrderBy[i].taskNum, resultList[i].taskNum);
                Assert.AreEqual(verifiedOrderBy[i].id, resultList[i].id);
            }
        }
Ejemplo n.º 28
0
        public async Task DecryptQueryBinaryResponse()
        {
            TestDoc testDoc = await EncryptionTests.CreateItemAsync(EncryptionTests.containerCore, EncryptionTests.dekId, TestDoc.PathsToEncrypt);

            CosmosSerializationFormatOptions options = new CosmosSerializationFormatOptions(
                Documents.ContentSerializationFormat.CosmosBinary.ToString(),
                (content) => JsonNavigator.Create(content),
                () => JsonWriter.Create(JsonSerializationFormat.Binary));

            QueryRequestOptions requestOptions = new QueryRequestOptions()
            {
                CosmosSerializationFormatOptions = options
            };

            TestDoc expectedDoc = new TestDoc(testDoc);

            string query = "SELECT * FROM c";

            FeedIterator feedIterator = EncryptionTests.containerCore.GetItemQueryStreamIterator(
                query,
                requestOptions: requestOptions);

            while (feedIterator.HasMoreResults)
            {
                ResponseMessage response = await feedIterator.ReadNextAsync();

                Assert.IsTrue(response.IsSuccessStatusCode);
                Assert.IsNull(response.ErrorMessage);

                // Copy the stream and check that the first byte is the correct value
                MemoryStream memoryStream = new MemoryStream();
                response.Content.CopyTo(memoryStream);
                byte[] content = memoryStream.ToArray();
                response.Content.Position = 0;

                // Examine the first buffer byte to determine the serialization format
                byte firstByte = content[0];
                Assert.AreEqual(128, firstByte);
                Assert.AreEqual(JsonSerializationFormat.Binary, (JsonSerializationFormat)firstByte);

                IJsonReader reader     = JsonReader.Create(content);
                IJsonWriter textWriter = JsonWriter.Create(JsonSerializationFormat.Text);
                textWriter.WriteAll(reader);
                string json = Encoding.UTF8.GetString(textWriter.GetResult().ToArray());
                Assert.IsNotNull(json);
                Assert.IsTrue(json.Contains(testDoc.Sensitive));
            }
        }
Ejemplo n.º 29
0
        public void SelectEnumerableValuesAsRelatedUsingEnumerablePathFromJson_Where_PathsContainASinglePathWhichIsScalar_Expected_FlattenedDataWithValueFromScalarPath()
        {
            IPath path  = new JsonPath("Name", "Name");
            var   paths = new List <IPath> {
                path
            };

            var JsonNavigator = new JsonNavigator(testData);

            var data = JsonNavigator.SelectEnumerablesAsRelated(paths);

            const string expected = "Dev2";
            var          actual   = string.Join("|", data[path].Select(s => s.ToString().Trim()));

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 30
0
        public void SelectEnumerableValuesAsRelatedUsingEnumerablePathFromJson_Where_PathsContainASinglePathWhichIsEnumerable_Expected_FlattenedDataWithValuesFromEnumerablePath()
        {
            IPath path  = new JsonPath("Departments().Employees().Name", "Departments.Employees.Name");
            var   paths = new List <IPath> {
                path
            };

            var JsonNavigator = new JsonNavigator(testData);

            var data = JsonNavigator.SelectEnumerablesAsRelated(paths);

            const string expected = "Brendon|Jayd|Bob|Joe";
            var          actual   = string.Join("|", data[path].Select(s => s.ToString().Trim()));

            Assert.AreEqual(expected, actual);
        }