Esempio n. 1
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 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);
        }
Esempio n. 3
0
 public void Insert(IEnumerable<Document> docs)
 {
     InsertMessage im = new InsertMessage();
     im.FullCollectionName = this.FullName;
     List<BsonDocument> bdocs = new List<BsonDocument>();
     foreach(Document doc in docs){
         if(doc.Contains("_id") == false) doc["_id"] = oidGenerator.Generate();
         bdocs.Add(BsonConvert.From(doc));
     }
     im.BsonDocuments = bdocs.ToArray();
     try{
         this.connection.SendMessage(im);
     }catch(IOException ioe){
         throw new MongoCommException("Could not insert document, communication failure", this.connection,ioe);
     }
 }
Esempio n. 4
0
 public void Insert(IEnumerable<Document> docs)
 {
     InsertMessage im = new InsertMessage();
     im.FullCollectionName = this.FullName;
     List<Document> idocs = new List<Document>();
     foreach(Document doc in docs){
         if(doc.Contains("_id") == false){
             Oid _id = oidGenerator.Generate();
             doc.Prepend("_id",_id);
         }
     }
     idocs.AddRange(docs);
     im.Documents = idocs.ToArray();
     try{
         this.connection.SendMessage(im);
     }catch(IOException ioe){
         throw new MongoCommException("Could not insert document, communication failure", this.connection,ioe);
     }
 }
Esempio n. 5
0
 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);
 }