public void CreateWrappedQuery_should_create_the_correct_query_when_not_connected_to_a_shard_router()
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Comment = "funny",
                Filter = BsonDocument.Parse("{x: 1}"),
                MaxTime = TimeSpan.FromSeconds(20),
                Modifiers = BsonDocument.Parse("{$comment: \"notfunny\", $snapshot: true}"),
                Projection = BsonDocument.Parse("{y: 1}"),
                Sort = BsonDocument.Parse("{a: 1}")
            };

            var expectedResult = new BsonDocument
            {
                { "$query", BsonDocument.Parse("{x: 1}") },
                { "$orderby", BsonDocument.Parse("{a: 1}") },
                { "$comment", "funny" },
                { "$maxTimeMS", 20000 },
                { "$snapshot", true }
            };


            var result = subject.CreateWrappedQuery(ServerType.ReplicaSetArbiter, ReadPreference.Secondary);

            result.Should().Be(expectedResult);
        }
Ejemplo n.º 2
0
        internal static IEnumerable<IFileSystemInformation> EnumerateChildrenInternal(
            string directory,
            ChildType childType,
            string searchPattern,
            System.IO.SearchOption searchOption,
            FileAttributes excludeAttributes,
            IFileService fileService)
        {
            // We want to be able to see all files as we recurse and open new find handles (that might be over MAX_PATH).
            // We've already normalized our base directory.
            string extendedDirectory = Paths.AddExtendedPrefix(directory);

            // The assertion here is that we want to find files that match the desired pattern in all subdirectories, even if the
            // subdirectories themselves don't match the pattern. That requires two passes to avoid overallocating for directories
            // with a large number of files.

            // First look for items that match the given search pattern in the current directory
            using (FindOperation findOperation = new FindOperation(Paths.Combine(extendedDirectory, searchPattern)))
            {
                FindResult findResult;
                while ((findResult = findOperation.GetNextResult()) != null)
                {
                    bool isDirectory = (findResult.Attributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY) == FileAttributes.FILE_ATTRIBUTE_DIRECTORY;

                    if ((findResult.Attributes & excludeAttributes) == 0
                        && findResult.FileName != "."
                        && findResult.FileName != ".."
                        && ((isDirectory && childType == ChildType.Directory)
                            || (!isDirectory && childType == ChildType.File)))
                    {
                        yield return FileSystemInformation.Create(findResult, directory, fileService);
                    }
                }
            }

            if (searchOption != System.IO.SearchOption.AllDirectories) yield break;

            // Now recurse into each subdirectory
            using (FindOperation findOperation = new FindOperation(Paths.Combine(extendedDirectory, "*"), directoriesOnly: true))
            {
                FindResult findResult;
                while ((findResult = findOperation.GetNextResult()) != null)
                {
                    // Unfortunately there is no guarantee that the API will return only directories even if we ask for it
                    bool isDirectory = (findResult.Attributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY) == FileAttributes.FILE_ATTRIBUTE_DIRECTORY;

                    if ((findResult.Attributes & excludeAttributes) == 0
                        && isDirectory
                        && findResult.FileName != "."
                        && findResult.FileName != "..")
                    {
                        foreach (var child in EnumerateChildrenInternal(Paths.Combine(directory, findResult.FileName), childType, searchPattern,
                            searchOption, excludeAttributes, fileService))
                        {
                            yield return child;
                        }
                    }
                }
            }
        }
        public void Constructor_should_initialize_object()
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.CollectionNamespace.Should().Be(_collectionNamespace);
            subject.ResultSerializer.Should().NotBeNull();
            subject.MessageEncoderSettings.Should().BeEquivalentTo(_messageEncoderSettings);
        }
        public void BatchSize_set_should_throw_when_value_is_invalid(
            [Values(-1)]
            int value)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            Action action = () => subject.BatchSize = value;

            action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("value");
        }
        public void BatchSize_set_should_throw_when_value_is_invalid(
            [Values(-1)]
            int value)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var exception = Record.Exception(() => { subject.BatchSize = value; });

            var argumentOutOfRangeException = exception.Should().BeOfType<ArgumentOutOfRangeException>().Subject;
            argumentOutOfRangeException.ParamName.Should().Be("value");
        }
        public void BatchSize_get_and_set_should_work(
            [Values(null, 0, 1)]
            int? value)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.BatchSize = value;
            var result = subject.BatchSize;

            result.Should().Be(value);
        }
        public void AllowPartialResults_get_and_set_should_work(
            [Values(null, false, true)]
            bool? value)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.AllowPartialResults = value;
            var result = subject.AllowPartialResults;

            result.Should().Be(value);
        }
