Writes primitives to a stream. Use Documents and Elements to write more complex items.
Наследование: IDisposable
        public void TestArrayElements()
        {
            String hexdoc = "82000000075f6964004a78937917220000000061cf0461005d0000" +
                            "00013000000000000000f03f013100000000000000004001320000" +
                            "000000000008400133000000000000001040013400000000000000" +
                            "145001350000000000000018400136000000000000001c40013700" +
                            "00000000000020400002620005000000746573740000";

            byte[] bytes = HexToBytes(hexdoc);
            MemoryStream buf = new MemoryStream(bytes);
            BsonReader reader = new BsonReader(buf);

            BsonDocument bdoc = new BsonDocument();
            bdoc.Read(reader);
            Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["a"].Val.TypeNum);

            buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);
            bdoc.Write(writer);

            String hexdump = BitConverter.ToString(buf.ToArray());
            hexdump = hexdump.Replace("-","").ToLower();

            Assert.AreEqual(hexdoc, hexdump);
        }
Пример #2
0
 protected override int CalculateBodySize(BsonWriter writer)
 {
     int size = 8; //first int32, second int32
     size += writer.CalculateSize(this.FullCollectionName,false);
     size += writer.CalculateSize(Selector);
     return size;
 }
 protected override void WriteBody(BsonWriter writer)
 {
     writer.WriteValue(BsonDataType.Integer,0);
     writer.WriteString(this.FullCollectionName);
     writer.WriteValue(BsonDataType.Integer,this.NumberToReturn);
     writer.WriteValue(BsonDataType.Long,this.CursorID);
 }
Пример #4
0
        public void TestReadBigDocument()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            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)
            ;
            writer.Write(expected);
            writer.Flush();
            ms.Seek(0,SeekOrigin.Begin);

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

            Assert.IsNotNull(doc);
        }
Пример #5
0
 protected override void WriteBody(BsonWriter writer)
 {
     writer.WriteValue(BsonDataType.Integer,0);
     writer.WriteString(this.FullCollectionName);
     writer.WriteValue(BsonDataType.Integer,0);
     writer.Write(this.Selector);
 }
Пример #6
0
 /// <summary>
 /// The base size that all chunks will have.
 /// </summary>
 protected int CalculateBaseSize(BsonWriter writer)
 {
     int size = 4; //first int32
     size += writer.CalculateSize(this.FullCollectionName,false);
     size += Header.MessageLength;
     return size;
 }
 protected override int CalculateBodySize(BsonWriter writer)
 {
     int size = 4; //first int32
     size += writer.CalculateSize(this.FullCollectionName,false);
     size += 12; //number to return + cursorid
     return size;
 }
Пример #8
0
 protected override int CalculateBodySize(BsonWriter writer)
 {
     int size = 8; //first int32, number of cursors
     foreach(long id in this.CursorIDs){
         size += 8;
     }
     return size;
 }
Пример #9
0
 protected override void WriteBody(Stream stream)
 {
     BsonWriter writer = new BsonWriter(stream);
     writer.Write((int)0);
     writer.Write(this.FullCollectionName);
     writer.Write((int)0);
     selector.Write(writer);
 }
Пример #10
0
 protected override void WriteBody(Stream stream)
 {
     BsonWriter writer = new BsonWriter(stream);
     writer.Write((int)0);
     writer.Write(this.FullCollectionName);
     foreach(BsonDocument bdoc in this.BsonDocuments){
         bdoc.Write(writer);
     }
 }
Пример #11
0
        protected override void WriteBody(BsonWriter writer)
        {
            writer.WriteValue(BsonDataType.Integer,0);
            writer.WriteValue(BsonDataType.Integer, this.CursorIDs.Length);

            foreach(long id in this.CursorIDs){
                writer.WriteValue(BsonDataType.Long, id);
            }
        }
Пример #12
0
 protected override int CalculateBodySize(BsonWriter writer)
 {
     int size = 4; //first int32
     size += writer.CalculateSize(this.FullCollectionName,false);
     size += 4; //flags
     size += writer.CalculateSize(this.Selector);
     size += writer.CalculateSize(this.Document);
     return size;
 }
Пример #13
0
 protected override void WriteBody(Stream stream)
 {
     BsonWriter writer = new BsonWriter(stream);
     writer.Write(0);
     writer.Write(this.FullCollectionName);
     writer.Write(this.NumberToReturn);
     writer.Write(this.cursorID);
     writer.Flush();
 }
 protected override void WriteBody(Stream stream)
 {
     BsonWriter writer = new BsonWriter(stream);
     writer.Write(0);
     writer.Write(this.CursorIDs.Length);
     foreach(long id in this.CursorIDs){
         writer.Write(id);
     }
     writer.Flush();
 }
