public Task <IAsyncCursor <BsonDocument> > ListCollectionsAsync(ListCollectionsOptions options, CancellationToken cancellationToken)
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new ListCollectionsOperation(_databaseNamespace, messageEncoderSettings)
            {
                Filter = options == null ? null : BsonDocumentHelper.FilterToBsonDocument <BsonDocument>(_settings.SerializerRegistry, options.Filter)
            };

            return(ExecuteReadOperation(operation, ReadPreference.Primary, cancellationToken));
        }
        // methods
        public Task CreateCollectionAsync(string name, CreateCollectionOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNullOrEmpty(name, "name");
            options = options ?? new CreateCollectionOptions();
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new CreateCollectionOperation(new CollectionNamespace(_databaseNamespace, name), messageEncoderSettings)
            {
                AutoIndexId      = options.AutoIndexId,
                Capped           = options.Capped,
                MaxDocuments     = options.MaxDocuments,
                MaxSize          = options.MaxSize,
                StorageEngine    = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, options.StorageEngine),
                UsePowerOf2Sizes = options.UsePowerOf2Sizes
            };

            return(ExecuteWriteOperation(operation, cancellationToken));
        }
        public Task <T> RunCommandAsync <T>(object command, ReadPreference readPreference = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(command, "command");
            readPreference = readPreference ?? ReadPreference.Primary;

            var commandDocument        = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, command);
            var serializer             = _settings.SerializerRegistry.GetSerializer <T>();
            var messageEncoderSettings = GetMessageEncoderSettings();

            if (readPreference == ReadPreference.Primary)
            {
                var operation = new WriteCommandOperation <T>(_databaseNamespace, commandDocument, serializer, messageEncoderSettings);
                return(ExecuteWriteOperation <T>(operation, cancellationToken));
            }
            else
            {
                var operation = new ReadCommandOperation <T>(_databaseNamespace, commandDocument, serializer, messageEncoderSettings);
                return(ExecuteReadOperation <T>(operation, readPreference, cancellationToken));
            }
        }
        public Task <T> RunCommandAsync <T>(object command, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(command, "command");

            var commandDocument = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, command);

            var isReadCommand          = CanCommandBeSentToSecondary.Delegate(commandDocument);
            var serializer             = _settings.SerializerRegistry.GetSerializer <T>();
            var messageEncoderSettings = GetMessageEncoderSettings();

            if (isReadCommand)
            {
                var operation = new ReadCommandOperation <T>(_databaseNamespace, commandDocument, serializer, messageEncoderSettings);
                return(ExecuteReadOperation <T>(operation, cancellationToken));
            }
            else
            {
                var operation = new WriteCommandOperation <T>(_databaseNamespace, commandDocument, serializer, messageEncoderSettings);
                return(ExecuteWriteOperation <T>(operation, cancellationToken));
            }
        }
Esempio n. 5
0
 private BsonDocument ConvertFilterToBsonDocument(object filter)
 {
     return(BsonDocumentHelper.FilterToBsonDocument <TDocument>(_settings.SerializerRegistry, filter));
 }
Esempio n. 6
0
 private BsonDocument ConvertToBsonDocument(object document)
 {
     return(BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, document));
 }
Esempio n. 7
0
        public async Task <IAsyncCursor <TResult> > MapReduceAsync <TResult>(BsonJavaScript map, BsonJavaScript reduce, MapReduceOptions <TResult> options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(map, "map");
            Ensure.IsNotNull(reduce, "reduce");

            options = options ?? new MapReduceOptions <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           = BsonDocumentHelper.FilterToBsonDocument <TDocument>(_settings.SerializerRegistry, options.Filter),
                    FinalizeFunction = options.Finalize,
                    JavaScriptMode   = options.JavaScriptMode,
                    Limit            = options.Limit,
                    MaxTime          = options.MaxTime,
                    Scope            = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, options.Scope),
                    Sort             = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, options.Sort),
                    Verbose          = options.Verbose
                };

                return(await ExecuteReadOperation(operation, cancellationToken));
            }
            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           = BsonDocumentHelper.FilterToBsonDocument <TDocument>(_settings.SerializerRegistry, options.Filter),
                    FinalizeFunction = options.Finalize,
                    JavaScriptMode   = options.JavaScriptMode,
                    Limit            = options.Limit,
                    MaxTime          = options.MaxTime,
                    NonAtomicOutput  = collectionOutputOptions.NonAtomic,
                    Scope            = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, options.Scope),
                    OutputMode       = collectionOutputOptions.OutputMode,
                    ShardedOutput    = collectionOutputOptions.Sharded,
                    Sort             = BsonDocumentHelper.ToBsonDocument(_settings.SerializerRegistry, options.Sort),
                    Verbose          = options.Verbose
                };

                var result = await ExecuteWriteOperation(operation, cancellationToken);

                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));
            }
        }
Esempio n. 8
0
 private BsonDocument ConvertFilterToBsonDocument(object filter)
 {
     return(BsonDocumentHelper.FilterToBsonDocument <TResult>(_collection.Settings.SerializerRegistry, filter));
 }