Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Refactore the messages to use Autopropertys which makes them less com…
Browse files Browse the repository at this point in the history
…plex.
  • Loading branch information
lanwin committed Mar 5, 2010
1 parent cf2892a commit f639dc3
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 241 deletions.
20 changes: 6 additions & 14 deletions MongoDBDriver/IO/DeleteMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,11 @@ public class DeleteMessage : RequestMessage
// cstring fullCollectionName; // "dbname.collectionname"
// int32 ZERO; // 0 - reserved for future use
// BSON selector; // query object. See below for details.
//}
private string fullCollectionName;
public string FullCollectionName {
get { return fullCollectionName; }
set { fullCollectionName = value; }
}

private Document selector;
public Document Selector {
get { return selector; }
set { selector = value; }
}

//}
public string FullCollectionName { get; set; }

public Document Selector { get; set; }

public DeleteMessage(){
this.Header = new MessageHeader(OpCode.Delete);
}
Expand All @@ -39,7 +31,7 @@ protected override void WriteBody (BsonWriter writer){
protected override int CalculateBodySize(BsonWriter writer){
int size = 8; //first int32, second int32
size += writer.CalculateSize(this.FullCollectionName,false);
size += writer.CalculateSize(selector);
size += writer.CalculateSize(Selector);
return size;
}
}
Expand Down
44 changes: 16 additions & 28 deletions MongoDBDriver/IO/GetMoreMessage.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
using System;
using MongoDB.Driver.Bson;

