private async Task <IEnumerable <BsonDocument> > ExecuteUsingCommandAsync(IConnectionSourceHandle connectionSource, ReadPreference readPreference, TimeSpan timeout, CancellationToken cancellationToken) { var databaseNamespace = _collectionNamespace.DatabaseNamespace; var command = new BsonDocument("listIndexes", _collectionNamespace.CollectionName); var operation = new ReadCommandOperation <BsonDocument>(databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); BsonDocument result; try { result = await operation.ExecuteAsync(connectionSource, readPreference, timeout, cancellationToken).ConfigureAwait(false); } catch (MongoCommandException ex) { var response = ex.Result; BsonValue code; if (response.TryGetValue("code", out code) && code.IsNumeric && code.ToInt32() == 26) { return(Enumerable.Empty <BsonDocument>()); } throw; } return(result["indexes"].AsBsonArray.Cast <BsonDocument>()); }
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; } }
public async Task <BsonDocument> ExecuteCommandAsync(IReadBinding binding, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation(_collectionNamespace.DatabaseNamespace, command, _messageEncoderSettings); return(await operation.ExecuteAsync(binding, timeout, cancellationToken)); }
public async Task <BsonDocument> ExecuteCommandAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation <BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); return(await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false)); }
public Task <TResult> ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation <TResult>(CollectionNamespace.DatabaseNamespace, command, _resultSerializer, MessageEncoderSettings); return(operation.ExecuteAsync(binding, cancellationToken)); }
/// <inheritdoc/> public Task <BsonDocument> ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, nameof(binding)); var command = CreateCommand(); var operation = new ReadCommandOperation <BsonDocument>(CollectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, MessageEncoderSettings); return(operation.ExecuteAsync(binding, cancellationToken)); }
public async Task <BsonDocument> ExecuteAsync(IReadBinding binding, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation("admin", command); return(await operation.ExecuteAsync(binding, timeout, cancellationToken)); }
public async Task <BsonValue> ExecuteAsync(IReadBinding binding, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation(_databaseName, command); var result = await operation.ExecuteAsync(binding, timeout, cancellationToken); return(result["retval"]); }
public async Task <long> ExecuteAsync(IReadBinding binding, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation(_collectionNamespace.DatabaseNamespace, command, _messageEncoderSettings); var document = await operation.ExecuteAsync(binding, timeout, cancellationToken).ConfigureAwait(false); return(document["n"].ToInt64()); }
public Task <BsonDocument> GetCollectionStatsAsync(IReadBinding binding) { var command = new BsonDocument { { "collStats", _collectionNamespace.CollectionName } }; var operation = new ReadCommandOperation <BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); return(operation.ExecuteAsync(binding, CancellationToken.None)); }
public async Task <IReadOnlyList <TValue> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var valueArraySerializer = new ArraySerializer <TValue>(_valueSerializer); var resultSerializer = new ElementDeserializer <TValue[]>("values", valueArraySerializer); var operation = new ReadCommandOperation <TValue[]>(_collectionNamespace.DatabaseNamespace, command, resultSerializer, _messageEncoderSettings); return(await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false)); }
public Task <BsonDocument> GetCollectionStatsAsync(IReadBinding binding) { var command = new BsonDocument { { "collStats", _collectionNamespace.CollectionName } }; var operation = new ReadCommandOperation(_collectionNamespace.DatabaseNamespace, command, _messageEncoderSettings); return(operation.ExecuteAsync(binding)); }
/// <inheritdoc/> public async Task <IAsyncCursor <BsonDocument> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation <BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); var response = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false); var databases = response["databases"].AsBsonArray.OfType <BsonDocument>(); return(new SingleBatchAsyncCursor <BsonDocument>(databases.ToList())); }
public async Task <IReadOnlyList <string> > ExecuteAsync(IReadBinding binding, TimeSpan timeout, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, "binding"); var command = CreateCommand(); var operation = new ReadCommandOperation(DatabaseNamespace.Admin, command, _messageEncoderSettings); var response = await operation.ExecuteAsync(binding, timeout, cancellationToken).ConfigureAwait(false); var databases = response["databases"].AsBsonArray; return(databases.Select(database => database["name"].ToString()).ToList()); }
/// <inheritdoc/> public async Task <IAsyncCursor <TResult> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, nameof(binding)); var command = CreateCommand(); var resultArraySerializer = new ArraySerializer <TResult>(_resultSerializer); var resultSerializer = new ElementDeserializer <TResult[]>("results", resultArraySerializer); var operation = new ReadCommandOperation <TResult[]>(CollectionNamespace.DatabaseNamespace, command, resultSerializer, MessageEncoderSettings); var result = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false); return(new SingleBatchAsyncCursor <TResult>(result)); }
/// <inheritdoc/> public async Task <IEnumerable <TResult> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, nameof(binding)); var command = CreateCommand(); var resultSerializer = _resultSerializer ?? BsonSerializer.LookupSerializer <TResult>(); var resultArraySerializer = new ArraySerializer <TResult>(resultSerializer); var commandResultSerializer = new ElementDeserializer <TResult[]>("retval", resultArraySerializer); var operation = new ReadCommandOperation <TResult[]>(_collectionNamespace.DatabaseNamespace, command, commandResultSerializer, _messageEncoderSettings); return(await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false)); }
private async Task <IReadOnlyList <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); return(response["collections"].AsBsonArray.Select(value => (BsonDocument)value).ToList()); }
public Task <BsonDocument> ExecuteAsync(IReadBinding binding, TimeSpan timeout, CancellationToken cancellationToken) { var command = CreateCommand(); var operation = new ReadCommandOperation <BsonDocument>( _collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); return(operation.ExecuteAsync(binding, timeout, cancellationToken)); }
public async Task <Cursor <BsonDocument> > ExecuteAsync(IReadBinding binding, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken)) { Ensure.IsNotNull(binding, "binding"); EnsureIsReadOnlyPipeline(); var slidingTimeout = new SlidingTimeout(timeout); using (var connectionSource = await binding.GetReadConnectionSourceAsync(slidingTimeout, cancellationToken)) { var command = CreateCommand(); var operation = new ReadCommandOperation(CollectionNamespace.DatabaseNamespace, command, MessageEncoderSettings); var result = await operation.ExecuteAsync(connectionSource, binding.ReadPreference, slidingTimeout, cancellationToken); return(CreateCursor(connectionSource, command, result, timeout, cancellationToken)); } }
// methods public async Task <IAsyncCursor <TResult> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, "binding"); EnsureIsReadOnlyPipeline(); using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false)) { var command = CreateCommand(channelSource.ServerDescription.Version); var serializer = new AggregateResultDeserializer(_resultSerializer); var operation = new ReadCommandOperation <AggregateResult>(CollectionNamespace.DatabaseNamespace, command, serializer, MessageEncoderSettings); var result = await operation.ExecuteAsync(channelSource, binding.ReadPreference, cancellationToken).ConfigureAwait(false); return(CreateCursor(channelSource, command, result)); } }
private BsonDocument GetCollectionStats(IReadBinding binding, bool async) { var command = new BsonDocument { { "collStats", _collectionNamespace.CollectionName } }; var operation = new ReadCommandOperation <BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); if (async) { return(operation.ExecuteAsync(binding, CancellationToken.None).GetAwaiter().GetResult()); } else { return(operation.Execute(binding, CancellationToken.None)); } }
/// <inheritdoc/> public async Task <IReadOnlyList <IAsyncCursor <TDocument> > > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) { Ensure.IsNotNull(binding, nameof(binding)); using (EventContext.BeginOperation()) using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false)) { var command = CreateCommand(); var operation = new ReadCommandOperation <BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings); var result = await operation.ExecuteAsync(channelSource, binding.ReadPreference, cancellationToken).ConfigureAwait(false); 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); } }
private async Task <IReadOnlyList <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); BsonDocument result; try { result = await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false); } catch (MongoCommandException ex) { if (ex.Code == 26) { return(new List <BsonDocument>()); } throw; } return(result["indexes"].AsBsonArray.Cast <BsonDocument>().ToList()); }
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); }