Beispiel #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();
            }
        }
Beispiel #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);
        }
Beispiel #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();
            }
        }
Beispiel #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);
            }
        }
Beispiel #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);
 }
Beispiel #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);
            }
        }
Beispiel #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);
            }
        }
Beispiel #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);
            }
        }
Beispiel #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
        }
Beispiel #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));
 }
        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);
            }
        }
        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;
        }
 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;
     }
 }
 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);
 }
Beispiel #15
0
 private async Task CreateFilesCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateCreateFilesCollectionIndexesOperation();
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
 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;
     }
 }
Beispiel #17
0
        private void CreateFilesCollectionIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
        {
            var operation = CreateCreateFilesCollectionIndexesOperation();

            operation.Execute(binding, cancellationToken);
        }
 private void CreateFilesCollectionIndexes(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateCreateFilesCollectionIndexesOperation();
     operation.Execute(binding, cancellationToken);
 }
 private async Task CreateFilesCollectionIndexesAsync(IReadWriteBindingHandle binding, CancellationToken cancellationToken)
 {
     var operation = CreateCreateFilesCollectionIndexesOperation();
     await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false);
 }
        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
        }
        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);
            }
        }