Example #1
0
        // methods
        public async Task <IReadOnlyList <string> > ExecuteAsync(IReadBinding binding, TimeSpan timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, "binding");
            var operation = new FindOperation <BsonDocument>(_databaseNamespace.SystemNamespacesCollection, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var cursor    = await operation.ExecuteAsync(binding, timeout, cancellationToken).ConfigureAwait(false);

            var result = new List <string>();
            var prefix = _databaseNamespace + ".";

            while (await cursor.MoveNextAsync().ConfigureAwait(false))
            {
                var batch = cursor.Current;
                foreach (var document in batch)
                {
                    var name = (string)document["name"];
                    if (name.StartsWith(prefix))
                    {
                        var collectionName = name.Substring(prefix.Length);
                        if (!collectionName.Contains('$') || collectionName.EndsWith(".oplog.$"))
                        {
                            result.Add(collectionName);
                        }
                    }
                }
            }

            return(result);
        }
Example #2
0
        // methods
        public async Task <IReadOnlyList <string> > ExecuteAsync(IReadBinding binding, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken))
        {
            Ensure.IsNotNull(binding, "binding");
            var operation = new FindOperation(_databaseName, "system.namespaces");
            var cursor    = await operation.ExecuteAsync(binding, timeout, cancellationToken);

            var result = new List <string>();
            var prefix = _databaseName + ".";

            while (await cursor.MoveNextAsync())
            {
                var batch = cursor.Current;
                foreach (var document in batch)
                {
                    var name = (string)document["name"];
                    if (name.StartsWith(prefix))
                    {
                        var collectionName = name.Substring(prefix.Length);
                        if (!collectionName.Contains('$') || collectionName.EndsWith(".oplog.$"))
                        {
                            result.Add(collectionName);
                        }
                    }
                }
            }

            return(result);
        }
        public void ExecuteAsync_should_throw_when_binding_is_null()
        {
            var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

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

            action.ShouldThrow <ArgumentNullException>().And.ParamName.Should().Be("binding");
        }
Example #4
0
        private async Task <IAsyncCursor <BsonDocument> > ExecuteUsingQueryAsync(IChannelSourceHandle channelSource, ReadPreference readPreference, CancellationToken cancellationToken)
        {
            var systemIndexesCollection = _collectionNamespace.DatabaseNamespace.SystemIndexesCollection;
            var filter    = new BsonDocument("ns", _collectionNamespace.FullName);
            var operation = new FindOperation <BsonDocument>(systemIndexesCollection, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = filter
            };

            return(await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false));
        }
Example #5
0
            public async Task <BsonDocument> ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
            {
                using (var cursor = await _explainOperation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
                {
                    if (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
                    {
                        var batch = cursor.Current;
                        return(batch.Single());
                    }
                }

                throw new MongoException("No explanation was returned.");
            }
Example #6
0
        private async Task <IReadOnlyList <BsonDocument> > ExecuteUsingQueryAsync(IChannelSourceHandle channelSource, ReadPreference readPreference, CancellationToken cancellationToken)
        {
            // if the filter includes a comparison to the "name" we must convert the value to a full namespace
            var filter = _filter;

            if (filter != null && filter.Contains("name"))
            {
                var value = filter["name"];
                if (!value.IsString)
                {
                    throw new NotSupportedException("Name filter must be a plain string when connected to a server version less than 2.8.");
                }
                filter         = (BsonDocument)filter.Clone(); // shallow clone
                filter["name"] = _databaseNamespace.DatabaseName + "." + value;
            }

            var operation = new FindOperation <BsonDocument>(_databaseNamespace.SystemNamespacesCollection, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = filter
            };
            var cursor = await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false);

            var collections = new List <BsonDocument>();
            var prefix      = _databaseNamespace + ".";

            while (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
            {
                var batch = cursor.Current;
                foreach (var collection in batch)
                {
                    var name = (string)collection["name"];
                    if (name.StartsWith(prefix))
                    {
                        var collectionName = name.Substring(prefix.Length);
                        if (!collectionName.Contains('$'))
                        {
                            collection["name"] = collectionName; // replace the full namespace with just the collection name
                            collections.Add(collection);
                        }
                    }
                }
            }

            return(collections);
        }
        public void Execute_should_throw_when_binding_is_null(
            [Values(false, true)]
            bool async)
        {
            var subject = new FindOperation <BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var exception = Record.Exception(() =>
            {
                if (async)
                {
                    subject.ExecuteAsync(null, CancellationToken.None).GetAwaiter().GetResult();
                }
                else
                {
                    subject.Execute(null, CancellationToken.None);
                }
            });

            var argumentNullException = exception.Should().BeOfType <ArgumentNullException>().Subject;

            argumentNullException.ParamName.Should().Be("binding");
        }
Example #8
0
        private async Task <IAsyncCursor <BsonDocument> > ExecuteUsingQueryAsync(IChannelSourceHandle channelSource, ReadPreference readPreference, CancellationToken cancellationToken)
        {
            // if the filter includes a comparison to the "name" we must convert the value to a full namespace
            var filter = _filter;

            if (filter != null && filter.Contains("name"))
            {
                var value = filter["name"];
                if (!value.IsString)
                {
                    throw new NotSupportedException("Name filter must be a plain string when connected to a server version less than 2.8.");
                }
                filter         = (BsonDocument)filter.Clone(); // shallow clone
                filter["name"] = _databaseNamespace.DatabaseName + "." + value;
            }

            var operation = new FindOperation <BsonDocument>(_databaseNamespace.SystemNamespacesCollection, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = filter
            };
            var cursor = await operation.ExecuteAsync(channelSource, readPreference, cancellationToken).ConfigureAwait(false);

            return(new BatchTransformingAsyncCursor <BsonDocument, BsonDocument>(cursor, NormalizeQueryResponse));
        }
        private async Task <IEnumerable <BsonDocument> > ExecuteUsingQueryAsync(IConnectionSourceHandle connectionSource, ReadPreference readPreference, TimeSpan timeout, CancellationToken cancellationToken)
        {
            var indexes = new List <BsonDocument>();

            var systemIndexesCollection = _collectionNamespace.DatabaseNamespace.SystemIndexesCollection;
            var criteria  = new BsonDocument("ns", _collectionNamespace.FullName);
            var operation = new FindOperation <BsonDocument>(systemIndexesCollection, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Criteria = criteria
            };

            var cursor = await operation.ExecuteAsync(connectionSource, readPreference, timeout, cancellationToken).ConfigureAwait(false);

            while (await cursor.MoveNextAsync().ConfigureAwait(false))
            {
                var batch = cursor.Current;
                foreach (var index in batch)
                {
                    indexes.Add(index);
                }
            }

            return(indexes);
        }