/// <summary> /// Writes the body. /// </summary> /// <param name="writer">The writer.</param> protected override void WriteBody(BsonWriter writer){ writer.WriteValue(BsonType.Integer, 0); writer.WriteValue(BsonType.Integer, CursorIds.Length); foreach(var id in CursorIds) writer.WriteValue(BsonType.Long, id); }
/// <summary> /// Writes the body. /// </summary> /// <param name="writer">The writer.</param> protected override void WriteBody(BsonWriter writer){ writer.WriteValue(BsonType.Integer, 0); writer.Write(FullCollectionName, false); writer.WriteValue(BsonType.Integer, Flags); writer.WriteObject(Selector); writer.WriteObject(Document); }
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); }
private string WriteStringAndGetHex(string val) { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); writer.Write(val, false); return BitConverter.ToString(ms.ToArray()); }
public void TestCalculateSizeOfEmptyDoc() { var doc = new Document(); var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); Assert.AreEqual(5, writer.CalculateSizeObject(doc)); }
/// <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; }
/// <summary> /// Writes the specified stream. /// </summary> /// <param name="stream">The stream.</param> public void Write(Stream stream){ var bstream = new BufferedStream(stream); var bwriter = new BsonWriter(bstream, _bsonWriterSettings); ChunkMessage(bwriter); foreach(var chunk in _chunks) WriteChunk(bstream, chunk); }
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()); } }
public void TestCalculateSizeOfSimpleDoc() { var doc = new Document {{"a", "a"}, {"b", 1}}; var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); //BsonDocument bdoc = BsonConvert.From(doc); Assert.AreEqual(21, writer.CalculateSizeObject(doc)); }
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(); }
public void TestCalculateSizeOfComplexDoc() { var doc = new Document(); doc.Add("a", "a"); doc.Add("b", 1); var sub = new Document().Add("c_1", 1).Add("c_2", DateTime.Now); doc.Add("c", sub); var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); Assert.AreEqual(51, writer.CalculateSizeObject(doc)); }
public void TestRoundTrip(){ var idoc = new Document{{"b", new Binary(new[]{(byte)1, (byte)2})}}; var stream = new MemoryStream(); var writer = new BsonWriter(stream, new BsonDocumentDescriptor()); writer.WriteObject(idoc); stream.Seek(0, SeekOrigin.Begin); var reader = new BsonReader(stream,new BsonDocumentBuilder()); var odoc = reader.Read(); Assert.AreEqual(idoc.ToString(), odoc.ToString()); }
public void TestNullsDontThrowExceptions() { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); var doc = new Document().Add("n", null); try { writer.WriteObject(doc); } catch(NullReferenceException) { Assert.Fail("Null Reference Exception was thrown on trying to serialize a null value"); } }
public void TestBinaryRead(){ const string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400"; var data = DecodeHex(hex); var inmem = new MemoryStream(data); var inreader = new BsonReader(inmem,new BsonDocumentBuilder()); var indoc = inreader.Read(); var outmem = new MemoryStream(); var outwriter = new BsonWriter(outmem, new BsonDocumentDescriptor()); outwriter.WriteObject(indoc); var outdata = outmem.ToArray(); var outhex = BitConverter.ToString(outdata); outhex = outhex.Replace("-", ""); Assert.AreEqual(hex, outhex.ToLower()); }
/// <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(); }
/// <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); }
// internal methods internal void WriteTo(BsonWriter bsonWriter) { bsonWriter.WriteName(_name); _value.WriteTo(bsonWriter); }
public void TestWritingTooLargeDocument() { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); var b = new Binary(new byte[BsonInfo.MaxDocumentSize]); var big = new Document().Add("x", b); writer.WriteObject(big); }
public void TestWriteSymbol() { var expected = "0700000073796D626F6C00"; var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); MongoSymbol val = "symbol"; Assert.IsTrue(String.IsInterned(val) != null); writer.WriteValue(BsonType.Symbol, val); var hexdump = BitConverter.ToString(ms.ToArray()).Replace("-", ""); Assert.AreEqual(expected, hexdump); }
public void TestWriteString() { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); const string expected = "54-65-73-74-73-2E-69-6E-73-65-72-74-73-00"; writer.Write("Tests.inserts", false); var hexdump = BitConverter.ToString(ms.ToArray()); Assert.AreEqual(expected, hexdump); }
/// <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); }
public void TestWriteSingle() { var expected = "000000E0FFFFEF47"; var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); var val = Single.MaxValue; writer.WriteValue(BsonType.Number, val); var hexdump = BitConverter.ToString(ms.ToArray()); hexdump = hexdump.Replace("-", ""); Assert.AreEqual(expected, hexdump); }
/// <summary> /// Calculates the size of the body. /// </summary> /// <param name="writer">The writer.</param> /// <returns></returns> protected abstract int CalculateBodySize(BsonWriter writer);
/// <summary> /// Writes the document to a BsonWriter. /// </summary> /// <param name="bsonWriter">The writer.</param> public new void WriteTo(BsonWriter bsonWriter) { Serialize(bsonWriter, typeof(BsonDocument), null); }
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 the body. /// </summary> /// <param name="writer">The writer.</param> protected override void WriteBody(BsonWriter writer){ writer.WriteValue(BsonType.Integer, (int)Options); writer.Write(FullCollectionName, false); writer.WriteValue(BsonType.Integer, NumberToSkip); writer.WriteValue(BsonType.Integer, NumberToReturn); writer.WriteObject(Query); if(ReturnFieldSelector != null) writer.WriteObject(ReturnFieldSelector); }
public void TestWriteArrayDoc() { const string expected = "2000000002300002000000610002310002000000620002320002000000630000"; var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); var str = new[] {"a", "b", "c"}; writer.WriteValue(BsonType.Array, str); var hexdump = BitConverter.ToString(ms.ToArray()); hexdump = hexdump.Replace("-", ""); Assert.AreEqual(expected, hexdump); }
/// <summary> /// Writes the body. /// </summary> /// <param name="writer">The writer.</param> protected override void WriteBody(BsonWriter writer){ writer.Write(Message, false); }
/// <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; }
private static void DoEncode(Document doc) { var ms = new MemoryStream(); for(var i = 0; i < perTrial; i++) { var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); writer.WriteObject(doc); ms.Seek(0, SeekOrigin.Begin); } }
public new void WriteTo(BsonWriter bsonWriter) { BsonSerializer.Serialize(bsonWriter, this); }
public void TestWriteDocument() { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); const string expected = "1400000002746573740005000000746573740000"; var doc = new Document().Add("test", "test"); writer.WriteObject(doc); var hexdump = BitConverter.ToString(ms.ToArray()); hexdump = hexdump.Replace("-", ""); Assert.AreEqual(expected, hexdump); }
private static void RunDecodeTest(string name, Document doc) { var ms = new MemoryStream(); var writer = new BsonWriter(ms, new BsonDocumentDescriptor()); writer.WriteObject(doc); var buff = ms.ToArray(); var lowest = TimeSpan.MaxValue; for(var i = 0; i < trials; i++) { var ret = TimeDecode(buff); if(ret < lowest) lowest = ret; } var opsSec = (int)(perTrial/lowest.TotalSeconds); Console.Out.WriteLine(String.Format("{0}{1} {2}", name + new string('.', 55 - name.Length), opsSec, lowest)); }
public override void Serialize(BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options) { BsonDocumentWrapperSerializer.Instance.Serialize(bsonWriter, nominalType, this, options); }