コード例 #1
0
        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
ファイル: BsonConvert.cs プロジェクト: sbos/mongodb-csharp
 public static BsonType Create(BsonDataType type)
 {
     BsonType ret = null;
     if(type == BsonDataType.Number){
         ret = new BsonNumber();
     }else if(type == BsonDataType.Number){
         throw new NotImplementedException();
     }else if(type == BsonDataType.String){
         ret = new BsonString();
     }else if(type == BsonDataType.Obj){
         ret = new BsonDocument();
     }else if(type == BsonDataType.Array){
         ret = new BsonArray();
     }else if(type == BsonDataType.Integer){
         ret = new BsonInteger();
     }else if(type == BsonDataType.Long){
         ret = new BsonLong();
     }else if(type == BsonDataType.Boolean){
         ret = new BsonBoolean();
     }else if(type == BsonDataType.Oid){
         ret = new BsonOid();
     }else if(type == BsonDataType.Date){
         ret = new BsonDate();
     }else if(type == BsonDataType.Regex){
         ret = new BsonRegex();
     }else{
         throw new ArgumentOutOfRangeException("Type: " + type + " not recognized");
     }
     return ret;
 }
コード例 #3
0
        protected QueryMessage generateQueryMessage()
        {
            BsonDocument qdoc = new BsonDocument();
            qdoc.Add("listDatabases", new BsonNumber(1.0));
            //QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces");
            QueryMessage qmsg = new QueryMessage(qdoc,"admin.$cmd");
            qmsg.NumberToReturn = -1;

            return qmsg;
        }
コード例 #4
0
 public QueryMessage(BsonDocument query, String fullCollectionName, Int32 numberToReturn, 
     Int32 numberToSkip, BsonDocument returnFieldSelector)
 {
     this.Header = new MessageHeader(OpCode.Query);
     this.Query = query;
     this.FullCollectionName = fullCollectionName;
     this.NumberToReturn = numberToReturn;
     this.NumberToSkip = NumberToSkip;
     this.ReturnFieldSelector = returnFieldSelector;
 }
コード例 #5
0
        public void TestAdds()
        {
            BsonDocument doc = new BsonDocument();
            for(int i = 1; i < 6; i++){
                BsonElement be = new BsonElement(Convert.ToString(i),new BsonInteger(i));
                doc.Add(be.Name, be);
            }
            Assert.AreEqual(5,doc.Count,"Not all elements there");

            doc.Add(new BsonElement("6", new BsonInteger(6)));
            Assert.AreEqual(6,doc.Count,"Not all elements there");

            doc.Add("7", new BsonInteger(7));
            Assert.AreEqual(7,doc.Count,"Not all elements there");
        }
コード例 #6
0
ファイル: TestConnection.cs プロジェクト: sbos/mongodb-csharp
        public void TestSendQueryMessage()
        {
            //Connection conn = new Connection("10.141.153.2");
            Connection conn = new Connection();
            conn.Open();

            BsonDocument qdoc = new BsonDocument();
            qdoc.Add("listDatabases", new BsonNumber(1.0));
            //QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces");
            QueryMessage qmsg = new QueryMessage(qdoc,"admin.$cmd");
            qmsg.NumberToReturn = -1;
            conn.SendTwoWayMessage(qmsg);

            conn.Close();
        }
コード例 #7
0
        public void TestAllBytesWritten()
        {
            BsonDocument query = new BsonDocument();
            query.Add("col1", BsonConvert.From(1));

            QueryMessage msg = new QueryMessage(query,"TestDB.TestCol");
            MemoryStream buffer = new MemoryStream();
            msg.Write(buffer);

            Byte[] output = buffer.ToArray();
            String hexdump = BitConverter.ToString(output);
            //Console.WriteLine("Dump: " + hexdump);

            Assert.IsTrue(output.Length > 0);
            Assert.AreEqual("3A-00-00-00-00-00-00-00-00-00-00-00-D4-07-00-00-00-00-00-00-54-65-73-74-44-42-2E-54-65-73-74-43-6F-6C-00-00-00-00-00-00-00-00-00-0F-00-00-00-10-63-6F-6C-31-00-01-00-00-00-00", hexdump);
        }
コード例 #8
0
        public void TestRoundTrip()
        {
            Document idoc = new Document();
            idoc.Add("b",new Binary(new byte[]{(byte)1,(byte)2}));
            BsonDocument bidoc = BsonConvert.From(idoc);

            MemoryStream stream = new MemoryStream();
            BsonWriter writer = new BsonWriter(stream);
            bidoc.Write(writer);

            stream.Seek(0,SeekOrigin.Begin);
            BsonReader reader = new BsonReader(stream);
            BsonDocument bodoc = new BsonDocument();
            bodoc.Read(reader);
            Document odoc = (Document) bodoc.ToNative();

            Assert.AreEqual(idoc.ToString(), odoc.ToString());
        }
