コード例 #1
0
        private void EnsureIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            _ensureIndexesSemaphore.Wait(cancellationToken);
            try
            {
                if (!_ensureIndexesDone)
                {
                    var isFilesCollectionEmpty = IsFilesCollectionEmpty(binding, cancellationToken);
                    if (isFilesCollectionEmpty)
                    {
                        if (!FilesCollectionIndexesExist(binding, cancellationToken))
                        {
                            CreateFilesCollectionIndexes(binding, cancellationToken);
                        }
                        if (!ChunksCollectionIndexesExist(binding, cancellationToken))
                        {
                            CreateChunksCollectionIndexes(binding, cancellationToken);
                        }
                    }

                    _ensureIndexesDone = true;
                }
            }
            finally
            {
                _ensureIndexesSemaphore.Release();
            }
        }
コード例 #2
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,
                ReadConcern = GetReadConcern(),
                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);
        }
コード例 #3
0
        private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            await _ensureIndexesSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                if (!_ensureIndexesDone)
                {
                    var isFilesCollectionEmpty = await IsFilesCollectionEmptyAsync(binding, cancellationToken).ConfigureAwait(false);

                    if (isFilesCollectionEmpty)
                    {
                        if (!(await FilesCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
                        {
                            await CreateFilesCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
                        }
                        if (!(await ChunksCollectionIndexesExistAsync(binding, cancellationToken).ConfigureAwait(false)))
                        {
                            await CreateChunksCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
                        }
                    }

                    _ensureIndexesDone = true;
                }
            }
            finally
            {
                _ensureIndexesSemaphore.Release();
            }
        }
コード例 #4
0
        private bool IsFilesCollectionEmpty(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var operation = CreateIsFilesCollectionEmptyOperation();

            using (var cursor = operation.Execute(binding, cancellationToken))
            {
                var firstOrDefault = cursor.FirstOrDefault(cancellationToken);
                return(firstOrDefault == null);
            }
        }
コード例 #5
0
 private async Task CreateFilesCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var collectionNamespace = GetFilesCollectionNamespace();
     var requests            = new[] { new CreateIndexRequest(new BsonDocument {
             { "filename", 1 }, { "uploadDate", 1 }
         }) };
     var messageEncoderSettings = GetMessageEncoderSettings();
     var operation = new CreateIndexesOperation(collectionNamespace, requests, messageEncoderSettings);
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
コード例 #6
0
        private async Task <bool> IsFilesCollectionEmptyAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var operation = CreateIsFilesCollectionEmptyOperation();

            using (var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
            {
                var firstOrDefault = await cursor.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

                return(firstOrDefault == null);
            }
        }
コード例 #7
0
        private void EnsureIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var ensuredIndexes = Interlocked.CompareExchange(ref _ensuredIndexes, 0, 0);

            if (ensuredIndexes == 0)
            {
                var isFilesCollectionEmpty = IsFilesCollectionEmpty(binding, cancellationToken);
                if (isFilesCollectionEmpty)
                {
                    CreateFilesCollectionIndexes(binding, cancellationToken);
                    CreateChunksCollectionIndexes(binding, cancellationToken);
                }

                Interlocked.Exchange(ref _ensuredIndexes, 1);
            }
        }
コード例 #8
0
        private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var ensuredIndexes = Interlocked.CompareExchange(ref _ensuredIndexes, 0, 0);

            if (ensuredIndexes == 0)
            {
                var isFilesCollectionEmpty = await IsFilesCollectionEmptyAsync(binding, cancellationToken).ConfigureAwait(false);

                if (isFilesCollectionEmpty)
                {
                    await CreateFilesCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
                    await CreateChunksCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
                }

                Interlocked.Exchange(ref _ensuredIndexes, 1);
            }
        }
コード例 #9
0
        private GridFSUploadStream <TFileId> CreateUploadStream(IReadWriteBindingHandle binding, TFileId id, string filename, GridFSUploadOptions options)
        {
#pragma warning disable 618
            var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes;
            var batchSize      = options.BatchSize ?? (16 * 1024 * 1024 / chunkSizeBytes);

            return(new GridFSForwardOnlyUploadStream <TFileId>(
                       this,
                       binding.Fork(),
                       id,
                       filename,
                       options.Metadata,
                       options.Aliases,
                       options.ContentType,
                       chunkSizeBytes,
                       batchSize));

#pragma warning restore
        }