namespace MongoDB.Driver.IO
{
/// <summary>
/// Description of GetMoreMessage.
/// </summary>
/// </summary>
/// <remarks>
/// struct {
/// MsgHeader header; // standard message header
/// int32 ZERO; // 0 - reserved for future use
/// cstring fullCollectionName; // "dbname.collectionname"
/// int32 numberToReturn; // number of documents to return
/// int64 cursorID; // cursorID from the OP_REPLY
/// }
/// </remarks>
public class GetMoreMessage : RequestMessage
{
// struct {
// MsgHeader header; // standard message header
// int32 ZERO; // 0 - reserved for future use
// cstring fullCollectionName; // "dbname.collectionname"
// int32 numberToReturn; // number of documents to return
// int64 cursorID; // cursorID from the OP_REPLY
// }
#region "Properties"
private long cursorID;
public long CursorID {
get { return cursorID; }
set { cursorID = value; }
}

private string fullCollectionName;
public string FullCollectionName {
get { return fullCollectionName; }
set { fullCollectionName = value; }
}

private Int32 numberToReturn;
public int NumberToReturn {
get { return numberToReturn; }
set { numberToReturn = value; }
}
#endregion
public long CursorID { get; set; }

public string FullCollectionName { get; set; }

public int NumberToReturn { get; set; }

public GetMoreMessage(string fullCollectionName, long cursorID)
:this(fullCollectionName, cursorID, 0){
}
Expand Down
24 changes: 8 additions & 16 deletions MongoDBDriver/IO/InsertMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@ namespace MongoDB.Driver.IO
/// <summary>
/// Description of InsertMessage.
/// </summary>
/// <remarks>
/// MsgHeader header; // standard message header
/// int32 ZERO; // 0 - reserved for future use
/// cstring fullCollectionName; // "dbname.collectionname"
/// BSON[] documents; // one or more documents to insert into the collection
/// </remarks>
public class InsertMessage : RequestMessage
{
public string FullCollectionName { get; set; }

// MsgHeader header; // standard message header
// int32 ZERO; // 0 - reserved for future use
// cstring fullCollectionName; // "dbname.collectionname"
// BSON[] documents; // one or more documents to insert into the collection
public Document[] Documents { get; set; }

private string fullCollectionName;
public string FullCollectionName {
get { return fullCollectionName; }
set { fullCollectionName = value; }
}

private Document[] documents;
public Document[] Documents {
get { return documents; }
set { documents = value; }
}

public InsertMessage(){
this.Header = new MessageHeader(OpCode.Insert);
}
Expand Down
27 changes: 14 additions & 13 deletions MongoDBDriver/IO/KillCursorsMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ namespace MongoDB.Driver.IO
{
/// <summary>
/// Description of KillCursorsMessage.
/// </summary>
/// </summary>
/// <remarks>
/// struct {
/// MsgHeader header; // standard message header
/// int32 ZERO; // 0 - reserved for future use
/// int32 numberOfCursorIDs; // number of cursorIDs in message
/// int64[] cursorIDs; // array of cursorIDs to close
/// }
/// </remarks>
public class KillCursorsMessage:RequestMessage
{
// struct {
// MsgHeader header; // standard message header
// int32 ZERO; // 0 - reserved for future use
// int32 numberOfCursorIDs; // number of cursorIDs in message
// int64[] cursorIDs; // array of cursorIDs to close
// }
private long[] cursorIDs;
public long[] CursorIDs {
get { return cursorIDs; }
set { cursorIDs = value; }
}
{
public long[] CursorIDs { get; set; }

public KillCursorsMessage(){
this.Header = new MessageHeader(OpCode.KillCursors);
}

public KillCursorsMessage(long cursorID):this(){
this.CursorIDs = new long[]{cursorID};
}

public KillCursorsMessage(long[] cursorIDs):this(){
this.CursorIDs = cursorIDs;
}
Expand Down
11 changes: 1 addition & 10 deletions MongoDBDriver/IO/Message.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
using System.Text;

namespace MongoDB.Driver.IO
{
public class Message
{
protected UTF8Encoding encoding = new UTF8Encoding();

private MessageHeader header;
public MessageHeader Header {
get { return header; }
set { header = value; }
}
public MessageHeader Header { get; set; }
}

}
40 changes: 14 additions & 26 deletions MongoDBDriver/IO/MessageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,19 @@ namespace MongoDB.Driver.IO
/// In general, each Message consists of a standard message header followed by request-specific data.
/// </summary>
public class MessageHeader
{
private Int32 messageLength; // total size of the message, including the 4 bytes of length
public int MessageLength {
get { return messageLength; }
set { messageLength = value; }
}

private Int32 requestId; // client or database-generated identifier for this message
public int RequestId {
get { return requestId; }
set { requestId = value; }
}

private Int32 responseTo; // requestID from the original request (used in reponses from db)
public int ResponseTo {
get { return responseTo; }
set { responseTo = value; }
}

private OpCode opCode; // request type - see table below
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", MessageId = "Member")]
public OpCode OpCode {
get { return opCode; }
set { opCode = value; }
}
{
// total size of the message, including the 4 bytes of length
public int MessageLength { get; set; }

// client or database-generated identifier for this message
public int RequestId { get; set; }

// requestID from the original request (used in reponses from db)
public int ResponseTo { get; set; }

// request type - see table below
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", MessageId = "Member")]
public OpCode OpCode { get; set; }

public MessageHeader(OpCode opCode)
{
Expand All @@ -42,7 +30,7 @@ public MessageHeader(OpCode opCode)
}

public override String ToString(){
return "length:" + this.messageLength + " requestId:" + this.requestId + " responseTo:" + this.responseTo + " opCode:" + this.opCode;
return "length:" + this.MessageLength + " requestId:" + this.RequestId + " responseTo:" + this.ResponseTo + " opCode:" + this.OpCode;
}

}
Expand Down
23 changes: 12 additions & 11 deletions MongoDBDriver/IO/MsgMessage.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using MongoDB.Driver.Bson;

namespace MongoDB.Driver.IO
{
{
/// <summary>
///
/// </summary>
/// <remarks>
/// struct {
/// MsgHeader header; // standard message header
/// cstring message; // message for the database
/// }
/// </remarks>
public class MsgMessage : RequestMessage
{
// struct {
// MsgHeader header; // standard message header
// cstring message; // message for the database
// }
private string message;
public string Message {
get {return message;}
set {message = value;}
}

public string Message { get; set; }

public MsgMessage(){
this.Header = new MessageHeader(OpCode.Msg);
}
Expand Down
85 changes: 28 additions & 57 deletions MongoDBDriver/IO/QueryMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,32 @@
using MongoDB.Driver.Bson;

namespace MongoDB.Driver.IO
{
/// <summary>
/// Description of QueryMessage.
/// </summary>
{
/// <summary>
/// Description of QueryMessage.
/// </summary>
/// <remarks>
/// MsgHeader header; // standard message header
/// int32 opts; // query options. See QueryOptions for values
/// cstring fullCollectionName; // "dbname.collectionname"
/// int32 numberToSkip; // number of documents to skip when returning results
/// int32 numberToReturn; // number of documents to return in the first OP_REPLY
/// BSON query ; // query object. See below for details.
/// [ BSON returnFieldSelector; ] // OPTIONAL : selector indicating the fields to return. See below for details.
/// </remarks>
public class QueryMessage : RequestMessage
{
// MsgHeader header; // standard message header
// int32 opts; // query options. See QueryOptions for values
// cstring fullCollectionName; // "dbname.collectionname"
// int32 numberToSkip; // number of documents to skip when returning results
// int32 numberToReturn; // number of documents to return in the first OP_REPLY
// BSON query ; // query object. See below for details.
// [ BSON returnFieldSelector; ] // OPTIONAL : selector indicating the fields to return. See below for details.

#region "Properties"
private QueryOptions options;
public QueryOptions Options {
get { return options; }
set { options = value; }
}

private string fullCollectionName;
public string FullCollectionName {
get { return fullCollectionName; }
set { fullCollectionName = value; }
}

private Int32 numberToSkip;
public int NumberToSkip {
get { return numberToSkip; }
set { numberToSkip = value; }
}

private Int32 numberToReturn;
public int NumberToReturn {
get { return numberToReturn; }
set { numberToReturn = value; }
}

private Document query;
public Document Query {
get { return query; }
set { query = value; }
}

private Document returnFieldSelector;
public Document ReturnFieldSelector {
get { return returnFieldSelector; }
set { returnFieldSelector = value; }
}

#endregion

#region "Ctors"
public QueryOptions Options { get; set; }

public string FullCollectionName { get; set; }

public int NumberToSkip { get; set; }

public int NumberToReturn { get; set; }

public Document Query { get; set; }

public Document ReturnFieldSelector { get; set; }

public QueryMessage(){
this.Header = new MessageHeader(OpCode.Query);
Expand All @@ -74,12 +46,11 @@ public QueryMessage(Document query, String fullCollectionName, Int32 numberToRet
this.Header = new MessageHeader(OpCode.Query);
this.Query = query;
this.FullCollectionName = fullCollectionName;
this.NumberToReturn = numberToReturn;
this.NumberToSkip = NumberToSkip;
this.NumberToReturn = numberToReturn;
this.NumberToSkip = numberToSkip;
this.ReturnFieldSelector = returnFieldSelector;
}
#endregion

}

protected override void WriteBody (BsonWriter writer){
writer.WriteValue(BsonDataType.Integer,(int)this.Options);
writer.WriteString(this.FullCollectionName);
Expand Down
Loading

0 comments on commit f639dc3

Please sign in to comment.