From 867c8c86e4d90ea1ad87b347694d1fec192e5d14 Mon Sep 17 00:00:00 2001 From: Paul Cheng Date: Tue, 15 Jun 2021 14:35:52 -0700 Subject: [PATCH] [INTERNAL] IncludedPath: Adds IsFullIndex for compute (#2555) * add internal IsFullIndex to IncludedPath class * add included path serialization unit test * remote INTERNAL macro Co-authored-by: j82w --- .../src/Resource/Settings/IncludedPath.cs | 12 ++--- .../CosmosContainerSettingsTests.cs | 45 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Resource/Settings/IncludedPath.cs b/Microsoft.Azure.Cosmos/src/Resource/Settings/IncludedPath.cs index 38b0c70335..ccf71c87f7 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Settings/IncludedPath.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Settings/IncludedPath.cs @@ -11,11 +11,7 @@ namespace Microsoft.Azure.Cosmos /// /// Specifies a path within a JSON document to be included in the Azure Cosmos DB service. /// - public -#if !INTERNAL - sealed -#endif - class IncludedPath + public sealed class IncludedPath { /// /// Gets or sets the path to be indexed in the Azure Cosmos DB service. @@ -38,5 +34,11 @@ class IncludedPath /// [JsonProperty(PropertyName = Constants.Properties.Indexes)] internal Collection Indexes { get; set; } = new Collection(); + + /// + /// Gets or sets whether this is a full index used for collection types. + /// + [JsonProperty(PropertyName = Constants.Properties.IsFullIndex, NullValueHandling = NullValueHandling.Ignore)] + internal bool? IsFullIndex { get; set; } } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs index 1ea21d2123..467e2aeafe 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs @@ -8,6 +8,7 @@ namespace Microsoft.Azure.Cosmos.Tests using System.IO; using System.Linq; using System.Text; + using System.Text.RegularExpressions; using Microsoft.Azure.Documents; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; @@ -120,6 +121,50 @@ public void DefaultIndexingPolicySameAsDocumentCollection() CosmosContainerSettingsTests.AssertSerializedPayloads(containerSettings, dc); } + [TestMethod] + public void ValidateIncludedPathSerialization() + { + ContainerProperties containerSettings = new ContainerProperties("TestContainer", "/partitionKey") + { + IndexingPolicy = new Cosmos.IndexingPolicy() + }; + + containerSettings.IndexingPolicy.IncludedPaths.Add(new Cosmos.IncludedPath() + { + Path = "/textprop/?", + }); + containerSettings.IndexingPolicy.IncludedPaths.Add(new Cosmos.IncludedPath() + { + Path = "/listprop/?", + IsFullIndex = true, + }); + + using (Stream stream = MockCosmosUtil.Serializer.ToStream(containerSettings)) + { + StreamReader reader = new StreamReader(stream); + string content = reader.ReadToEnd(); + + Match match = Regex.Match(content, "\"includedPaths\":\\[(.+?)\\],\"excludedPaths\""); + Assert.IsTrue(match.Success, "IncludedPaths not found in serialized content"); + + // verify IncludedPath ignores null IsFullIndex + string includedPaths = match.Groups[1].Value; + string delimiter = "},{"; + int position = includedPaths.IndexOf(delimiter); + string textPropIncludedPath = includedPaths.Substring(0, position + 1); + string listPropIncludedPath = includedPaths.Substring(position + delimiter.Length - 1); + + Assert.AreEqual("{\"path\":\"/textprop/?\",\"indexes\":[]}", textPropIncludedPath); + Assert.AreEqual("{\"path\":\"/listprop/?\",\"indexes\":[],\"isFullIndex\":true}", listPropIncludedPath); + + // verify deserialization + stream.Position = 0; + containerSettings = MockCosmosUtil.Serializer.FromStream(stream); + Assert.IsNull(containerSettings.IndexingPolicy.IncludedPaths[0].IsFullIndex, "textprop IsFullIndex is not null"); + Assert.IsTrue((bool)containerSettings.IndexingPolicy.IncludedPaths[1].IsFullIndex, "listprop IsFullIndex is not set to true"); + } + } + private static string SerializeDocumentCollection(DocumentCollection collection) { using (MemoryStream ms = new MemoryStream())