Ejemplo n.º 8
0
        private BsonDocument FindUser(IChannelSourceHandle channelSource, ICoreSessionHandle session, CollectionNamespace collectionNamespace, CancellationToken cancellationToken)
        {
            var operation = new FindOperation <BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = new BsonDocument("user", _username),
                Limit  = -1
            };
            var cursor        = operation.Execute(channelSource, ReadPreference.Primary, session, cancellationToken);
            var userDocuments = cursor.ToList();

            return(userDocuments.FirstOrDefault());
        }
Ejemplo n.º 9
0
        // methods
        public async Task <IEnumerable <BsonDocument> > ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        {
            var collectionNamespace = new CollectionNamespace(_databaseNamespace, "system.users");
            var filter    = _username == null ? new BsonDocument() : new BsonDocument("user", _username);
            var operation = new FindOperation <BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = filter
            };
            var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);

            return(await cursor.ToListAsync(cancellationToken).ConfigureAwait(false));
        }
        public void Collation_get_and_set_should_work(
            [Values(null, "en_US", "fr_CA")]
            string locale)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value = locale == null ? null : new Collation(locale);

            subject.Collation = value;
            var result = subject.Collation;

            result.Should().Be(value);
        }
        public void CollectionNamespace_get_should_return_expected_result(
            [Values("a", "b")]
            string collectionName)
        {
            var databaseNamespace = new DatabaseNamespace("test");
            var collectionNamespace = new CollectionNamespace(databaseNamespace, collectionName);
            var subject = new FindOperation<BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            var result = subject.CollectionNamespace;

            result.Should().Be(collectionNamespace);
        }
        // methods
        public override async Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(PipelineDefinition <TDocument, TResult> pipeline, AggregateOptions options, CancellationToken cancellationToken)
        {
            var renderedPipeline = Ensure.IsNotNull(pipeline, "pipeline").Render(_documentSerializer, _settings.SerializerRegistry);

            options = options ?? new AggregateOptions();

            var last = renderedPipeline.Documents.LastOrDefault();

            if (last != null && last.GetElement(0).Name == "$out")
            {
                var operation = new AggregateToCollectionOperation(
                    _collectionNamespace,
                    renderedPipeline.Documents,
                    _messageEncoderSettings)
                {
                    AllowDiskUse = options.AllowDiskUse,
                    MaxTime      = options.MaxTime
                };

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

                var outputCollectionName = last.GetElement(0).Value.AsString;

                var findOperation = new FindOperation <TResult>(
                    new CollectionNamespace(_collectionNamespace.DatabaseNamespace, outputCollectionName),
                    renderedPipeline.OutputSerializer,
                    _messageEncoderSettings)
                {
                    BatchSize = options.BatchSize,
                    MaxTime   = options.MaxTime
                };

                // we want to delay execution of the find because the user may
                // not want to iterate the results at all...
                return(await Task.FromResult <IAsyncCursor <TResult> >(new DeferredAsyncCursor <TResult>(ct => ExecuteReadOperation(findOperation, ReadPreference.Primary, ct))).ConfigureAwait(false));
            }
            else
            {
                var aggregateOperation = new AggregateOperation <TResult>(
                    _collectionNamespace,
                    renderedPipeline.Documents,
                    renderedPipeline.OutputSerializer,
                    _messageEncoderSettings)
                {
                    AllowDiskUse = options.AllowDiskUse,
                    BatchSize    = options.BatchSize,
                    MaxTime      = options.MaxTime,
                    UseCursor    = options.UseCursor
                };
                return(await ExecuteReadOperation(aggregateOperation, cancellationToken).ConfigureAwait(false));
            }
        }
Ejemplo n.º 13
0
        private async Task <BsonDocument> FindUserAsync(IChannelSourceHandle channelSource, CollectionNamespace collectionNamespace, CancellationToken cancellationToken)
        {
            var operation = new FindOperation <BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = new BsonDocument("user", _username),
                Limit  = -1
            };
            var cursor = await operation.ExecuteAsync(channelSource, ReadPreference.Primary, cancellationToken).ConfigureAwait(false);

            var userDocuments = await cursor.ToListAsync().ConfigureAwait(false);

            return(userDocuments.FirstOrDefault());
        }
