public void CreateOutputOptions_should_return_expected_result()
        {
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings);
            var subjectReflector = new Reflector(subject);
            var expectedResult = new BsonDocument("inline", 1);

            var result = subjectReflector.CreateOutputOptions();

            result.Should().Be(expectedResult);
        }
        public void constructor_should_initialize_instance()
        {
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings);

            subject.CollectionNamespace.Should().BeSameAs(_collectionNamespace);
            subject.MapFunction.Should().BeSameAs(_mapFunction);
            subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings);
            subject.Filter.Should().BeNull();
            subject.ReduceFunction.Should().BeSameAs(_reduceFunction);
            subject.ResultSerializer.Should().BeSameAs(_resultSerializer);
        }
        public async Task ExecuteAsync_should_return_expected_results()
        {
            await EnsureTestDataAsync();

            var mapFunction = "function() { emit(this.x, this.v); }";
            var reduceFunction = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, mapFunction, reduceFunction, _resultSerializer, _messageEncoderSettings);
            var expectedResults = new List<BsonDocument>
            {
                new BsonDocument { {"_id", 1 }, { "value", 3 } },
                new BsonDocument { {"_id", 2 }, { "value", 4 } },
            };

            var cursor = await ExecuteOperationAsync(subject);
            var results = await cursor.ToListAsync();

            results.Should().Equal(expectedResults);
        }
Пример #4
0
        public void Execute_should_return_expected_results(
            [Values(false, true)]
            bool async)
        {
            RequireServer.Any();
            EnsureTestData();
            var mapFunction = "function() { emit(this.x, this.v); }";
            var reduceFunction = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, mapFunction, reduceFunction, _resultSerializer, _messageEncoderSettings);
            var expectedResults = new List<BsonDocument>
            {
                new BsonDocument { {"_id", 1 }, { "value", 3 } },
                new BsonDocument { {"_id", 2 }, { "value", 4 } },
            };

            var cursor = ExecuteOperation(subject, async);
            var results = ReadCursorToEnd(cursor, async);

            results.Should().Equal(expectedResults);
        }
        public void ExecuteAsync_should_throw_when_binding_is_null()
        {
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings);

            Func<Task> act = () => subject.ExecuteAsync(null, CancellationToken.None);

            act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
        }
 // constructor
 public Reflector(MapReduceOperation<BsonDocument> instance)
 {
     _instance = instance;
 }
        public void ResultSerializer_should_get_value()
        {
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings);

            var result = subject.ResultSerializer;

            result.Should().BeSameAs(_resultSerializer);
        }
        public override async Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TDocument, TResult> options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(map, "map");
            Ensure.IsNotNull(reduce, "reduce");

            options = options ?? new MapReduceOptions <TDocument, TResult>();
            var outputOptions    = options.OutputOptions ?? MapReduceOutputOptions.Inline;
            var resultSerializer = ResolveResultSerializer <TResult>(options.ResultSerializer);

            if (outputOptions == MapReduceOutputOptions.Inline)
            {
                var operation = new MapReduceOperation <TResult>(
                    _collectionNamespace,
                    map,
                    reduce,
                    resultSerializer,
                    _messageEncoderSettings)
                {
                    Filter           = options.Filter == null ? null : options.Filter.Render(_documentSerializer, _settings.SerializerRegistry),
                    FinalizeFunction = options.Finalize,
                    JavaScriptMode   = options.JavaScriptMode,
                    Limit            = options.Limit,
                    MaxTime          = options.MaxTime,
                    Scope            = options.Scope,
                    Sort             = options.Sort == null ? null : options.Sort.Render(_documentSerializer, _settings.SerializerRegistry),
                    Verbose          = options.Verbose
                };

                return(await ExecuteReadOperation(operation, cancellationToken).ConfigureAwait(false));
            }
            else
            {
                var collectionOutputOptions = (MapReduceOutputOptions.CollectionOutput)outputOptions;
                var databaseNamespace       = collectionOutputOptions.DatabaseName == null ?
                                              _collectionNamespace.DatabaseNamespace :
                                              new DatabaseNamespace(collectionOutputOptions.DatabaseName);
                var outputCollectionNamespace = new CollectionNamespace(databaseNamespace, collectionOutputOptions.CollectionName);

                var operation = new MapReduceOutputToCollectionOperation(
                    _collectionNamespace,
                    outputCollectionNamespace,
                    map,
                    reduce,
                    _messageEncoderSettings)
                {
                    Filter           = options.Filter == null ? null : options.Filter.Render(_documentSerializer, _settings.SerializerRegistry),
                    FinalizeFunction = options.Finalize,
                    JavaScriptMode   = options.JavaScriptMode,
                    Limit            = options.Limit,
                    MaxTime          = options.MaxTime,
                    NonAtomicOutput  = collectionOutputOptions.NonAtomic,
                    Scope            = options.Scope,
                    OutputMode       = collectionOutputOptions.OutputMode,
                    ShardedOutput    = collectionOutputOptions.Sharded,
                    Sort             = options.Sort == null ? null : options.Sort.Render(_documentSerializer, _settings.SerializerRegistry),
                    Verbose          = options.Verbose
                };

                await ExecuteWriteOperation(operation, cancellationToken).ConfigureAwait(false);

                var findOperation = new FindOperation <TResult>(
                    outputCollectionNamespace,
                    resultSerializer,
                    _messageEncoderSettings)
                {
                    MaxTime = options.MaxTime
                };

                // we want to delay execution of the find because the user may
                // not want to iterate the results at all...
                var deferredCursor = new DeferredAsyncCursor <TResult>(ct => ExecuteReadOperation(findOperation, ReadPreference.Primary, ct));
                return(await Task.FromResult(deferredCursor).ConfigureAwait(false));
            }
        }
Пример #9
0
        public void CreateCommand_should_include_read_concern_when_appropriate(
            [Values(null, ReadConcernLevel.Local, ReadConcernLevel.Majority)] ReadConcernLevel? readConcernLevel)
        {
            var readConcern = new ReadConcern(readConcernLevel);
            var mapFunction = "function() { emit(this.x, this.v); }";
            var reduceFunction = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, mapFunction, reduceFunction, _resultSerializer, _messageEncoderSettings)
            {
                ReadConcern = readConcern
            };

            var command = subject.CreateCommand(new SemanticVersion(3, 2, 0));

            if (readConcern.IsServerDefault)
            {
                command.Contains("readConcern").Should().BeFalse();
            }
            else
            {
                command["readConcern"].Should().Be(readConcern.ToBsonDocument());
            }
        }
Пример #10
0
        public void Execute_should_throw_when_binding_is_null(
            [Values(false, true)]
            bool async)
        {
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings);
            IReadBinding binding = null;

            Action act = () => ExecuteOperation(subject, binding, async);

            act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
        }
Пример #11
0
        public void CreateCommand_should_throw_when_read_concern_is_not_supported()
        {
            var mapFunction = "function() { emit(this.x, this.v); }";
            var reduceFunction = "function(key, values) { var sum = 0; for (var i = 0; i < values.length; i++) { sum += values[i]; }; return sum; }";
            var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, mapFunction, reduceFunction, _resultSerializer, _messageEncoderSettings)
            {
                ReadConcern = ReadConcern.Majority
            };

            Action act = () => subject.CreateCommand(new SemanticVersion(3, 0, 0));
            act.ShouldThrow<MongoClientException>();
        }