コード例 #9
0
        public void TestBinaryRead()
        {
            string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";

            byte[] data = DecodeHex (hex);
            MemoryStream inmem = new MemoryStream (data);
            BsonReader inreader = new BsonReader (inmem);
            BsonDocument indoc = new BsonDocument ();
            indoc.Read (inreader);

            MemoryStream outmem = new MemoryStream ();
            BsonWriter outwriter = new BsonWriter (outmem);
            indoc.Write (outwriter);
            byte[] outdata = outmem.ToArray ();
            String outhex = BitConverter.ToString (outdata);
            outhex = outhex.Replace ("-", "");

            Assert.AreEqual (hex, outhex.ToLower());
        }
コード例 #10
0
        public void TestWriteMessageTwice()
        {
            string expectedHex = "3A-00-00-00-00-00-00-00-00-00-00-00-D4-07-00-00-00-00-00-00-54-65-73-74-44-42-2E-54-65-73-74-43-6F-6C-00-00-00-00-00-00-00-00-00-0F-00-00-00-10-63-6F-6C-31-00-01-00-00-00-00";
            BsonDocument query = new BsonDocument();
            query.Add("col1", BsonConvert.From(1));

            QueryMessage msg = new QueryMessage(query,"TestDB.TestCol");
            MemoryStream buffer = new MemoryStream();
            msg.Write(buffer);

            Byte[] output = buffer.ToArray();
            String hexdump = BitConverter.ToString(output);

            MemoryStream buffer2 = new MemoryStream();
            msg.Write(buffer2);

            Byte[] output2 = buffer.ToArray();
            String hexdump2 = BitConverter.ToString(output2);

            Assert.AreEqual(expectedHex,hexdump);
            Assert.AreEqual(hexdump,hexdump2);
        }
コード例 #11
0
ファイル: BsonConvert.cs プロジェクト: averyj/mongodb-csharp
 public static BsonType Create(BsonDataType type)
 {
     BsonType ret = null;
     if(type == BsonDataType.Number){
         ret = new BsonNumber();
     }else if(type == BsonDataType.Number){
         throw new NotImplementedException();
     }else if(type == BsonDataType.String){
         ret = new BsonString();
     }else if(type == BsonDataType.Obj){
         ret = new BsonDocument();
     }else if(type == BsonDataType.Array){
         ret = new BsonArray();
     }else if(type == BsonDataType.Integer){
         ret = new BsonInteger();
     }else if(type == BsonDataType.Long){
         ret = new BsonLong();
     }else if(type == BsonDataType.Boolean){
         ret = new BsonBoolean();
     }else if(type == BsonDataType.Oid){
         ret = new BsonOid();
     }else if(type == BsonDataType.Date){
         ret = new BsonDate();
     }else if(type == BsonDataType.Regex){
         ret = new BsonRegex();
     }else if(type == BsonDataType.Undefined){
         ret = new BsonUndefined();
     }else if(type == BsonDataType.Null){
         ret = new BsonNull();
     }else if(type == BsonDataType.Code){
         ret = new BsonCode();
     }else if(type == BsonDataType.CodeWScope){
         ret = new BsonCodeWScope();
     }else{
         string typename = Enum.GetName(typeof(BsonDataType), type);
         throw new ArgumentOutOfRangeException("type",typename + "is not recognized");
     }
     return ret;
 }
コード例 #12
0
        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);
        }
コード例 #13
0
        public void Read(Stream stream)
        {
            /* Used during debugging of the stream.
            BsonReader headerreader = new BsonReader(stream);
            this.Header = ReadHeader(headerreader);

            //buffer the whole response into a memorystream for debugging.
            MemoryStream buffer = new MemoryStream();
            BinaryReader buffReader = new BinaryReader(stream);
            BinaryWriter buffWriter = new BinaryWriter(buffer);
            byte[] body = buffReader.ReadBytes(this.Header.MessageLength - 16);
            System.Console.WriteLine(BitConverter.ToString(body));
            buffWriter.Write(body);
            buffer.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(buffer);*/

            BsonReader reader = new BsonReader(stream);
            this.Header = ReadHeader(reader);

            this.ResponseFlag = reader.ReadInt32();
            this.CursorID = reader.ReadInt64();
            this.StartingFrom = reader.ReadInt32();
            this.NumberReturned = reader.ReadInt32();

            List<BsonDocument> docs = new List<BsonDocument>();
            for(int num = 0; num < this.NumberReturned; num++){
                BsonDocument doc = new BsonDocument();
                doc.Read(reader);
                docs.Add(doc);
            }
            this.Documents = docs.ToArray();
        }