Ejemplo n.º 14
0
        // methods
        public IEnumerable <BsonDocument> Execute(IReadBinding binding, CancellationToken cancellationToken)
        {
            var collectionNamespace = new CollectionNamespace(_databaseNamespace, "system.users");
            var filter    = _username == null ? new BsonDocument() : new BsonDocument("user", _username);
            var operation = new FindOperation <BsonDocument>(collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter         = filter,
                RetryRequested = false
            };
            var cursor = operation.Execute(binding, cancellationToken);

            return(cursor.ToList(cancellationToken));
        }
Ejemplo n.º 15
0
        // private methods
        private async Task GetChunkAsync(long n, CancellationToken cancellationToken)
        {
            var chunksCollectionNamespace = Bucket.GetChunksCollectionNamespace();
            var messageEncoderSettings    = Bucket.GetMessageEncoderSettings();

#pragma warning disable 618
            var filter = new BsonDocument
            {
                { "files_id", FilesCollectionDocument.IdAsBsonValue },
                { "n", n }
            };
#pragma warning restore

            var operation = new FindOperation <BsonDocument>(
                chunksCollectionNamespace,
                BsonDocumentSerializer.Instance,
                messageEncoderSettings)
            {
                Filter = filter,
                Limit  = -1
            };

            using (var cursor = await operation.ExecuteAsync(Binding, cancellationToken).ConfigureAwait(false))
            {
                var documents = await cursor.ToListAsync();

                if (documents.Count == 0)
                {
#pragma warning disable 618
                    throw new GridFSChunkException(FilesCollectionDocument.IdAsBsonValue, n, "missing");
#pragma warning restore
                }

                var document = documents[0];
                var data     = document["data"].AsBsonBinaryData.Bytes;

                var chunkSizeBytes    = FilesCollectionDocument.ChunkSizeBytes;
                var lastChunk         = 0;
                var expectedChunkSize = n == lastChunk ? FilesCollectionDocument.Length % chunkSizeBytes : chunkSizeBytes;
                if (data.Length != expectedChunkSize)
                {
#pragma warning disable 618
                    throw new GridFSChunkException(FilesCollectionDocument.IdAsBsonValue, n, "the wrong size");
#pragma warning restore
                }

                _chunk = data;
                _n     = n;
            }
        }
Ejemplo n.º 16
0
        private async Task <IAsyncCursor <BsonDocument> > Query(IReadBinding binding, BsonDocument filter)
        {
            var findOp = new FindOperation <BsonDocument>(_collection, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = filter,
                Limit  = -1
            };

            using (var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
                using (var linked = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, _cancellationTokenSource.Token))
                {
                    return(await findOp.ExecuteAsync(binding, linked.Token));
                }
        }
Ejemplo n.º 17
0
        public static void DeleteDirectoryRecursive(string path)
        {
            var data = FileMethods.TryGetFileInfo(path);

            if (!data.HasValue)
            {
                // Nothing found
                throw ErrorHelper.GetIoExceptionForError(WindowsError.ERROR_PATH_NOT_FOUND, path);
            }

            if ((data.Value.Attributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY) != FileAttributes.FILE_ATTRIBUTE_DIRECTORY)
            {
                // Not a directory, a file
                throw ErrorHelper.GetIoExceptionForError(WindowsError.ERROR_FILE_EXISTS, path);
            }

            if ((data.Value.Attributes & FileAttributes.FILE_ATTRIBUTE_READONLY) == FileAttributes.FILE_ATTRIBUTE_READONLY)
            {
                // Make it writable
                FileMethods.SetFileAttributes(path, data.Value.Attributes & ~FileAttributes.FILE_ATTRIBUTE_READONLY);
            }

            // Reparse points don't need to be empty to be deleted. Deleting will simply disconnect the reparse point, which is what we want.
            if ((data.Value.Attributes & FileAttributes.FILE_ATTRIBUTE_REPARSE_POINT) != FileAttributes.FILE_ATTRIBUTE_REPARSE_POINT)
            {
                using (FindOperation findOperation = new FindOperation(Paths.Combine(path, "*")))
                {
                    FindResult findResult;
                    while ((findResult = findOperation.GetNextResult()) != null)
                    {
                        if (findResult.FileName != "." && findResult.FileName != "..")
                        {
                            bool isDirectory = (findResult.Attributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY) == FileAttributes.FILE_ATTRIBUTE_DIRECTORY;

                            if (isDirectory)
                            {
                                DeleteDirectoryRecursive(Paths.Combine(path, findResult.FileName));
                            }
                            else
                            {
                                FileMethods.DeleteFile(Paths.Combine(path, findResult.FileName));
                            }
                        }
                    }
                }
            }

            // We've either emptied or we're a reparse point, delete the directory
            DirectoryMethods.RemoveDirectory(path);
        }
