예제 #1
0
        private async Task DownloadToStreamHelperAsync(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            var retryReads = _database.Client.Settings.RetryReads;

            using (var source = new GridFSForwardOnlyDownloadStream <TFileId>(this, binding.Fork(), fileInfo, checkMD5)
            {
                RetryReads = retryReads
            })
            {
                var count  = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    count -= partialCount;
                }

                await source.CloseAsync(cancellationToken).ConfigureAwait(false);
            }
        }
예제 #2
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);
            }
        }
예제 #3
0
 // constructors
 public Request(ServerDescription serverDescription, MongoServerInstance serverInstance, IReadBindingHandle binding)
 {
     _serverDescription = serverDescription;
     _serverInstance    = serverInstance;
     _binding           = binding;
     _nestingLevel      = 1;
 }
예제 #4
0
        private async Task <GridFSFileInfo> GetFileInfoAsync(IReadBindingHandle binding, BsonValue id, CancellationToken cancellationToken)
        {
            var filesCollectionNamespace = GetFilesCollectionNamespace();
            var serializerRegistry       = _database.Settings.SerializerRegistry;
            var fileInfoSerializer       = serializerRegistry.GetSerializer <GridFSFileInfo>();
            var messageEncoderSettings   = GetMessageEncoderSettings();
            var filter = new BsonDocument("_id", id);

            var operation = new FindOperation <GridFSFileInfo>(
                filesCollectionNamespace,
                fileInfoSerializer,
                messageEncoderSettings)
            {
                Filter = filter,
                Limit  = -1
            };

            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(id);
                }
                return(fileInfo);
            }
        }
예제 #5
0
 // constructors
 public Request(ServerDescription serverDescription, MongoServerInstance serverInstance, IReadBindingHandle binding, ConnectionId connectionId, IClientSessionHandle session)
 {
     _serverDescription = serverDescription;
     _serverInstance    = serverInstance;
     _binding           = binding;
     _connectionId      = connectionId;
     _session           = session;
     _nestingLevel      = 1;
 }
예제 #6
0
        private IWriteBindingHandle ToWriteBinding(IReadBindingHandle binding)
        {
            var writeBinding = binding as IWriteBindingHandle;

            if (writeBinding == null)
            {
                throw new InvalidOperationException("The current binding cannot be used for writing.");
            }
            return(writeBinding);
        }
예제 #7
0
        private async Task <byte[]> DownloadAsBytesHelperAsync(IReadBindingHandle binding, GridFSFileInfo fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (fileInfo.Length > int.MaxValue)
            {
                throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array.");
            }

            using (var destination = new MemoryStream((int)fileInfo.Length))
            {
                await DownloadToStreamHelperAsync(binding, fileInfo, destination, options, cancellationToken).ConfigureAwait(false);

                return(destination.GetBuffer());
            }
        }
예제 #8
0
        private GridFSFileInfo <TFileId> GetFileInfoByName(IReadBindingHandle binding, string filename, int revision, CancellationToken cancellationToken)
        {
            var operation = CreateGetFileInfoByNameOperation(filename, revision);

            using (var cursor = operation.Execute(binding, cancellationToken))
            {
                var fileInfo = cursor.FirstOrDefault(cancellationToken);
                if (fileInfo == null)
                {
                    throw new GridFSFileNotFoundException(filename, revision);
                }
                return(fileInfo);
            }
        }
예제 #9
0
        private GridFSFileInfo <TFileId> GetFileInfo(IReadBindingHandle binding, TFileId id, CancellationToken cancellationToken)
        {
            var operation = CreateGetFileInfoOperation(id);

            using (var cursor = operation.Execute(binding, cancellationToken))
            {
                var fileInfo = cursor.FirstOrDefault(cancellationToken);
                if (fileInfo == null)
                {
                    throw new GridFSFileNotFoundException(_idSerializationInfo.SerializeValue(id));
                }
                return(fileInfo);
            }
        }
예제 #10
0
        private byte[] DownloadAsBytesHelper(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (fileInfo.Length > int.MaxValue)
            {
                throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array.");
            }

            var bytes = new byte[(int)fileInfo.Length];

            using (var destination = new MemoryStream(bytes))
            {
                DownloadToStreamHelper(binding, fileInfo, destination, options, cancellationToken);
                return(bytes);
            }
        }
