Ejemplo n.º 1
0
        internal BsonDocument CreateWrappedQuery(ServerType serverType, ReadPreference readPreference)
        {
            var readPreferenceDocument = QueryHelper.CreateReadPreferenceDocument(serverType, readPreference);

            var wrappedQuery = new BsonDocument
            {
                { "$query", _filter ?? new BsonDocument() },
                { "$readPreference", readPreferenceDocument, readPreferenceDocument != null },
                { "$orderby", _sort, _sort != null },
                { "$comment", _comment, _comment != null },
                { "$maxTimeMS", () => MaxTimeHelper.ToMaxTimeMS(_maxTime.Value), _maxTime.HasValue },
                { "$hint", _hint, _hint != null },
                { "$max", _max, _max != null },
                { "$maxScan", () => _maxScan.Value, _maxScan.HasValue },
                { "$min", _min, _min != null },
                { "$showDiskLoc", () => _showRecordId.Value, _showRecordId.HasValue },
                { "$snapshot", () => _snapshot.Value, _snapshot.HasValue }
            };

            if (_modifiers != null)
            {
                wrappedQuery.Merge(_modifiers, overwriteExistingElements: false);
            }

            if (wrappedQuery.ElementCount == 1 && wrappedQuery.GetElement(0).Name == "$query")
            {
                var unwrappedQuery = wrappedQuery[0].AsBsonDocument;
                if (!unwrappedQuery.Contains("query"))
                {
                    return(unwrappedQuery);
                }
            }

            return(wrappedQuery);
        }
Ejemplo n.º 2
0
        public void CreateReadPreferenceDocument_should_return_a_document_when_the_mode_is_not_Primary_or_SecondaryPreferred()
        {
            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ShardRouter, ReadPreference.PrimaryPreferred, out var slaveOk);

            result.Should().Be("{mode: \"primaryPreferred\"}");
            slaveOk.Should().BeTrue();
        }
Ejemplo n.º 3
0
        // methods
        private BsonDocument CreateWrappedCommand(ServerDescription serverDescription, ReadPreference readPreference)
        {
            BsonDocument readPreferenceDocument = null;

            if (serverDescription.Type == ServerType.ShardRouter)
            {
                readPreferenceDocument = QueryHelper.CreateReadPreferenceDocument(serverDescription.Type, readPreference);
            }

            var wrappedCommand = new BsonDocument
            {
                { "$query", _command },
                { "$readPreference", readPreferenceDocument, readPreferenceDocument != null },
                { "$comment", () => _comment, _comment != null }
            };

            if (_additionalOptions != null)
            {
                wrappedCommand.Merge(_additionalOptions, overwriteExistingElements: false);
            }

            if (wrappedCommand.ElementCount == 1)
            {
                return(_command);
            }
            else
            {
                return(wrappedCommand);
            }
        }
Ejemplo n.º 4
0
        public void CreateReadPreferenceDocument_should_return_null_when_the_readPreference_is_SecondaryPreferred_with_no_additional_options()
        {
            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ShardRouter, ReadPreference.SecondaryPreferred, out var slaveOk);

            result.Should().BeNull();
            slaveOk.Should().BeTrue();
        }
Ejemplo n.º 5
0
        public void CreateReadPreferenceDocument_should_return_null_when_the_serverType_is_not_a_shard_router()
        {
            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ReplicaSetSecondary, ReadPreference.PrimaryPreferred, out var slaveOk);

            result.Should().BeNull();
            slaveOk.Should().BeTrue();
        }
Ejemplo n.º 6
0
        public void CreateReadPreferenceDocument_should_return_null_when_the_readPreference_is_Primary_with_no_additional_options()
        {
            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ShardRouter, ReadPreference.Primary, out var secondaryOk);

            result.Should().BeNull();
            secondaryOk.Should().BeFalse();
        }
Ejemplo n.º 7
0
        public void CreateReadPreferenceDocument_should_return_a_document_when_their_are_tag_sets()
        {
            var rp = ReadPreference.Secondary.With(tagSets: new[] { new TagSet(new[] { new Tag("dc", "tx") }) });

            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ShardRouter, rp);

            result.Should().Be("{mode: \"secondary\", tags: [{dc: \"tx\"}]}");
        }