Ejemplo n.º 18
0
        public void OpenFileWithTrailingSeparator()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string testFile = cleaner.CreateTestFile(nameof(OpenFileWithTrailingSeparator));

                string fullName = Storage.GetFullPathName(Paths.AddTrailingSeparator(testFile));

                FindOperation <string> find = new FindOperation <string>(testFile);
                Action action = () => find.FirstOrDefault();
                action.Should().Throw <ArgumentException>().And.HResult.Should().Be((int)WindowsError.ERROR_INVALID_PARAMETER.ToHResult());

                action = () => Storage.CreateFile(Paths.AddTrailingSeparator(testFile), CreationDisposition.OpenExisting, DesiredAccess.ReadAttributes);
                action.Should().Throw <WInteropIOException>().And.HResult.Should().Be((int)WindowsError.ERROR_INVALID_NAME.ToHResult());
            }
        }
        protected override void Given()
        {
            _subject = new FindOperation <BsonDocument>(
                CollectionNamespace,
                BsonDocumentSerializer.Instance,
                MessageEncoderSettings);

            Insert(new[]
            {
                BsonDocument.Parse("{_id: 1, x: 1}"),
                BsonDocument.Parse("{_id: 2, x: 2}"),
                BsonDocument.Parse("{_id: 3, x: 3}"),
                BsonDocument.Parse("{_id: 4, x: 4}"),
                BsonDocument.Parse("{_id: 5, x: 5}"),
                BsonDocument.Parse("{_id: 6, x: 6}"),
            });
        }
Ejemplo n.º 20
0
        // methods
        public async Task <IAsyncEnumerable <TResult> > AggregateAsync <TResult>(AggregateModel <TResult> model, TimeSpan?timeout, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(model, "model");

            var pipeline = model.Pipeline.Select(x => ConvertToBsonDocument(x)).ToList();

            var last = pipeline.LastOrDefault();

            if (last != null && last.GetElement(0).Name == "$out")
            {
                var operation = new AggregateToCollectionOperation(
                    _collectionNamespace,
                    pipeline,
                    _messageEncoderSettings)
                {
                    AllowDiskUse = model.AllowDiskUse,
                    MaxTime      = model.MaxTime
                };

                await ExecuteWriteOperation(operation, timeout, cancellationToken);

                var outputCollectionName = last.GetElement(0).Value.AsString;
                var findOperation        = new FindOperation <TResult>(
                    new CollectionNamespace(_collectionNamespace.DatabaseNamespace, outputCollectionName),
                    model.ResultSerializer ?? _settings.SerializerRegistry.GetSerializer <TResult>(),
                    _messageEncoderSettings)
                {
                    BatchSize = model.BatchSize,
                    MaxTime   = model.MaxTime
                };

                return(await Task.FromResult <IAsyncEnumerable <TResult> >(new AsyncCursorAsyncEnumerable <TResult>(
                                                                               () => ExecuteReadOperation(findOperation, timeout, cancellationToken),
                                                                               null)));
            }
            else
            {
                var operation = CreateAggregateOperation <TResult>(model, pipeline);

                return(await Task.FromResult <IAsyncEnumerable <TResult> >(new AsyncCursorAsyncEnumerable <TResult>(
                                                                               () => ExecuteReadOperation(operation, timeout, cancellationToken),
                                                                               null)));
            }
        }