예제 #11
0
        private async Task <GridFSFileInfo <TFileId> > GetFileInfoAsync(IReadBindingHandle binding, TFileId id, CancellationToken cancellationToken)
        {
            var operation = CreateGetFileInfoOperation(id);

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

                if (fileInfo == null)
                {
                    throw new GridFSFileNotFoundException(_idSerializationInfo.SerializeValue(id));
                }
                return(fileInfo);
            }
        }
예제 #12
0
        private async Task <GridFSFileInfo <TFileId> > GetFileInfoByNameAsync(IReadBindingHandle binding, string filename, int revision, CancellationToken cancellationToken)
        {
            var operation = CreateGetFileInfoByNameOperation(filename, revision);

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

                if (fileInfo == null)
                {
                    throw new GridFSFileNotFoundException(filename, revision);
                }
                return(fileInfo);
            }
        }
예제 #13
0
        private GridFSDownloadStream <TFileId> CreateDownloadStream(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;
            var seekable = options.Seekable ?? false;

            if (checkMD5 && seekable)
            {
                throw new ArgumentException("CheckMD5 can only be used when Seekable is false.");
            }

            if (seekable)
            {
                return(new GridFSSeekableDownloadStream <TFileId>(this, binding, fileInfo));
            }
            else
            {
                return(new GridFSForwardOnlyDownloadStream <TFileId>(this, binding, fileInfo, checkMD5));
            }
        }
예제 #14
0
        private void DownloadToStreamHelper(IReadBindingHandle binding, GridFSFileInfo <TFileId> fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream <TFileId>(this, binding.Fork(), fileInfo, checkMD5))
            {
                var count  = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    source.ReadBytes(buffer, 0, partialCount, cancellationToken);
                    //((Stream)source).ReadBytes(buffer, 0, partialCount, cancellationToken);
                    destination.Write(buffer, 0, partialCount);
                    count -= partialCount;
                }
            }
        }
예제 #15
0
        private async Task DownloadToStreamAsyncHelper(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), filesCollectionDocument, checkMD5))
            {
                var count  = source.Length;
                var buffer = new byte[filesCollectionDocument.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);

                    count -= partialCount;
                }

                await source.CloseAsync(cancellationToken).ConfigureAwait(false);
            }
        }
예제 #16
0
 private GridFSFileInfo GetFileInfo(IReadBindingHandle binding, BsonValue id, CancellationToken cancellationToken)
 {
     var operation = CreateGetFileInfoOperation(id);
     using (var cursor = operation.Execute(binding, cancellationToken))
     {
         var fileInfo = cursor.FirstOrDefault(cancellationToken);
         if (fileInfo == null)
         {
             throw new GridFSFileNotFoundException(id);
         }
         return fileInfo;
     }
 }
예제 #17
0
        private bool FilesCollectionIndexesExist(IReadBindingHandle binding, CancellationToken cancellationToken)
        {
            var indexes = ListIndexes(binding, this.GetFilesCollectionNamespace(), cancellationToken);

            return(FilesCollectionIndexesExist(indexes));
        }
예제 #18
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;
            }
        }
예제 #19
0
 private RetryableReadContext CreateRetryableReadContext(IReadBindingHandle readBindingHandle, bool async)
 {
     return(async
         ? RetryableReadContext.CreateAsync(readBindingHandle, retryRequested: false, CancellationToken.None).GetAwaiter().GetResult()
         : RetryableReadContext.Create(readBindingHandle, retryRequested: false, CancellationToken.None));
 }
예제 #20
0
        private async Task DownloadToStreamHelperAsync(IReadBindingHandle binding, GridFSFileInfo fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), fileInfo, checkMD5))
            {
                var count = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);
                    await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false);
                    count -= partialCount;
                }

                await source.CloseAsync(cancellationToken).ConfigureAwait(false);
            }
        }
