当前位置: 首页>>代码示例>>C#>>正文


C# MongoGridFS.GetChunksCollection方法代码示例

本文整理汇总了C#中MongoDB.Driver.GridFS.MongoGridFS.GetChunksCollection方法的典型用法代码示例。如果您正苦于以下问题:C# MongoGridFS.GetChunksCollection方法的具体用法?C# MongoGridFS.GetChunksCollection怎么用?C# MongoGridFS.GetChunksCollection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MongoDB.Driver.GridFS.MongoGridFS的用法示例。


在下文中一共展示了MongoGridFS.GetChunksCollection方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SaveChunk

        private void SaveChunk()
        {
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                var database = gridFS.GetDatabase(ReadPreference.Primary);
                var chunksCollection = gridFS.GetChunksCollection(database);

                var lastChunkIndex = (_length + _fileInfo.ChunkSize - 1) / _fileInfo.ChunkSize - 1;
                if (_chunkIndex == -1 || _chunkIndex > lastChunkIndex)
                {
                    var message = string.Format("Invalid chunk index {0}.", _chunkIndex);
                    throw new MongoGridFSException(message);
                }

                var lastChunkSize = (int)(_length % _fileInfo.ChunkSize);
                if (lastChunkSize == 0)
                {
                    lastChunkSize = _fileInfo.ChunkSize;
                }

                BsonBinaryData data;
                if (_chunkIndex < lastChunkIndex || lastChunkSize == _fileInfo.ChunkSize)
                {
                    data = new BsonBinaryData(_chunk);
                }
                else
                {
                    var lastChunk = new byte[lastChunkSize];
                    Buffer.BlockCopy(_chunk, 0, lastChunk, 0, lastChunkSize);
                    data = new BsonBinaryData(lastChunk);
                }

                var query = Query.EQ("_id", _chunkId);
                var update = new UpdateDocument
                {
                    { "_id", _chunkId },
                    { "files_id", _fileInfo.Id },
                    { "n", (_chunkIndex < int.MaxValue) ? (BsonValue)new BsonInt32((int)_chunkIndex) : new BsonInt64(_chunkIndex) },
                    { "data", data }
                };
                chunksCollection.Update(query, update, UpdateFlags.Upsert);
                _chunkIsDirty = false;
            }
        }
开发者ID:GGsus,项目名称:mongo-csharp-driver,代码行数:45,代码来源:MongoGridFSStream.cs

示例2: LoadChunk

        private void LoadChunk(long chunkIndex)
        {
            if (_chunkIsDirty) { SaveChunk(); }

            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                var database = gridFS.GetDatabase();
                var chunksCollection = gridFS.GetChunksCollection(database);

                var query = Query.And(Query.EQ("files_id", _fileInfo.Id), Query.EQ("n", chunkIndex));
                var document = chunksCollection.FindOne(query);
                if (document == null)
                {
                    if (_chunk == null)
                    {
                        _chunk = new byte[_fileInfo.ChunkSize];
                    }
                    else
                    {
                        Array.Clear(_chunk, 0, _chunk.Length);
                    }
                    _chunkId = ObjectId.GenerateNewId();
                }
                else
                {
                    var bytes = document["data"].AsBsonBinaryData.Bytes;
                    if (bytes.Length == _fileInfo.ChunkSize)
                    {
                        _chunk = bytes;
                    }
                    else
                    {
                        if (_chunk == null)
                        {
                            _chunk = new byte[_fileInfo.ChunkSize];
                        }
                        Buffer.BlockCopy(bytes, 0, _chunk, 0, bytes.Length);
                        Array.Clear(_chunk, bytes.Length, _chunk.Length - bytes.Length);
                    }
                    _chunkId = document["_id"];
                }
                _chunkIndex = chunkIndex;
            }
        }
开发者ID:GGsus,项目名称:mongo-csharp-driver,代码行数:45,代码来源:MongoGridFSStream.cs

示例3: LoadChunkNoData

        private void LoadChunkNoData(long chunkIndex)
        {
            if (_chunkIsDirty) { SaveChunk(); }

            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                var database = gridFS.GetDatabase();
                var chunksCollection = gridFS.GetChunksCollection(database);

                if (_chunk == null)
                {
                    _chunk = new byte[_fileInfo.ChunkSize];
                }
                else
                {
                    Array.Clear(_chunk, 0, _chunk.Length);
                }

                var query = Query.And(Query.EQ("files_id", _fileInfo.Id), Query.EQ("n", chunkIndex));
                var fields = Fields.Include("_id");
                var document = chunksCollection.Find(query).SetFields(fields).SetLimit(1).FirstOrDefault();
                if (document == null)
                {
                    _chunkId = ObjectId.GenerateNewId();
                }
                else
                {
                    _chunkId = document["_id"];
                }
                _chunkIndex = chunkIndex;
            }
        }
开发者ID:GGsus,项目名称:mongo-csharp-driver,代码行数:33,代码来源:MongoGridFSStream.cs

示例4: AddMissingChunks

        // private methods
        private void AddMissingChunks()
        {
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                var database = gridFS.GetDatabase(ReadPreference.Primary);
                var chunksCollection = gridFS.GetChunksCollection(database);

                var query = Query.EQ("files_id", _fileInfo.Id);
                var fields = Fields.Include("n");
                var chunkCount = (_length + _fileInfo.ChunkSize - 1) / _fileInfo.ChunkSize;
                var chunksFound = new HashSet<long>();
                var foundExtraChunks = false;
                foreach (var chunk in chunksCollection.Find(query).SetFields(fields))
                {
                    var n = chunk["n"].ToInt64();
                    chunksFound.Add(n);
                    if (n >= chunkCount)
                    {
                        foundExtraChunks = true;
                    }
                }

                if (foundExtraChunks)
                {
                    var extraChunksQuery = Query.And(Query.EQ("files_id", _fileInfo.Id), Query.GTE("n", chunkCount));
                    chunksCollection.Remove(extraChunksQuery);
                }

                BsonBinaryData zeros = null; // delay creating it until it's actually needed
                for (var n = 0L; n < chunkCount; n++)
                {
                    if (!chunksFound.Contains(n))
                    {
                        if (zeros == null)
                        {
                            zeros = new BsonBinaryData(new byte[_fileInfo.ChunkSize]);
                        }
                        var missingChunk = new BsonDocument
                    {
                        { "_id", ObjectId.GenerateNewId() },
                        { "files_id", _fileInfo.Id },
                        { "n", (n < int.MaxValue) ? (BsonValue)new BsonInt32((int)n) : new BsonInt64(n) },
                        { "data", zeros }
                    };
                        chunksCollection.Insert(missingChunk);
                    }
                }
            }
        }
开发者ID:GGsus,项目名称:mongo-csharp-driver,代码行数:51,代码来源:MongoGridFSStream.cs


注:本文中的MongoDB.Driver.GridFS.MongoGridFS.GetChunksCollection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。