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); } }
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); } }
// constructors public Request(ServerDescription serverDescription, MongoServerInstance serverInstance, IReadBindingHandle binding) { _serverDescription = serverDescription; _serverInstance = serverInstance; _binding = binding; _nestingLevel = 1; }
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); } }
// 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; }
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); }
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()); } }
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); } }
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); } }
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); } }
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); } }
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); } }
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)); } }
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; } } }
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); } }
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; } }
private bool FilesCollectionIndexesExist(IReadBindingHandle binding, CancellationToken cancellationToken) { var indexes = ListIndexes(binding, this.GetFilesCollectionNamespace(), cancellationToken); return(FilesCollectionIndexesExist(indexes)); }
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; } }
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)); }
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); } }
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; } }
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); } }
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(); } }
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; } }
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; } }
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; } }
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; } } }
private async Task <bool> FilesCollectionIndexesExistAsync(IReadBindingHandle binding, CancellationToken cancellationToken) { var indexes = await ListIndexesAsync(binding, this.GetFilesCollectionNamespace(), cancellationToken).ConfigureAwait(false); return(FilesCollectionIndexesExist(indexes)); }