Ejemplo n.º 21
0
        public async Task <IAsyncCursor <TResult> > AggregateAsync <TResult>(IEnumerable <object> pipeline, AggregateOptions <TResult> options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(pipeline, "pipeline");

            options = options ?? new AggregateOptions <TResult>();
            var pipelineDocuments = pipeline.Select(x => ConvertToBsonDocument(x)).ToList();

            var last = pipelineDocuments.LastOrDefault();

            if (last != null && last.GetElement(0).Name == "$out")
            {
                var operation = new AggregateToCollectionOperation(
                    _collectionNamespace,
                    pipelineDocuments,
                    _messageEncoderSettings)
                {
                    AllowDiskUse = options.AllowDiskUse,
                    MaxTime      = options.MaxTime
                };

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

                var outputCollectionName = last.GetElement(0).Value.AsString;
                var resultSerializer     = ResolveResultSerializer(options.ResultSerializer);

                var findOperation = new FindOperation <TResult>(
                    new CollectionNamespace(_collectionNamespace.DatabaseNamespace, outputCollectionName),
                    resultSerializer,
                    _messageEncoderSettings)
                {
                    BatchSize = options.BatchSize,
                    MaxTime   = options.MaxTime
                };

                // we want to delay execution of the find because the user may
                // not want to iterate the results at all...
                return(await Task.FromResult <IAsyncCursor <TResult> >(new DeferredAsyncCursor <TResult>(ct => ExecuteReadOperation(findOperation, ReadPreference.Primary, ct))).ConfigureAwait(false));
            }
            else
            {
                var operation = CreateAggregateOperation <TResult>(options, pipelineDocuments);
                return(await ExecuteReadOperation(operation, cancellationToken).ConfigureAwait(false));
            }
        }
Ejemplo n.º 22
0
        public void FindRecursive()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string subdirA = Paths.Combine(cleaner.TempFolder, "A");
                Storage.CreateDirectory(subdirA);
                string fileB = Paths.Combine(subdirA, "B");
                FileHelper.WriteAllText(fileB, "B file");
                string subdirC = Paths.Combine(subdirA, "C");
                Storage.CreateDirectory(subdirC);
                string subdirD = Paths.Combine(subdirA, "D");
                Storage.CreateDirectory(subdirD);
                string fileE = Paths.Combine(subdirD, "E");
                FileHelper.WriteAllText(fileE, "E file");

                var files = new FindOperation <string>(subdirA, recursive: true).ToArray();
                files.Should().BeEquivalentTo(System.IO.Directory.GetFileSystemEntries(subdirA, "*", System.IO.SearchOption.AllDirectories));
            }
        }
Ejemplo n.º 23
0
        private IAsyncCursor <BsonDocument> CreateAndRunFindOperation(RetryableReadContext context, bool async)
        {
            var findOperation = new FindOperation <BsonDocument>(
                _collectionNamespace,
                BsonDocumentSerializer.Instance,
                _messageEncoderSettings)
            {
                BatchSize = 1
            };

            if (async)
            {
                return(findOperation.ExecuteAsync(context, CancellationToken.None).GetAwaiter().GetResult());
            }
            else
            {
                return(findOperation.Execute(context, CancellationToken.None));
            }
        }
Ejemplo n.º 24
0
        internal static IEnumerable <IFileSystemInformation> EnumerateChildrenInternal(
            string directory,
            ChildType childType,
            string searchPattern,
            System.IO.SearchOption searchOption,
            FileAttributes excludeAttributes,
            IFileService fileService)
        {
            // We want to be able to see all files as we recurse and open new find handles (that might be over MAX_PATH).
            // We've already normalized our base directory.
            string extendedDirectory = Paths.AddExtendedPrefix(directory);

            var transformFilter = new FindTransformFilter(excludeAttributes, fileService);
            FindOperation <IFileSystemInformation> findOperation = new FindOperation <IFileSystemInformation>(
                extendedDirectory,
                searchPattern,
                recursive: searchOption == System.IO.SearchOption.AllDirectories ? true : false,
                transformFilter,
                transformFilter);

            return(findOperation);
        }
Ejemplo n.º 25
0
        // private methods
        private async Task GetFirstBatchAsync(CancellationToken cancellationToken)
        {
            var chunksCollectionNamespace = Bucket.GetChunksCollectionNamespace();
            var messageEncoderSettings    = Bucket.GetMessageEncoderSettings();

#pragma warning disable 618
            var filter = new BsonDocument("files_id", FileInfo.IdAsBsonValue);
#pragma warning restore
            var sort = new BsonDocument("n", 1);

            var operation = new FindOperation <BsonDocument>(
                chunksCollectionNamespace,
                BsonDocumentSerializer.Instance,
                messageEncoderSettings)
            {
                Filter = filter,
                Sort   = sort
            };

            _cursor = await operation.ExecuteAsync(Binding, cancellationToken).ConfigureAwait(false);
            await GetNextBatchAsync(cancellationToken).ConfigureAwait(false);
        }
