Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactored ISerializationFactory to use GetBsonWriterSettings instead…
… of GetBsonDescriptor directly.
  • Loading branch information
lanwin committed May 6, 2010
1 parent 3f5f34c commit 2f438f1
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 174 deletions.
124 changes: 64 additions & 60 deletions source/MongoDB.Tests/IntegrationTests/Connections/TestConnection.cs
@@ -1,68 +1,72 @@
using System;
using System.IO;
using System.Text;
using MongoDB.Bson;
using MongoDB.Connections;
using MongoDB.Protocol;
using NUnit.Framework;

namespace MongoDB.IntegrationTests.Connections
{
[TestFixture()]
public class TestConnection
{
[Test]
public void TestSendQueryMessage(){
//Connection conn = new Connection("10.141.153.2");
Connection conn = ConnectionFactory.GetConnection(string.Empty);
conn.Open();

var qmsg = generateQueryMessage();
conn.SendTwoWayMessage(qmsg);

conn.Close();
}

[Test]
public void TestReconnectOnce(){
Connection conn = ConnectionFactory.GetConnection(string.Empty);
conn.Open();

WriteBadMessage(conn);
try{
var qmsg = generateQueryMessage();
conn.SendTwoWayMessage(qmsg);

}catch(IOException){
//Should be able to resend.
Assert.IsTrue(conn.State == ConnectionState.Opened);
var qmsg = generateQueryMessage();
ReplyMessage<Document> rmsg = conn.SendTwoWayMessage(qmsg);
Assert.IsNotNull(rmsg);

}
}

protected void WriteBadMessage(Connection conn){
//Write a bad message to the socket to force mongo to shut down our connection.
BinaryWriter writer = new BinaryWriter(conn.GetStream());
System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
Byte[] msg = encoding.GetBytes("Goodbye MongoDB!");
writer.Write(16 + msg.Length + 1);
writer.Write(1);
writer.Write(1);
writer.Write(1001);
writer.Write(msg);
writer.Write((byte)0);
}

protected QueryMessage generateQueryMessage(){
Document qdoc = new Document();
qdoc.Add("listDatabases", 1.0);
namespace MongoDB.IntegrationTests.Connections
{
[TestFixture]
public class TestConnection
{
protected void WriteBadMessage(Connection conn)
{
//Write a bad message to the socket to force mongo to shut down our connection.
var writer = new BinaryWriter(conn.GetStream());
var encoding = new UTF8Encoding();
var msg = encoding.GetBytes("Goodbye MongoDB!");
writer.Write(16 + msg.Length + 1);
writer.Write(1);
writer.Write(1);
writer.Write(1001);
writer.Write(msg);
writer.Write((byte)0);
}

protected QueryMessage GenerateQueryMessage()
{
var qdoc = new Document {{"listDatabases", 1.0}};
//QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces");
var qmsg = new QueryMessage(new BsonDocumentDescriptor(), qdoc, "admin.$cmd");
qmsg.NumberToReturn = -1;

return qmsg;
}
}
return new QueryMessage(new BsonWriterSettings(), qdoc, "admin.$cmd")
{
NumberToReturn = -1
};
}

[Test]
public void TestReconnectOnce()
{
var conn = ConnectionFactory.GetConnection(string.Empty);
conn.Open();

WriteBadMessage(conn);
try
{
var qmsg = GenerateQueryMessage();
conn.SendTwoWayMessage(qmsg);
}
catch(IOException)
{
//Should be able to resend.
Assert.IsTrue(conn.State == ConnectionState.Opened);
var qmsg = GenerateQueryMessage();
var rmsg = conn.SendTwoWayMessage(qmsg);
Assert.IsNotNull(rmsg);
}
}

[Test]
public void TestSendQueryMessage()
{
//Connection conn = new Connection("10.141.153.2");
var conn = ConnectionFactory.GetConnection(string.Empty);
conn.Open();

var qmsg = GenerateQueryMessage();
conn.SendTwoWayMessage(qmsg);

conn.Close();
}
}
}
93 changes: 46 additions & 47 deletions source/MongoDB.Tests/UnitTests/IO/TestQueryMessage.cs
Expand Up @@ -4,52 +4,51 @@
using MongoDB.Protocol;
using NUnit.Framework;

namespace MongoDB.UnitTests.IO
{
[TestFixture]
public class TestQueryMessage
{
[Test]
public void TestAllBytesWritten()
{
Document query = new Document();
query.Add("col1", 1);

var msg = new QueryMessage(new BsonDocumentDescriptor(),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);
}

[Test]
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";
Document query = new Document();
namespace MongoDB.UnitTests.IO
{
[TestFixture]
public class TestQueryMessage
{
[Test]
public void TestAllBytesWritten()
{
var query = new Document {{"col1", 1}};

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

var output = buffer.ToArray();
var 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);
}

[Test]
public void TestWriteMessageTwice()
{
const 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";
var query = new Document();
query.Add("col1", 1);

var msg = new QueryMessage(new BsonDocumentDescriptor(),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);

}
}

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

var output = buffer.ToArray();
var hexdump = BitConverter.ToString(output);

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

var output2 = buffer.ToArray();
var hexdump2 = BitConverter.ToString(output2);

Assert.AreEqual(expectedHex, hexdump);
Assert.AreEqual(hexdump, hexdump2);
}
}
}
4 changes: 2 additions & 2 deletions source/MongoDB/Connections/Connection.cs
Expand Up @@ -234,9 +234,9 @@ public T SendCommand<T>(ISerializationFactory factory, string database, Type roo
private T SendCommandCore<T>(ISerializationFactory factory, string database, Type rootType, object command)
where T : class
{
var descriptor = factory.GetBsonDescriptor(rootType);
var writerSettings = factory.GetBsonWriterSettings(rootType);

var query = new QueryMessage(descriptor)
var query = new QueryMessage(writerSettings)
{
FullCollectionName = database + ".$cmd",
NumberToReturn = -1,
Expand Down
16 changes: 11 additions & 5 deletions source/MongoDB/Cursor_1.cs
Expand Up @@ -262,11 +262,17 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
/// <summary>
/// Retrieves the data.
/// </summary>
private void RetrieveData(){
var descriptor = _serializationFactory.GetBsonDescriptor(typeof(T));

var query = new QueryMessage(descriptor) { FullCollectionName = FullCollectionName, Query = BuildSpec(),
NumberToReturn = _limit, NumberToSkip = _skip, Options = _options };
private void RetrieveData(){
var writerSettings = _serializationFactory.GetBsonWriterSettings(typeof(T));

var query = new QueryMessage(writerSettings)
{
FullCollectionName = FullCollectionName,
Query = BuildSpec(),
NumberToReturn = _limit,
NumberToSkip = _skip,
Options = _options
};

if (_fields != null)
query.ReturnFieldSelector = _fields;
Expand Down
35 changes: 18 additions & 17 deletions source/MongoDB/MongoCollection_1.cs
Expand Up @@ -304,9 +304,9 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
}

var rootType = typeof(T);
var bsonDescriptor = _configuration.SerializationFactory.GetBsonDescriptor(rootType);
var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(rootType);

var insertMessage = new InsertMessage(bsonDescriptor)
var insertMessage = new InsertMessage(writerSettings)
{
FullCollectionName = FullName
};
Expand Down Expand Up @@ -353,12 +353,14 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
/// An empty document will match all documents in the collection and effectively truncate it.
/// </remarks>
public void Delete(object selector){
var descriptor = _configuration.SerializationFactory.GetBsonDescriptor(typeof(T));

var deleteMessage = new DeleteMessage(descriptor) { FullCollectionName = FullName, Selector = selector };

var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));

try {
_connection.SendMessage(deleteMessage);
_connection.SendMessage(new DeleteMessage(writerSettings)
{
FullCollectionName = FullName,
Selector = selector
});
} catch (IOException exception) {
throw new MongoConnectionException("Could not delete document, communication failure", _connection, exception);
}
Expand Down Expand Up @@ -427,17 +429,16 @@ public void Update(object document, bool safemode)
/// <param name="selector">The query spec to find the document to update.</param>
/// <param name="flags"><see cref="UpdateFlags"/></param>
public void Update(object document, object selector, UpdateFlags flags){
var descriptor = _configuration.SerializationFactory.GetBsonDescriptor(typeof(T));

var updateMessage = new UpdateMessage(descriptor){
FullCollectionName = FullName,
Selector = selector,
Document = document,
Flags = (int)flags
};

var writerSettings = _configuration.SerializationFactory.GetBsonWriterSettings(typeof(T));

try {
_connection.SendMessage(updateMessage);
_connection.SendMessage(new UpdateMessage(writerSettings)
{
FullCollectionName = FullName,
Selector = selector,
Document = document,
Flags = (int)flags
});
} catch (IOException exception) {
throw new MongoConnectionException("Could not update document, communication failure", _connection, exception);
}
Expand Down
6 changes: 3 additions & 3 deletions source/MongoDB/Protocol/DeleteMessage.cs
Expand Up @@ -19,9 +19,9 @@ public class DeleteMessage : RequestMessageBase
/// <summary>
/// Initializes a new instance of the <see cref="DeleteMessage"/> class.
/// </summary>
/// <param name="objectDescriptor">The object descriptor.</param>
public DeleteMessage(IBsonObjectDescriptor objectDescriptor)
: base(objectDescriptor){
/// <param name="bsonWriterSettings">The bson writer settings.</param>
public DeleteMessage(BsonWriterSettings bsonWriterSettings)
: base(bsonWriterSettings){
Header = new MessageHeader(OpCode.Delete);
}

Expand Down
2 changes: 1 addition & 1 deletion source/MongoDB/Protocol/GetMoreMessage.cs
Expand Up @@ -33,7 +33,7 @@ public GetMoreMessage(string fullCollectionName, long cursorId)
/// <param name="cursorId">The cursor id.</param>
/// <param name="numberToReturn">The number to return.</param>
public GetMoreMessage(string fullCollectionName, long cursorId, int numberToReturn)
: base(new BsonDocumentDescriptor())
: base(new BsonWriterSettings())
{
Header = new MessageHeader(OpCode.GetMore);
FullCollectionName = fullCollectionName;
Expand Down
16 changes: 9 additions & 7 deletions source/MongoDB/Protocol/InsertMessage.cs
Expand Up @@ -18,16 +18,18 @@ namespace MongoDB.Protocol
/// </remarks>
public class InsertMessage : MessageBase, IRequestMessage
{
private readonly IBsonObjectDescriptor _objectDescriptor;
private readonly BsonWriterSettings _bsonWriterSettings;
private readonly List<MessageChunk> _chunks = new List<MessageChunk>();

/// <summary>
/// Initializes a new instance of the <see cref="InsertMessage"/> class.
/// </summary>
public InsertMessage(IBsonObjectDescriptor objectDescriptor){
if(objectDescriptor == null)
throw new ArgumentNullException("objectDescriptor");
_objectDescriptor = objectDescriptor;
public InsertMessage(BsonWriterSettings bsonWriterSettings)
{
if(bsonWriterSettings == null)
throw new ArgumentNullException("bsonWriterSettings");

_bsonWriterSettings = bsonWriterSettings;
Header = new MessageHeader(OpCode.Insert);
}

Expand All @@ -49,7 +51,7 @@ public class InsertMessage : MessageBase, IRequestMessage
/// <param name="stream">The stream.</param>
public void Write(Stream stream){
var bstream = new BufferedStream(stream);
var bwriter = new BsonWriter(bstream, _objectDescriptor);
var bwriter = new BsonWriter(bstream, _bsonWriterSettings);

ChunkMessage(bwriter);

Expand Down Expand Up @@ -103,7 +105,7 @@ public class InsertMessage : MessageBase, IRequestMessage
protected void WriteChunk(Stream stream, MessageChunk chunk){
WriteHeader(new BinaryWriter(stream), chunk.Size);

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

Expand Down

0 comments on commit 2f438f1

Please sign in to comment.