예제 #21
0
        private async Task<GridFSFileInfo> GetFileInfoAsync(IReadBindingHandle binding, BsonValue id, CancellationToken cancellationToken)
        {
            var filesCollectionNamespace = GetFilesCollectionNamespace();
            var serializerRegistry = _database.Settings.SerializerRegistry;
            var fileInfoSerializer = serializerRegistry.GetSerializer<GridFSFileInfo>();
            var messageEncoderSettings = GetMessageEncoderSettings();
            var filter = new BsonDocument("_id", id);

            var operation = new FindOperation<GridFSFileInfo>(
                filesCollectionNamespace,
                fileInfoSerializer,
                messageEncoderSettings)
            {
                Filter = filter,
                Limit = 1,
                SingleBatch = true
            };

            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(id);
                }
                return fileInfo;
            }
        }
예제 #22
0
        private GridFSDownloadStream CreateDownloadStream(IReadBindingHandle binding, GridFSFileInfo fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;
            var seekable = options.Seekable ?? false;
            if (checkMD5 && seekable)
            {
                throw new ArgumentException("CheckMD5 can only be used when Seekable is false.");
            }

            if (seekable)
            {
                return new GridFSSeekableDownloadStream(this, binding, fileInfo);
            }
            else
            {
                return new GridFSForwardOnlyDownloadStream(this, binding, fileInfo, checkMD5);
            }
        }
예제 #23
0
        private async Task<byte[]> DownloadAsBytesHelperAsync(IReadBindingHandle binding, GridFSFileInfo fileInfo, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (fileInfo.Length > int.MaxValue)
            {
                throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array.");
            }

            using (var destination = new MemoryStream((int)fileInfo.Length))
            {
                await DownloadToStreamHelperAsync(binding, fileInfo, destination, options, cancellationToken).ConfigureAwait(false);
                return destination.GetBuffer();
            }
        }
예제 #24
0
 private async Task<GridFSFileInfo> GetFileInfoAsync(IReadBindingHandle binding, BsonValue id, CancellationToken cancellationToken)
 {
     var operation = CreateGetFileInfoOperation(id);
     using (var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
     {
         var fileInfo = await cursor.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
         if (fileInfo == null)
         {
             throw new GridFSFileNotFoundException(id);
         }
         return fileInfo;
     }
 }
예제 #25
0
 private GridFSFileInfo GetFileInfoByName(IReadBindingHandle binding, string filename, int revision, CancellationToken cancellationToken)
 {
     var operation = CreateGetFileInfoByNameOperation(filename, revision);
     using (var cursor = operation.Execute(binding, cancellationToken))
     {
         var fileInfo = cursor.FirstOrDefault(cancellationToken);
         if (fileInfo == null)
         {
             throw new GridFSFileNotFoundException(filename, revision);
         }
         return fileInfo;
     }
 }
예제 #26
0
 private async Task<GridFSFileInfo> GetFileInfoByNameAsync(IReadBindingHandle binding, string filename, int revision, CancellationToken cancellationToken)
 {
     var operation = CreateGetFileInfoByNameOperation(filename, revision);
     using (var cursor = await operation.ExecuteAsync(binding, cancellationToken).ConfigureAwait(false))
     {
         var fileInfo = await cursor.FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);
         if (fileInfo == null)
         {
             throw new GridFSFileNotFoundException(filename, revision);
         }
         return fileInfo;
     }
 }
예제 #27
0
        private void DownloadToStreamHelper(IReadBindingHandle binding, GridFSFileInfo fileInfo, Stream destination, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken))
        {
            var checkMD5 = options.CheckMD5 ?? false;

            using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), fileInfo, checkMD5))
            {
                var count = source.Length;
                var buffer = new byte[fileInfo.ChunkSizeBytes];

                while (count > 0)
                {
                    var partialCount = (int)Math.Min(buffer.Length, count);
                    source.ReadBytes(buffer, 0, partialCount, cancellationToken);
                    //((Stream)source).ReadBytes(buffer, 0, partialCount, cancellationToken);
                    destination.Write(buffer, 0, partialCount);
                    count -= partialCount;
                }
            }
        }
예제 #28
0
        private async Task <bool> FilesCollectionIndexesExistAsync(IReadBindingHandle binding, CancellationToken cancellationToken)
        {
            var indexes = await ListIndexesAsync(binding, this.GetFilesCollectionNamespace(), cancellationToken).ConfigureAwait(false);

            return(FilesCollectionIndexesExist(indexes));
        }