Ejemplo n.º 1
0
        private IReadOnlyList <IAsyncCursor <TDocument> > CreateCursors(IChannelSourceHandle channelSource, BsonDocument command, BsonDocument result)
        {
            var cursors = new List <AsyncCursor <TDocument> >();

            foreach (var cursorDocument in result["cursors"].AsBsonArray.Select(v => v["cursor"].AsBsonDocument))
            {
                var cursorId   = cursorDocument["id"].ToInt64();
                var firstBatch = cursorDocument["firstBatch"].AsBsonArray.Select(v =>
                {
                    var bsonDocument = (BsonDocument)v;
                    using (var reader = new BsonDocumentReader(bsonDocument))
                    {
                        var context  = BsonDeserializationContext.CreateRoot(reader);
                        var document = _serializer.Deserialize(context);
                        return(document);
                    }
                })
                                 .ToList();

                var cursor = new AsyncCursor <TDocument>(
                    channelSource.Fork(),
                    _collectionNamespace,
                    command,
                    firstBatch,
                    cursorId,
                    _batchSize ?? 0,
                    0, // limit
                    _serializer,
                    _messageEncoderSettings);

                cursors.Add(cursor);
            }

            return(cursors);
        }
Ejemplo n.º 2
0
        private async Task <IAsyncCursor <BsonDocument> > ExecuteUsingCommandAsync(IChannelSourceHandle channelSource, ReadPreference readPreference, CancellationToken cancellationToken)
        {
            var databaseNamespace = _collectionNamespace.DatabaseNamespace;
            var command           = new BsonDocument("listIndexes", _collectionNamespace.CollectionName);
            var operation         = new ReadCommandOperation <BsonDocument>(databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            try
            {
                var response = await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false);

                var cursorDocument = response["cursor"].AsBsonDocument;
                var cursor         = new AsyncCursor <BsonDocument>(
                    channelSource.Fork(),
                    CollectionNamespace.FromFullName(cursorDocument["ns"].AsString),
                    command,
                    cursorDocument["firstBatch"].AsBsonArray.OfType <BsonDocument>().ToList(),
                    cursorDocument["id"].ToInt64(),
                    0,
                    0,
                    BsonDocumentSerializer.Instance,
                    _messageEncoderSettings);

                return(cursor);
            }
            catch (MongoCommandException ex)
            {
                if (ex.Code == 26)
                {
                    return(new SingleBatchAsyncCursor <BsonDocument>(new List <BsonDocument>()));
                }
                throw;
            }
        }
 /// <summary>
 /// Executes a write operation using a channel source.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="operation">The write operation.</param>
 /// <param name="channelSource">The channel source.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>A Task whose result is the result of the operation.</returns>
 public static async Task <TResult> ExecuteAsync <TResult>(
     this IWriteOperation <TResult> operation,
     IChannelSourceHandle channelSource,
     CancellationToken cancellationToken)
 {
     Ensure.IsNotNull(operation, "operation");
     using (var writeBinding = new ChannelSourceReadWriteBinding(channelSource.Fork(), ReadPreference.Primary))
     {
         return(await operation.ExecuteAsync(writeBinding, cancellationToken).ConfigureAwait(false));
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Executes a write operation using a channel source.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="operation">The write operation.</param>
 /// <param name="channelSource">The channel source.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The result of the operation.</returns>
 public static TResult Execute <TResult>(
     this IWriteOperation <TResult> operation,
     IChannelSourceHandle channelSource,
     CancellationToken cancellationToken)
 {
     Ensure.IsNotNull(operation, nameof(operation));
     using (var writeBinding = new ChannelSourceReadWriteBinding(channelSource.Fork(), ReadPreference.Primary))
     {
         return(operation.Execute(writeBinding, cancellationToken));
     }
 }
Ejemplo n.º 5
0
 private AsyncCursor <TResult> CreateCursorFromCursorResult(IChannelSourceHandle channelSource, BsonDocument command, AggregateResult result)
 {
     return(new AsyncCursor <TResult>(
                channelSource.Fork(),
                CollectionNamespace,
                command,
                result.Results,
                result.CursorId.GetValueOrDefault(0),
                _batchSize ?? 0,
                0, // limit
                _resultSerializer,
                MessageEncoderSettings));
 }
 /// <summary>
 /// Executes a read operation using a channel source.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="operation">The read operation.</param>
 /// <param name="channelSource">The channel source.</param>
 /// <param name="readPreference">The read preference.</param>
 /// <param name="session">The session.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// A Task whose result is the result of the operation.
 /// </returns>
 public static async Task <TResult> ExecuteAsync <TResult>(
     this IReadOperation <TResult> operation,
     IChannelSourceHandle channelSource,
     ReadPreference readPreference,
     ICoreSessionHandle session,
     CancellationToken cancellationToken)
 {
     Ensure.IsNotNull(operation, nameof(operation));
     using (var readBinding = new ChannelSourceReadWriteBinding(channelSource.Fork(), readPreference, session.Fork()))
     {
         return(await operation.ExecuteAsync(readBinding, cancellationToken).ConfigureAwait(false));
     }
 }
 /// <summary>
 /// Executes a read operation using a channel source.
 /// </summary>
 /// <typeparam name="TResult">The type of the result.</typeparam>
 /// <param name="operation">The read operation.</param>
 /// <param name="channelSource">The channel source.</param>
 /// <param name="readPreference">The read preference.</param>
 /// <param name="session">The session.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>
 /// The result of the operation.
 /// </returns>
 public static TResult Execute <TResult>(
     this IReadOperation <TResult> operation,
     IChannelSourceHandle channelSource,
     ReadPreference readPreference,
     ICoreSessionHandle session,
     CancellationToken cancellationToken)
 {
     Ensure.IsNotNull(operation, nameof(operation));
     using (var readBinding = new ChannelSourceReadWriteBinding(channelSource.Fork(), readPreference, session.Fork()))
     {
         return(operation.Execute(readBinding, cancellationToken));
     }
 }
Ejemplo n.º 8
0
 // private methods
 private IAsyncCursor <TDocument> CreateCursor(IChannelSourceHandle channelSource, BsonDocument query, CursorBatch <TDocument> batch)
 {
     return(new AsyncCursor <TDocument>(
                channelSource.Fork(),
                _collectionNamespace,
                query,
                batch.Documents,
                batch.CursorId,
                _batchSize,
                _limit < 0 ? Math.Abs(_limit.Value) : _limit,
                _resultSerializer,
                _messageEncoderSettings));
 }
Ejemplo n.º 9
0
 private AsyncCursor <TDocument> CreateCursor(IChannelSourceHandle channelSource, CursorBatch <TDocument> batch, bool slaveOk)
 {
     return(new AsyncCursor <TDocument>(
                channelSource.Fork(),
                _collectionNamespace,
                _filter ?? new BsonDocument(),
                batch.Documents,
                batch.CursorId,
                _batchSize,
                _limit < 0 ? Math.Abs(_limit.Value) : _limit,
                _resultSerializer,
                _messageEncoderSettings,
                null)); // maxTime
 }
Ejemplo n.º 10
0
        private IAsyncCursor <BsonDocument> CreateCursor(IChannelSourceHandle channelSource, BsonDocument result, BsonDocument command)
        {
            var cursorDocument = result["cursor"].AsBsonDocument;
            var cursor         = new AsyncCursor <BsonDocument>(
                channelSource.Fork(),
                CollectionNamespace.FromFullName(cursorDocument["ns"].AsString),
                command,
                cursorDocument["firstBatch"].AsBsonArray.OfType <BsonDocument>().ToList(),
                cursorDocument["id"].ToInt64(),
                0,
                0,
                BsonDocumentSerializer.Instance,
                _messageEncoderSettings);

            return(cursor);
        }
Ejemplo n.º 11
0
        private async Task <IAsyncCursor <BsonDocument> > ExecuteUsingCommandAsync(IChannelSourceHandle channelSource, ReadPreference readPreference, CancellationToken cancellationToken)
        {
            var command = new BsonDocument
            {
                { "listCollections", 1 },
                { "filter", _filter, _filter != null }
            };
            var operation = new ReadCommandOperation <BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var response  = await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false);

            var cursorDocument = response["cursor"].AsBsonDocument;
            var cursor         = new AsyncCursor <BsonDocument>(
                channelSource.Fork(),
                CollectionNamespace.FromFullName(cursorDocument["ns"].AsString),
                command,
                cursorDocument["firstBatch"].AsBsonArray.OfType <BsonDocument>().ToList(),
                cursorDocument["id"].ToInt64(),
                0,
                0,
                BsonDocumentSerializer.Instance,
                _messageEncoderSettings);

            return(cursor);
        }
Ejemplo n.º 12
0
 private IChannelSourceHandle GetChannelSourceHelper()
 {
     return(_channelSource.Fork());
 }
Ejemplo n.º 13
0
 // methods
 /// <inheritdoc/>
 public Task <IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
 {
     ThrowIfDisposed();
     return(Task.FromResult(_channelSource.Fork()));
 }