/// <summary>
 /// Calculates the size of the body.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <returns></returns>
 protected override int CalculateBodySize(BsonWriter writer){
     var size = 4; //first int32
     size += writer.CalculateSize(FullCollectionName, false);
     size += 4; //flags
     size += writer.CalculateSizeObject(Selector);
     size += writer.CalculateSizeObject(Document);
     return size;
 }
 private string WriteAndReadLenString(string val){
     var ms = new MemoryStream();
     var bs = new BsonWriter(ms, new BsonDocumentDescriptor());
     var w = new BinaryWriter(ms);
     var byteCount = bs.CalculateSize(val, false);
     w.Write(byteCount);
     bs.Write(val, false);
     ms.Seek(0, SeekOrigin.Begin);
     var reader = new BsonReader(ms, new BsonDocumentBuilder());
     return reader.ReadLengthString();
 }
        /// <summary>
        ///   Breaks down an insert message that may be too large into managable sizes.  
        ///   When inserting only one document there will be only one chunk.  However chances
        ///   are that when inserting thousands of documents at once there will be many.
        /// </summary>
        protected void ChunkMessage(BsonWriter writer){
            var baseSize = CalculateBaseSize(writer);

            var chunk = new MessageChunk{Size = baseSize, Documents = new List<object>()};
            
            foreach(var document in Documents){
                var documentSize = writer.CalculateSize(document);
                
                if(documentSize + baseSize >= MaximumMessageSize)
                    throw new MongoException("Document is too big to fit in a message.");

                if(documentSize + chunk.Size > MaximumMessageSize){
                    _chunks.Add(chunk);
                    chunk = new MessageChunk{Size = baseSize, Documents = new List<object>()};
                }
                
                chunk.Documents.Add(document);
                chunk.Size += documentSize;
            }

            _chunks.Add(chunk);
        }
 /// <summary>
 /// Calculates the size of the body.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <returns></returns>
 protected override int CalculateBodySize(BsonWriter writer){
     var size = 12; //options, numbertoskip, numbertoreturn
     size += writer.CalculateSize(FullCollectionName, false);
     size += writer.CalculateSizeObject(Query);
     if(ReturnFieldSelector != null)
         size += writer.CalculateSizeObject(ReturnFieldSelector);
     return size;
 }
 /// <summary>
 /// Calculates the size of the body.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <returns></returns>
 protected override int CalculateBodySize(BsonWriter writer){
     return writer.CalculateSize(Message, false);
 }
 /// <summary>
 ///   The base size that all chunks will have.
 /// </summary>
 protected int CalculateBaseSize(BsonWriter writer){
     var size = 4; //first int32
     size += writer.CalculateSize(FullCollectionName, false);
     size += Header.MessageLength;
     return size;
 }
 /// <summary>
 /// Calculates the size of the body.
 /// </summary>
 /// <param name="writer">The writer.</param>
 /// <returns></returns>
 protected override int CalculateBodySize(BsonWriter writer){
     var size = 4; //first int32
     size += writer.CalculateSize(FullCollectionName, false);
     size += 12; //number to return + cursorid
     return size;
 }