// methods
        public BsonDocument CreateIndexDocument()
        {
            var additionalOptionsName = _additionalOptions == null ? null : _additionalOptions.GetValue("name", null);
            var name     = _name ?? additionalOptionsName ?? IndexNameHelper.GetIndexName(_keys);
            var document = new BsonDocument
            {
                { "key", _keys },
                { "name", name },
                { "background", () => _background.Value, _background.HasValue },
                { "bits", () => _bits.Value, _bits.HasValue },
                { "bucketSize", () => _bucketSize.Value, _bucketSize.HasValue },
                { "default_language", () => _defaultLanguage, _defaultLanguage != null },
                { "expireAfterSeconds", () => _expireAfter.Value.TotalSeconds, _expireAfter.HasValue },
                { "language_override", () => _languageOverride, _languageOverride != null },
                { "max", () => _max.Value, _max.HasValue },
                { "min", () => _min.Value, _min.HasValue },
                { "sparse", () => _sparse.Value, _sparse.HasValue },
                { "2dsphereIndexVersion", () => _sphereIndexVersion.Value, _sphereIndexVersion.HasValue },
                { "storageOptions", () => _storageOptions, _storageOptions != null },
                { "textIndexVersion", () => _textIndexVersion.Value, _textIndexVersion.HasValue },
                { "unique", () => _unique.Value, _unique.HasValue },
                { "v", () => _version.Value, _version.HasValue },
                { "weights", () => _weights, _weights != null }
            };

            if (_additionalOptions != null)
            {
                document.Merge(_additionalOptions, overwriteExistingElements: false);
            }
            return(document);
        }
Example #2
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="DropIndexOperation"/> class.
 /// </summary>
 /// <param name="collectionNamespace">The collection namespace.</param>
 /// <param name="keys">The keys.</param>
 /// <param name="messageEncoderSettings">The message encoder settings.</param>
 public DropIndexOperation(
     CollectionNamespace collectionNamespace,
     BsonDocument keys,
     MessageEncoderSettings messageEncoderSettings)
     : this(collectionNamespace, IndexNameHelper.GetIndexName(keys), messageEncoderSettings)
 {
 }
        public void GetIndexName_with_names_should_return_expected_result()
        {
            var keys           = new[] { "a", "b", "c c" };
            var expectedResult = "a_1_b_1_c_c_1";

            var result = IndexNameHelper.GetIndexName(keys);

            result.Should().Be(expectedResult);
        }
Example #4
0
        public void GetIndexName_with_wildcard_keys_should_return_expected_result(
            [Values("$**", "a.$**")] string key)
        {
            var keys           = new[] { key };
            var expectedResult = key + "_1";

            var result = IndexNameHelper.GetIndexName(keys);

            result.Should().Be(expectedResult);
        }
Example #5
0
        // publuc methods
        /// <summary>
        /// Gets the name of the index.
        /// </summary>
        /// <returns>The name of the index.</returns>
        public string GetIndexName()
        {
            if (_name != null)
            {
                return(_name);
            }

            if (_additionalOptions != null)
            {
                BsonValue name;
                if (_additionalOptions.TryGetValue("name", out name))
                {
                    return(name.AsString);
                }
            }

            return(IndexNameHelper.GetIndexName(_keys));
        }
        public void GetIndexName_with_BsonDocument_should_return_expected_result()
        {
            var keys = new BsonDocument
            {
                { "a", new BsonDouble(1.0) },
                { "b", new BsonInt32(1) },
                { "c", new BsonInt64(1) },
                { "d", new BsonDouble(-1.0) },
                { "e", new BsonInt32(-1) },
                { "f", new BsonInt64(-1) },
                { "g g", "s s" },
                { "h", false }
            };
            var expectedResult = "a_1_b_1_c_1_d_-1_e_-1_f_-1_g_g_s_s_h_x";

            var result = IndexNameHelper.GetIndexName(keys);

            result.Should().Be(expectedResult);
        }