Ejemplo n.º 26
0
        private async Task <bool> IsFilesCollectionEmptyAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var filesCollectionNamespace = GetFilesCollectionNamespace();
            var messageEncoderSettings   = GetMessageEncoderSettings();
            var operation = new FindOperation <BsonDocument>(filesCollectionNamespace, BsonDocumentSerializer.Instance, messageEncoderSettings)
            {
                Limit      = 1,
                Projection = new BsonDocument("_id", 1)
            };

            using (var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
            {
                if (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
                {
                    var batch = cursor.Current;
                    if (batch.Count() > 0)
                    {
                        return(false);
                    }
                }
            }

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

            subject.CollectionNamespace.Should().Be(_collectionNamespace);
            subject.ResultSerializer.Should().NotBeNull();
            subject.MessageEncoderSettings.Should().BeEquivalentTo(_messageEncoderSettings);

            subject.AllowPartialResults.Should().NotHaveValue();
            subject.BatchSize.Should().NotHaveValue();
            subject.Comment.Should().BeNull();
            subject.CursorType.Should().Be(CursorType.NonTailable);
            subject.Filter.Should().BeNull();
            subject.FirstBatchSize.Should().NotHaveValue();
            subject.Hint.Should().BeNull();
            subject.Limit.Should().NotHaveValue();
            subject.Max.Should().BeNull();
            subject.MaxScan.Should().NotHaveValue();
            subject.MaxTime.Should().NotHaveValue();
            subject.Min.Should().BeNull();
            subject.Modifiers.Should().BeNull();
            subject.NoCursorTimeout.Should().NotHaveValue();
            subject.OplogReplay.Should().NotHaveValue();
            subject.Projection.Should().BeNull();
            subject.ReadConcern.Should().Be(ReadConcern.Default);
            subject.ResultSerializer.Should().Be(BsonDocumentSerializer.Instance);
            subject.ReturnKey.Should().NotHaveValue();
            subject.ShowRecordId.Should().NotHaveValue();
            subject.SingleBatch.Should().NotHaveValue();
            subject.Skip.Should().NotHaveValue();
            subject.Snapshot.Should().NotHaveValue();
            subject.Sort.Should().BeNull();
        }
Ejemplo n.º 28
0
        private async Task<GridFSFileInfo> GetFileInfoByNameAsync(IReadBindingHandle binding, string filename, int revision, CancellationToken cancellationToken)
        {
            var collectionNamespace = GetFilesCollectionNamespace();
            var serializerRegistry = _database.Settings.SerializerRegistry;
            var fileInfoSerializer = serializerRegistry.GetSerializer<GridFSFileInfo>();
            var messageEncoderSettings = GetMessageEncoderSettings();
            var filter = new BsonDocument("filename", filename);
            var skip = revision >= 0 ? revision : -revision - 1;
            var limit = 1;
            var sort = new BsonDocument("uploadDate", revision >= 0 ? 1 : -1);

            var operation = new FindOperation<GridFSFileInfo>(
                collectionNamespace,
                fileInfoSerializer,
                messageEncoderSettings)
            {
                Filter = filter,
                Limit = limit,
                Skip = skip,
                Sort = sort
            };

            using (var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
            {
                var fileInfoList = await cursor.ToListAsync(cancellationToken).ConfigureAwait(false);
                var fileInfo = fileInfoList.FirstOrDefault();
                if (fileInfo == null)
                {
                    throw new GridFSFileNotFoundException(filename, revision);
                }
                return fileInfo;
            }
        }
        public void Sort_get_and_set_should_work(
            [Values(null, "{ a : 1 }", "{ b : -1 }")]
            string json)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value = json == null ? null : BsonDocument.Parse(json);

            subject.Sort = value;
            var result = subject.Sort;

            result.Should().Be(value);
        }
        public void Comment_get_and_set_should_work(
            [Values(null, "a", "b")]
            string value)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.Comment = value;
            var result = subject.Comment;

            result.Should().Be(value);
        }
        public void ResultSerializer_get_should_return_expected_result()
        {
            var resultSerializer = new BsonDocumentSerializer();
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, resultSerializer, _messageEncoderSettings);

            var result = subject.ResultSerializer;

            result.Should().Be(resultSerializer);
        }
        public void CreateFindOpcodeOperation_should_return_expected_result_when_singleBatch_is_true()
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            subject.Limit = 1;
            subject.SingleBatch = true;

            var result = subject.CreateFindOpcodeOperation();

            result.Limit.Should().Be(-subject.Limit);
        }
        public void CursorType_get_and_set_should_work(
            [Values(CursorType.NonTailable, CursorType.Tailable)]
            CursorType value)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.CursorType = value;
            var result = subject.CursorType;

            result.Should().Be(value);
        }
        public void CreateFindOpcodeOperation_should_return_expected_result()
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            subject.AllowPartialResults = true;
            subject.BatchSize = 1;
            subject.Comment = "comment";
            subject.CursorType = CursorType.Tailable;
            subject.Filter = new BsonDocument("filter", 1);
            subject.FirstBatchSize = 2;
            subject.Hint = "x_1";
            subject.Limit = 3;
            subject.Max = new BsonDocument("max", 1);
            subject.MaxScan = 4;
            subject.MaxTime = TimeSpan.FromSeconds(1);
            subject.Min = new BsonDocument("min", 1);
            subject.NoCursorTimeout = true;
            subject.OplogReplay = true;
            subject.Projection = new BsonDocument("projection", 1);
            subject.ReadConcern = ReadConcern.Local;
            subject.ReturnKey = true;
            subject.ShowRecordId = true;
            subject.SingleBatch = false;
            subject.Skip = 6;
            subject.Snapshot = true;
            subject.Sort = new BsonDocument("sort", 1);

            var result = subject.CreateFindOpcodeOperation();

            result.AllowPartialResults.Should().Be(subject.AllowPartialResults);
            result.BatchSize.Should().Be(subject.BatchSize);
            result.CollectionNamespace.Should().Be(subject.CollectionNamespace);
            result.Comment.Should().Be(subject.Comment);
            result.CursorType.Should().Be(subject.CursorType);
            result.Filter.Should().Be(subject.Filter);
            result.FirstBatchSize.Should().Be(subject.FirstBatchSize);
            result.Hint.Should().Be(subject.Hint);
            result.Limit.Should().Be(subject.Limit);
            result.Max.Should().Be(subject.Max);
            result.MaxScan.Should().Be(subject.MaxScan);
            result.MaxTime.Should().Be(subject.MaxTime);
            result.MessageEncoderSettings.Should().BeSameAs(subject.MessageEncoderSettings);
            result.Min.Should().Be(subject.Min);
            result.Modifiers.Should().Be(subject.Modifiers);
            result.NoCursorTimeout.Should().Be(subject.NoCursorTimeout);
            result.OplogReplay.Should().Be(subject.OplogReplay);
            result.Projection.Should().Be(subject.Projection);
            result.ResultSerializer.Should().Be(subject.ResultSerializer);
            result.ShowRecordId.Should().Be(subject.ShowRecordId);
            result.Skip.Should().Be(subject.Skip);
            result.Snapshot.Should().Be(subject.Snapshot);
            result.Sort.Should().Be(subject.Sort);
        }
