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

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