Ejemplo n.º 8
0
        public void CreateReadPreferenceDocument_should_return_a_document_when_maxStaleness_is_used()
        {
            var readPreference = ReadPreference.Secondary.With(maxStaleness: TimeSpan.FromSeconds(1));

            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ShardRouter, readPreference);

            result.Should().Be("{ mode : \"secondary\", maxStalenessSeconds : 1.0 }");
        }
Ejemplo n.º 9
0
        public void CreateReadPreferenceDocument_should_return_expected_result_when_hedge_is_used(
            bool?isEnabled,
            string expectedResult)
        {
            var hedge          = isEnabled.HasValue ? new ReadPreferenceHedge(isEnabled.Value) : null;
            var readPreference = ReadPreference.Secondary.With(hedge: hedge);

            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ShardRouter, readPreference, out var slaveOk);

            result.Should().Be(expectedResult);
            slaveOk.Should().BeTrue();
        }
Ejemplo n.º 10
0
        internal BsonDocument CreateWrappedQuery(ServerType serverType, ReadPreference readPreference)
        {
            var readPreferenceDocument = QueryHelper.CreateReadPreferenceDocument(serverType, readPreference);

            var wrappedQuery = new BsonDocument
            {
                { "$query", _filter ?? new BsonDocument() },
                { "$readPreference", readPreferenceDocument, readPreferenceDocument != null },
                { "$orderby", _sort, _sort != null },
                { "$comment", _comment, _comment != null },
                { "$maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue }
            };

            if (_modifiers != null)
            {
                wrappedQuery.Merge(_modifiers, overwriteExistingElements: false);
            }

            return(wrappedQuery);
        }
Ejemplo n.º 11
0
        // methods
        private BsonDocument CreateCommand(ServerDescription serverDescription, ReadPreference readPreference)
        {
            _readConcern.ThrowIfNotSupported(serverDescription.Version);

            var firstBatchSize         = _firstBatchSize ?? (_batchSize > 0 ? _batchSize : null);
            var readPreferenceDocument = QueryHelper.CreateReadPreferenceDocument(serverDescription.Type, readPreference);
            var isShardRouter          = serverDescription.Type == ServerType.ShardRouter;

            var command = new BsonDocument
            {
                { "find", _collectionNamespace.CollectionName },
                { "filter", _filter, _filter != null },
                { "sort", _sort, _sort != null },
                { "projection", _projection, _projection != null },
                { "hint", _hint, _hint != null },
                { "skip", () => _skip.Value, _skip.HasValue },
                { "limit", () => Math.Abs(_limit.Value), _limit.HasValue&& _limit != 0 },
                { "batchSize", () => firstBatchSize.Value, firstBatchSize.HasValue },
                { "singleBatch", () => _limit < 0 || _singleBatch.Value, _limit < 0 || _singleBatch.HasValue },
                { "comment", _comment, _comment != null },
                { "maxScan", () => _maxScan.Value, _maxScan.HasValue },
                { "maxTimeMS", () => _maxTime.Value.TotalMilliseconds, _maxTime.HasValue },
                { "readPreference", readPreferenceDocument, readPreferenceDocument != null },
                { "max", _max, _max != null },
                { "min", _min, _min != null },
                { "returnKey", () => _returnKey.Value, _returnKey.HasValue },
                { "showRecordId", () => _showRecordId.Value, _showRecordId.HasValue },
                { "snapshot", () => _snapshot.Value, _snapshot.HasValue },
                { "tailable", true, _cursorType == CursorType.Tailable || _cursorType == CursorType.TailableAwait },
                { "oplogReplay", () => _oplogReplay.Value, _oplogReplay.HasValue },
                { "noCursorTimeout", () => _noCursorTimeout.Value, _noCursorTimeout.HasValue },
                { "awaitData", true, _cursorType == CursorType.TailableAwait },
                { "allowPartialResults", () => _allowPartialResults.Value, _allowPartialResults.HasValue&& isShardRouter },
                { "readConcern", () => _readConcern.ToBsonDocument(), !_readConcern.IsServerDefault }
            };

            return(command);
        }
Ejemplo n.º 12
0
        public void CreateReadPreferenceDocument_should_return_null_when_the_readPreference_is_SecondaryPreferred_with_no_tag_sets()
        {
            var result = QueryHelper.CreateReadPreferenceDocument(ServerType.ShardRouter, ReadPreference.SecondaryPreferred);

            result.Should().BeNull();
        }