Пример #1
0
        public void TestReadBigDocument()
        {
            MemoryStream ms = new MemoryStream();
            var writer = new BsonWriter(ms, new BsonDocumentDescriptor());

            Document expected = new Document();
            expected.Append("str", "test")
                .Append("int", 45)
                .Append("long", (long)46)
                .Append("num", 4.5)
                .Append("date",DateTime.Today)
                .Append("_id", new OidGenerator().Generate())
                .Append("code", new Code("return 1;"))
                .Append("subdoc", new Document().Append("a",1).Append("b",2))
                .Append("array", new String[]{"a","b","c","d"})
                .Append("codewscope", new CodeWScope("return 2;", new Document().Append("c",1)))
                .Append("binary", new Binary(new byte[]{0,1,2,3}))
                .Append("regex", new MongoRegex("[A-Z]"))
                .Append("minkey", MongoMinKey.Value)
                .Append("maxkey", MongoMaxKey.Value)
                .Append("symbol", new MongoSymbol("symbol"))
            ;
            writer.WriteObject(expected);
            writer.Flush();
            ms.Seek(0,SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms, new BsonDocumentBuilder());
            Document doc = reader.Read();

            Assert.IsNotNull(doc);
        }
Пример #2
0
 protected string Serialize(Document document, BsonWriterSettings settings)
 {
     using(var mem = new MemoryStream())
     {
         var writer = new BsonWriter(mem, settings);
         writer.WriteObject(document);
         writer.Flush();
         return Convert.ToBase64String(mem.ToArray());
     }
 }
        /// <summary>
        /// Writes the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        public void Write(Stream stream){
            var header = Header;
            var bstream = new BufferedStream(stream);
            var writer = new BinaryWriter(bstream);
            var bwriter = new BsonWriter(bstream, _bsonWriterSettings);

            Header.MessageLength += CalculateBodySize(bwriter);
            if(Header.MessageLength > MaximumMessageSize)
                throw new MongoException("Maximum message length exceeded");

            writer.Write(header.MessageLength);
            writer.Write(header.RequestId);
            writer.Write(header.ResponseTo);
            writer.Write((int)header.OpCode);
            writer.Flush();
            WriteBody(bwriter);
            bwriter.Flush();
        }
        protected Document WriteAndRead(Document source){
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms, new BsonDocumentDescriptor());

            writer.WriteObject(source);
            writer.Flush();
            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms,new BsonDocumentBuilder());
            return reader.Read();
        }   
        /// <summary>
        ///   Writes out a header and the chunk of documents.
        /// </summary>
        /// <param name = "stream"></param>
        /// <param name = "chunk"></param>
        protected void WriteChunk(Stream stream, MessageChunk chunk){
            WriteHeader(new BinaryWriter(stream), chunk.Size);

            var writer = new BsonWriter(stream, _bsonWriterSettings);
            writer.WriteValue(BsonType.Integer, 0);
            writer.Write(FullCollectionName, false);

            foreach(var document in chunk.Documents)
                writer.WriteObject(document);

            writer.Flush();
        }