Пример #15
0
        public void Write(Stream stream)
        {
            MessageHeader header = this.Header;
            BufferedStream bstream = new BufferedStream(stream);

            BsonWriter bwriter = new BsonWriter(bstream);
            ChunkMessage(bwriter);

            foreach(MessageChunk chunk in chunks){
                WriteChunk(bstream, chunk);
            }
        }
Пример #16
0
 public void TestFormatting()
 {
     BsonRegex reg = new BsonRegex("test");
     MemoryStream buf = new MemoryStream();
     BsonWriter writer = new BsonWriter(buf);
     reg.Write(writer);
     writer.Flush();
     Byte[] output = buf.ToArray();
     String hexdump = BitConverter.ToString(output);
     hexdump = hexdump.Replace("-","");
     Assert.AreEqual("746573740000",hexdump, "Dump not correct");
 }
Пример #17
0
 public void TestFormattingWithUKPound()
 {
     BsonString str = new BsonString("1234£56");
     MemoryStream buf = new MemoryStream();
     BsonWriter writer = new BsonWriter(buf);
     str.Write(writer);
     writer.Flush();
     Byte[] output = buf.ToArray();
     String hexdump = BitConverter.ToString(output);
     hexdump = hexdump.Replace("-","");
     Assert.AreEqual("0900000031323334C2A3353600",hexdump, "Dump not correct");
     Assert.AreEqual(9,output[0],"Size didn't take into account size of pound symbol");
 }
Пример #18
0
 public void TestFormatting()
 {
     BsonString str = new BsonString("test");
     MemoryStream buf = new MemoryStream();
     BsonWriter writer = new BsonWriter(buf);
     str.Write(writer);
     writer.Flush();
     Byte[] output = buf.ToArray();
     String hexdump = BitConverter.ToString(output);
     hexdump = hexdump.Replace("-","");
     Assert.AreEqual(5,output[0],"Size didn't take into count null terminator");
     Assert.AreEqual("050000007465737400",hexdump, "Dump not correct");
 }
Пример #19
0
        public void Write(Stream stream)
        {
            MessageHeader header = this.Header;
            BufferedStream bstream = new BufferedStream(stream);
            BinaryWriter writer = new BinaryWriter(bstream);
            BsonWriter bwriter = new BsonWriter(bstream);

            Header.MessageLength += this.CalculateBodySize(bwriter);

            writer.Write(header.MessageLength);
            writer.Write(header.RequestId);
            writer.Write(header.ResponseTo);
            writer.Write((int)header.OpCode);
            writer.Flush();
            WriteBody(bwriter);
            bwriter.Flush();
        }
Пример #20
0
        public void TestBinary()
        {
            byte[] data = File.ReadAllBytes(@"test-data\tests.binary.txt");
            BsonBinary binaryIn = new BsonBinary(new Binary(data));
            MemoryStream stream = new MemoryStream();
            BsonWriter bsonWriter = new BsonWriter(stream);
            binaryIn.Write(bsonWriter);

            stream.Position = 0;
            BsonReader reader = new BsonReader(stream);
            BsonBinary binaryOut = new BsonBinary();
            int size = reader.ReadInt32();
            binaryOut.Subtype = reader.ReadByte();
            binaryOut.Val = reader.ReadBytes(size);
            Assert.AreEqual(binaryIn.Val, binaryOut.Val);
            Assert.AreEqual(binaryIn.Subtype, binaryOut.Subtype);
            Assert.AreEqual(data.Length, binaryOut.Size);
        }
Пример #21
0
        /// <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)
        {
            int baseSize = CalculateBaseSize(writer);

            MessageChunk chunk = new MessageChunk(){Size = baseSize, Documents = new List<Document>()};
            foreach(Document doc in this.Documents){
                int docSize = writer.CalculateSize(doc);
                if(docSize + baseSize >= MessageBase.MaximumMessageSize) throw new MongoException("Document is too big to fit in a message.");

                if(docSize + chunk.Size > MessageBase.MaximumMessageSize){
                    chunks.Add(chunk);
                    chunk = new MessageChunk(){Size = baseSize, Documents = new List<Document>()};
                }
                chunk.Documents.Add(doc);
                chunk.Size += docSize;
            }
            chunks.Add(chunk);
        }