コード例 #14
0
ファイル: BsonConvert.cs プロジェクト: averyj/mongodb-csharp
 public static BsonDocument From(Document doc)
 {
     BsonDocument bdoc = new BsonDocument();
     foreach(String key in doc.Keys){
         bdoc.Add(key, From(doc[key]));
     }
     return bdoc;
 }
コード例 #15
0
        public void TestElements()
        {
            BsonDocument bdoc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            Oid oid = new Oid("4a753ad8fac16ea58b290351");

            bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid)))
                .Append("a", new BsonElement("a",new BsonNumber(1)))
                .Append("b", new BsonElement("b",new BsonString("test")));
            bdoc.Write(writer);

            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");
                             //0         1         2         3         4         5         6         7         8         9
                             //0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
            string expected = "2D000000075F6964004A753AD8FAC16EA58B290351016100000000000000F03F02620005000000746573740000";
            Assert.AreEqual(expected,hexdump, "Dump not correct");
        }
コード例 #16
0
        public void TestSize()
        {
            BsonDocument doc = new BsonDocument();
            Assert.AreEqual(5, doc.Size);
            doc.Add("test", new BsonString("test"));
            Assert.AreEqual(20, doc.Size);
            for(int i = 1; i < 6; i++){
                doc.Add(Convert.ToString(i),new BsonInteger(i));
            }
            Assert.AreEqual(55, doc.Size);

            BsonDocument sub = new BsonDocument();
            sub.Add("test", new BsonString("sub test"));
            Assert.AreEqual(24,sub.Size);
            doc.Add("sub",sub);
            Assert.AreEqual(84,doc.Size);
        }
コード例 #17
0
ファイル: Collection.cs プロジェクト: sbos/mongodb-csharp
 public void Insert(IEnumerable<Document> docs, int length)
 {
     InsertMessage im = new InsertMessage();
     im.FullCollectionName = this.FullName;
     BsonDocument[] bdocs = new BsonDocument[length];
     int i = 0;
     foreach(Document doc in docs)
         bdocs[i++] = BsonConvert.From(doc);
     im.BsonDocuments = bdocs;
     this.connection.SendMessage(im);
 }
コード例 #18
0
 public QueryMessage(BsonDocument query, String fullCollectionName)
     : this(query,fullCollectionName,0,0)
 {
 }
コード例 #19
0
        public void TestNumberElements()
        {
            BsonDocument bdoc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            Oid oid = new Oid("4a75384cfac16ea58b290350");

            bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid)))
                .Append("a", new BsonElement("a",new BsonNumber(1)))
                .Append("b", new BsonElement("b",new BsonNumber(2)));
            bdoc.Write(writer);

            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");

            Assert.AreEqual("2C000000075F6964004A75384CFAC16EA58B290350016100000000000000F03F016200000000000000004000",hexdump, "Dump not correct");
        }
コード例 #20
0
        public void TestFormattingWithUKPound()
        {
            BsonDocument doc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            doc.Add("_id", new BsonOid(new Oid("4ABBED9D1D8B0F0218000001")));
            doc.Add("test", new BsonString("1234£56"));
            doc.Write(writer);
            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");
            //Assert.AreEqual(20,output[0],"Size didn't take into count null terminator");
            Assert.AreEqual("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000",hexdump, "Dump not correct");
        }
コード例 #21
0
 public QueryMessage(BsonDocument query, String fullCollectionName, Int32 numberToReturn, Int32 numberToSkip)
     : this(query,fullCollectionName,numberToReturn, numberToSkip, null)
 {
 }
コード例 #22
0
ファイル: BsonConvert.cs プロジェクト: averyj/mongodb-csharp
 public static BsonDocument From(DBRef val)
 {
     BsonDocument ret = new BsonDocument();
     ret.Add(new BsonElement("$ref", new BsonString(val.CollectionName)));
     ret.Add(new BsonElement("$id", From(val.Id)));
     return ret;
 }
コード例 #23
0
 public BsonDocument Update(BsonDocument from)
 {
     if(from == null) return this;
     foreach(String key in from.Keys){
         this[key] = from[key];
     }
     return this;
 }
コード例 #24
0
        public void TestFormatting()
        {
            BsonDocument doc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            doc.Add("test", new BsonString("test"));
            doc.Write(writer);
            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");
            Assert.AreEqual(20,output[0],"Size didn't take into count null terminator");
            Assert.AreEqual("1400000002746573740005000000746573740000",hexdump, "Dump not correct");
        }