Ejemplo n.º 35
0
        private async Task<IAsyncCursor<BsonDocument>> Query(IReadBinding binding, BsonDocument filter)
        {
            var findOp = new FindOperation<BsonDocument>(_collection, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Filter = filter,
                Limit = -1
            };

            using (var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
            using (var linked = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, _cancellationTokenSource.Token))
            {
                return await findOp.ExecuteAsync(binding, linked.Token);
            }
        }
Ejemplo n.º 36
0
        public void findOperationTest()
        {
            var f = new FindOperation();

            Assert.AreEqual("0 + 8 - 1 + 90 - 45", f.findOperation(new[] { 0, 8, 1, 90, 45 }, 52));
        }
        public void Execute_should_find_all_the_documents_matching_the_query_when_split_across_batches(
            [Values(false, true)]
            bool async)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                BatchSize = 2
            };

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

            result.Should().HaveCount(5);
        }
Ejemplo n.º 38
0
        private async Task<bool> IsFilesCollectionEmptyAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var filesCollectionNamespace = GetFilesCollectionNamespace();
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new FindOperation<BsonDocument>(filesCollectionNamespace, BsonDocumentSerializer.Instance, messageEncoderSettings)
            {
                Limit = 1,
                SingleBatch = true,
                Projection = new BsonDocument("_id", 1)
            };

            using (var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
            {
                if (await cursor.MoveNextAsync(cancellationToken).ConfigureAwait(false))
                {
                    var batch = cursor.Current;
                    if (batch.Count() > 0)
                    {
                        return false;
                    }
                }
            }

            return true;
        }
        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");
        }
        public void Execute_should_raise_an_error_when_an_unsupported_read_concern_is_specified(
            [Values(false, true)]
            bool async)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                ReadConcern = ReadConcern.Majority
            };

            Action act = () => ExecuteOperation(subject, async);
            act.ShouldThrow<MongoClientException>();
        }
        public void Execute_should_find_documents_matching_options(
            [Values(false, true)]
            bool async)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                Comment = "funny",
                Filter = BsonDocument.Parse("{ y : 1 }"),
                Limit = 4,
                MaxTime = TimeSpan.FromSeconds(20),
                Projection = BsonDocument.Parse("{ y : 1 }"),
                Skip = 1,
                Sort = BsonDocument.Parse("{ _id : -1 }")
            };

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

            result.Should().HaveCount(1);
        }
        public void MaxTime_get_and_set_should_work(
            [Values(null, 1)]
            int? seconds)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
            var value = seconds == null ? (TimeSpan?)null : TimeSpan.FromSeconds(seconds.Value);

            subject.MaxTime = value;
            var result = subject.MaxTime;

            result.Should().Be(value);
        }
        public void MessageEncoderSettings_get_should_return_expected_result(
            [Values(GuidRepresentation.CSharpLegacy, GuidRepresentation.Standard)]
            GuidRepresentation guidRepresentation)
        {
            var messageEncoderSettings = new MessageEncoderSettings { { "GuidRepresentation", guidRepresentation } };
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, messageEncoderSettings);

            var result = subject.MessageEncoderSettings;

            result.Should().BeEquivalentTo(messageEncoderSettings);
        }