Example #7
0
        public void CreateEncryptedCreateCollectionOperationIfConfigured_should_return_expected_result_when_EncryptedFields_is_set(string[] escCollectionStrElement, string[] eccCollectionStrElement, string[] ecocCollectionStrElement)
        {
            var encryptedFields = BsonDocument.Parse($@"
            {{
                {GetFirstElementWithCommaOrEmpty(escCollectionStrElement)}
                {GetFirstElementWithCommaOrEmpty(eccCollectionStrElement)}
                {GetFirstElementWithCommaOrEmpty(ecocCollectionStrElement)}
                ""fields"" :
                [{{
                    ""path"" : ""firstName"",
                    ""keyId"" : {{ ""$binary"" : {{ ""subType"" : ""04"", ""base64"" : ""AAAAAAAAAAAAAAAAAAAAAA=="" }} }},
                    ""bsonType"" : ""string"",
                    ""queries"" : {{ ""queryType"" : ""equality"" }}
                }},
                {{
                    ""path"" : ""ssn"",
                    ""keyId"" : {{ ""$binary"" : {{ ""subType"" : ""04"", ""base64"": ""BBBBBBBBBBBBBBBBBBBBBB=="" }} }},
                    ""bsonType"" : ""string""
                }}]
            }}");

            var subject = CreateCollectionOperation.CreateEncryptedCreateCollectionOperationIfConfigured(_collectionNamespace, encryptedFields, _messageEncoderSettings, null);
            var session = OperationTestHelper.CreateSession();

            var operations = ((CompositeWriteOperation <BsonDocument>)subject)._operations <BsonDocument>();

            // esc
            AssertCreateCollectionCommand(
                operations[0],
                new CollectionNamespace(_collectionNamespace.DatabaseNamespace.DatabaseName, GetExpectedCollectionName(escCollectionStrElement)),
                encryptedFields: null,
                isMainOperation: false);
            // ecc
            AssertCreateCollectionCommand(
                operations[1],
                new CollectionNamespace(_collectionNamespace.DatabaseNamespace.DatabaseName, GetExpectedCollectionName(eccCollectionStrElement)),
                encryptedFields: null,
                isMainOperation: false);
            // eco
            AssertCreateCollectionCommand(
                operations[2],
                new CollectionNamespace(_collectionNamespace.DatabaseNamespace.DatabaseName, GetExpectedCollectionName(ecocCollectionStrElement)),
                encryptedFields: null,
                isMainOperation: false);
            // main
            AssertCreateCollectionCommand(
                operations[3],
                _collectionNamespace,
                encryptedFields,
                isMainOperation: true,
                withClusteredIndex: false);
            // __safeContent__
            AssertIndex(operations[4], _collectionNamespace, index: new BsonDocument("__safeContent__", 1));

            void AssertCreateCollectionCommand((IWriteOperation <BsonDocument> Operation, bool IsMainOperation) operationInfo, CollectionNamespace collectionNamespace, BsonDocument encryptedFields, bool isMainOperation, bool withClusteredIndex = true)
            {
                var expectedResult = new BsonDocument
                {
                    { "create", collectionNamespace.CollectionName },
                    { "encryptedFields", encryptedFields, encryptedFields != null },
                    { "clusteredIndex", new BsonDocument {
                          { "key", new BsonDocument("_id", 1) }, { "unique", true }
                      }, withClusteredIndex }
                };

                AssertCommand(operationInfo, isMainOperation, expectedResult);
            }

            void AssertIndex((IWriteOperation <BsonDocument> Operation, bool IsMainOperation) operationInfo, CollectionNamespace collectionNamespace, BsonDocument index)
            {
                var expectedResult = new BsonDocument
                {
                    { "createIndexes", collectionNamespace.CollectionName },
                    {
                        "indexes",
                        BsonArray.Create(new [] { new BsonDocument {
                                                      { "key", index }, { "name", IndexNameHelper.GetIndexName(index) }
                                                  } })
                    }
                };

                AssertCommand(operationInfo, isMainOperation: false, expectedResult);
            }

            void AssertCommand((IWriteOperation <BsonDocument> Operation, bool IsMainOperation) operationInfo, bool isMainOperation, BsonDocument expectedResult)
            {
                operationInfo.IsMainOperation.Should().Be(isMainOperation);
                var operation = operationInfo.Operation;

                var result = operation switch
                {
                    CreateCollectionOperation createCollectionOperation => createCollectionOperation.CreateCommand(session),
                    CreateIndexesOperation createIndexesOperation => createIndexesOperation.CreateCommand(session, OperationTestHelper.CreateConnectionDescription()),
                    _ => throw new Exception($"Unexpected operation {operation}."),
                };

                result.Should().Be(expectedResult);
            }

            string GetFirstElementWithCommaOrEmpty(string[] array) => array.First() != null ? $"{array.First()}," : string.Empty;

            string GetExpectedCollectionName(string[] array)
            {
                var first = array.First();
                var last  = array.Last();

                if (first != null)
                {
                    return(last);
                }
                else
                {
                    return($"enxcol_.{_collectionNamespace.CollectionName}.{last}");
                }
            }
        }
        // publuc methods
        /// <summary>
        /// Gets the name of the index.
        /// </summary>
        /// <returns>The name of the index.</returns>
        public string GetIndexName()
        {
            var additionalOptionsName = _additionalOptions == null ? null : (string)_additionalOptions.GetValue("name", null);

            return(_name ?? additionalOptionsName ?? IndexNameHelper.GetIndexName(_keys));
        }