コード例 #10
0
 private RetryableWriteContext CreateRetryableWriteContext(IReadWriteBindingHandle readWriteBindingHandle, bool async)
 {
     return(async
             ? RetryableWriteContext.CreateAsync(readWriteBindingHandle, retryRequested: false, CancellationToken.None).GetAwaiter().GetResult()
             : RetryableWriteContext.Create(readWriteBindingHandle, retryRequested: false, CancellationToken.None));
 }
コード例 #11
0
        private async Task EnsureIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var ensuredIndexes = Interlocked.CompareExchange(ref _ensuredIndexes, 0, 0);
            if (ensuredIndexes == 0)
            {
                var isFilesCollectionEmpty = await IsFilesCollectionEmptyAsync(binding, cancellationToken).ConfigureAwait(false);
                if (isFilesCollectionEmpty)
                {
                    await CreateFilesCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
                    await CreateChunksCollectionIndexesAsync(binding, cancellationToken).ConfigureAwait(false);
                }

                Interlocked.Exchange(ref _ensuredIndexes, 1);
            }
        }
コード例 #12
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;
        }
コード例 #13
0
 private bool IsFilesCollectionEmpty(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateIsFilesCollectionEmptyOperation();
     using (var cursor = operation.Execute(binding, cancellationToken))
     {
         var firstOrDefault = cursor.FirstOrDefault(cancellationToken);
         return firstOrDefault == null;
     }
 }
コード例 #14
0
 private async Task CreateFilesCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var collectionNamespace = GetFilesCollectionNamespace();
     var requests = new[] { new CreateIndexRequest(new BsonDocument { { "filename", 1 }, { "uploadDate", 1 } }) };
     var messageEncoderSettings = GetMessageEncoderSettings();
     var operation = new CreateIndexesOperation(collectionNamespace, requests, messageEncoderSettings);
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
コード例 #15
0
 private async Task CreateFilesCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateCreateFilesCollectionIndexesOperation();
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
コード例 #16
0
 private async Task<bool> IsFilesCollectionEmptyAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateIsFilesCollectionEmptyOperation();
     using (var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
     {
         var firstOrDefault = await cursor.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
         return firstOrDefault == null;
     }
 }
コード例 #17
0
        private void CreateFilesCollectionIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var operation = CreateCreateFilesCollectionIndexesOperation();

            operation.Execute(binding, cancellationToken);
        }
コード例 #18
0
 private void CreateFilesCollectionIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateCreateFilesCollectionIndexesOperation();
     operation.Execute(binding, cancellationToken);
 }
コード例 #19
0
 private async Task CreateFilesCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateCreateFilesCollectionIndexesOperation();
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
コード例 #20
0
        private GridFSUploadStream CreateUploadStream(IReadWriteBindingHandle binding, string filename, GridFSUploadOptions options)
        {
#pragma warning disable 618
            var id = ObjectId.GenerateNewId();
            var chunkSizeBytes = options.ChunkSizeBytes ?? _options.ChunkSizeBytes;
            var batchSize = options.BatchSize ?? (16 * 1024 * 1024 / chunkSizeBytes);

            return new GridFSForwardOnlyUploadStream(
                this,
                binding.Fork(),
                id,
                filename,
                options.Metadata,
                options.Aliases,
                options.ContentType,
                chunkSizeBytes,
                batchSize);
#pragma warning restore
        }
コード例 #21
0
        private void EnsureIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var ensuredIndexes = Interlocked.CompareExchange(ref _ensuredIndexes, 0, 0);
            if (ensuredIndexes == 0)
            {
                var isFilesCollectionEmpty = IsFilesCollectionEmpty(binding, cancellationToken);
                if (isFilesCollectionEmpty)
                {
                    CreateFilesCollectionIndexes(binding, cancellationToken);
                    CreateChunksCollectionIndexes(binding, cancellationToken);
                }

                Interlocked.Exchange(ref _ensuredIndexes, 1);
            }
        }