Пример #22
0
        public void TestDateUTC()
        {
            DateTime now = DateTime.UtcNow;
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            Document source = new Document();
            source.Append("d",now);

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

            BsonReader reader = new BsonReader(ms);
            Document copy = reader.Read();

            DateTime then = (DateTime)copy["d"];

            Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
        }
        public void Write(Stream stream)
        {
            MessageHeader header = this.Header;
            BufferedStream bstream = new BufferedStream(stream);
            BinaryWriter writer = new BinaryWriter(bstream);
            BsonWriter bwriter = new BsonWriter(bstream);

            Header.MessageLength += this.CalculateBodySize(bwriter);
            if(Header.MessageLength > MessageBase.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();
        }
Пример #24
0
        public void TestWrite()
        {
            MemoryStream ms = new MemoryStream();

            InsertMessage im = new InsertMessage();
            im.FullCollectionName ="Tests.inserts";
            Document doc = new Document();
            Document[] docs = new Document[]{doc};
            doc.Append("a","a");
            doc.Append("b",1);
            im.Documents = docs;

            Assert.AreEqual(16,im.Header.MessageLength);

            BsonWriter2 bwriter = new BsonWriter2(ms);

            Assert.AreEqual(21,bwriter.CalculateSize(doc));

            im.Write(ms);

            Byte[] bytes = ms.ToArray();
            String hexdump = BitConverter.ToString(bytes);
            System.Console.WriteLine(hexdump);

            MemoryStream ms2 = new MemoryStream();
            BsonWriter b = new BsonWriter(ms2);
            b.Write(im.Header.MessageLength);
            b.Write(im.Header.RequestId);
            b.Write(im.Header.ResponseTo);
            b.Write((int)im.Header.OpCode);
            b.Write((int)0);
            b.Write(im.FullCollectionName);
            BsonDocument bdoc = BsonConvert.From(doc);
            bdoc.Write(b);

            b.Flush();
            String hexdump2 = BitConverter.ToString(ms2.ToArray());
            System.Console.WriteLine(hexdump2);
            Assert.AreEqual(hexdump2,hexdump);
            Assert.AreEqual(bytes.Length,im.Header.MessageLength);
        }
        public void TestArraysWithHoles()
        {
            String hexdoc =
                "46000000075F6964004A79BFD517220000000061D304617272617900" +
                "29000000023000020000006100023100020000006200023200020000" +
                "0063000234000200000065000000";
            byte[] bytes = HexToBytes(hexdoc);
            MemoryStream buf = new MemoryStream(bytes);
            BsonReader reader = new BsonReader(buf);
            BsonDocument bdoc = new BsonDocument();
            bdoc.Read(reader);
            Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["array"].Val.TypeNum);

            buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);
            bdoc.Write(writer);

            String hexdump = BitConverter.ToString(buf.ToArray());
            hexdump = hexdump.Replace("-","");

            Assert.AreEqual(hexdoc, hexdump);
        }
Пример #26
0
        public void TestDBRef()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            Document source = new Document();
            source.Append("x",1).Append("ref",new DBRef("refs","ref1"));

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

            BsonReader reader = new BsonReader(ms);
            Document copy = reader.Read();

            Assert.IsTrue(copy.Contains("ref"));
            Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef));

            DBRef sref = (DBRef)source["ref"];
            DBRef cref = (DBRef)copy["ref"];

            Assert.AreEqual(sref.Id, cref.Id);
        }
Пример #27
0
        /// <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);

            BsonWriter writer = new BsonWriter(stream);
            writer.WriteValue(BsonDataType.Integer,0);
            writer.WriteString(this.FullCollectionName);

            foreach(Document doc in chunk.Documents){
                writer.Write(doc);
            }
            writer.Flush();
        }
Пример #28
0
        protected Document WriteAndRead(Document source)
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

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

            BsonReader reader = new BsonReader(ms);
            return reader.Read();
        }
Пример #29
0
 //        [Test]
 //        public void TestBsonInteger(){
 //            InitStreams();
 //            BsonInteger w = new BsonInteger(5);
 //            w.Write(writer);
 //            
 //            FlushAndGotoBegin();
 //            
 //            BsonInteger r = new BsonInteger();
 //            r.Read(reader);
 //            
 //            Assert.AreEqual(w.Val, r.Val);
 //        }
 protected void InitStreams()
 {
     mem = new MemoryStream();
     reader = new BsonReader(mem);
     writer = new BsonWriter(mem);
 }
Пример #30
0
 public void Write(BsonWriter writer)
 {
     writer.Write(this.Val.Length);
     writer.Write(this.Subtype);
     writer.Write(this.Val);
 }