Ejemplo n.º 44
0
 private string Find(FindOperation op, HtmlDocument doc)
 {
     return op(doc);
 }
        public void ReadConcern_get_and_set_should_work()
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.ReadConcern = ReadConcern.Local;
            var result = subject.ReadConcern;

            result.Should().Be(ReadConcern.Local);
        }
        public void Limit_get_and_set_should_work(
            [Values(-2, -1, 0, 1, 2)]
            int? value)
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);

            subject.Limit = value;
            var result = subject.Limit;

            result.Should().Be(value);
        }
        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));
            }
        }
        public void CreateFindCommandOperation_should_return_expected_result_when_modifiers_are_provided()
        {
            var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings)
            {
                ReadConcern = ReadConcern.Majority
            };
            subject.Modifiers = new BsonDocument
            {
                { "$hint", "x_1" },
                { "$max", new BsonDocument("max", 1) },
                { "$maxScan", 1 },
                { "$maxTimeMS", 2000 },
                { "$min", new BsonDocument("min", 1) },
                { "$orderby", new BsonDocument("sort", 1) },
                { "$showDiskLoc", true },
                { "$snapshot", true }
            };

            var result = subject.CreateFindCommandOperation();

            result.Hint.Should().Be(subject.Modifiers["$hint"]);
            result.Max.Should().Be(subject.Modifiers["$max"].AsBsonDocument);
            result.MaxScan.Should().Be(subject.Modifiers["$maxScan"].AsInt32);
            result.MaxTime.Should().Be(TimeSpan.FromMilliseconds(subject.Modifiers["$maxTimeMS"].AsInt32));
            result.Min.Should().Be(subject.Modifiers["$min"].AsBsonDocument);
            result.ReadConcern.Should().Be(subject.ReadConcern);
            result.ShowRecordId.Should().Be(subject.Modifiers["$showDiskLoc"].AsBoolean);
            result.Snapshot.Should().Be(subject.Modifiers["$snapshot"].AsBoolean);
            result.Sort.Should().Be(subject.Modifiers["$orderby"].AsBsonDocument);
        }
        // private methods
        private async Task GetFirstBatchAsync(CancellationToken cancellationToken)
        {
            var chunksCollectionNamespace = Bucket.GetChunksCollectionNamespace();
            var messageEncoderSettings = Bucket.GetMessageEncoderSettings();
#pragma warning disable 618
            var filter = new BsonDocument("files_id", FilesCollectionDocument.IdAsBsonValue);
#pragma warning restore
            var sort = new BsonDocument("n", 1);

            var operation = new FindOperation<BsonDocument>(
                chunksCollectionNamespace,
                BsonDocumentSerializer.Instance,
                messageEncoderSettings)
            {
                Filter = filter,
                Sort = sort
            };

            _cursor = await operation.ExecuteAsync(Binding, cancellationToken).ConfigureAwait(false);
            await GetNextBatchAsync(cancellationToken).ConfigureAwait(false);
        }