Skip to content

Commit

Permalink
Fix Explain and Hint.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 6, 2010
1 parent a203331 commit 7b25618
Showing 1 changed file with 75 additions and 44 deletions.
119 changes: 75 additions & 44 deletions source/MongoDB/Cursor_1.cs
@@ -1,19 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using MongoDB.Bson;
using MongoDB.Connections;
using MongoDB.Protocol;
using MongoDB.Serialization;

using MongoDB.Serialization;
using System.Linq;

namespace MongoDB
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class Cursor<T> : ICursor<T> where T : class
{
{
private readonly Connection _connection;
private readonly Document _specOpts = new Document();
private bool _isModifiable = true;
Expand Down Expand Up @@ -175,17 +175,23 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
public Document Explain(){
//Fixme Return a single Document and not T
TryModify();
_specOpts["$explain"] = true;

// var documents = Documents;

return null;
// using((IDisposable)documents){
// foreach(var document in documents)
// return document;
// }

// throw new InvalidOperationException("Explain failed.");
_specOpts["$explain"] = true;

var explainResult = GetQueryRepley<Document>();
try
{
var explain = explainResult.Documents.FirstOrDefault();

if(explain==null)
throw new InvalidOperationException("Explain failed. No documents where returned.");

return explain;
}
finally
{
if(explainResult.CursorId > 0)
KillCursor(explainResult.CursorId);
}
}

/// <summary>
Expand Down Expand Up @@ -233,21 +239,30 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose(){
public void Dispose()
{
if (Id == 0)
//All server side resources disposed of.
return;

var killCursorsMessage = new KillCursorsMessage(Id);
return;

KillCursor(Id);
}

/// <summary>
/// Kills the cursor.
/// </summary>
private void KillCursor(long cursorId)
{
var killCursorsMessage = new KillCursorsMessage(cursorId);

try {
_connection.SendMessage(killCursorsMessage);
Id = 0;
} catch (IOException exception) {
throw new MongoConnectionException("Could not read data, communication failure", _connection, exception);
}
}

}
}

/// <summary>
/// Optionses the specified options.
/// </summary>
Expand All @@ -257,12 +272,15 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
TryModify();
_options = options;
return this;
}

/// <summary>
/// Retrieves the data.
/// </summary>
private void RetrieveData(){
}

/// <summary>
/// Gets the query repley.
/// </summary>
/// <typeparam name="TReply">The type of the reply.</typeparam>
/// <returns></returns>
private ReplyMessage<TReply> GetQueryRepley<TReply>() where TReply : class
{
var writerSettings = _serializationFactory.GetBsonWriterSettings(typeof(T));

var query = new QueryMessage(writerSettings)
Expand All @@ -272,22 +290,35 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection,
NumberToReturn = _limit,
NumberToSkip = _skip,
Options = _options
};

if (_fields != null)
query.ReturnFieldSelector = _fields;

var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T));

try {
_reply = _connection.SendTwoWayMessage<T>(query, readerSettings);
Id = _reply.CursorId;
if (_limit < 0)
_limit = _limit * -1;
_isModifiable = false;
} catch (IOException exception) {
throw new MongoConnectionException("Could not read data, communication failure", _connection, exception);
}
};

if(_fields != null)
query.ReturnFieldSelector = _fields;

var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T));

try
{
return _connection.SendTwoWayMessage<TReply>(query, readerSettings);
}
catch(IOException exception)
{
throw new MongoConnectionException("Could not read data, communication failure", _connection, exception);
}
}

/// <summary>
/// Retrieves the data.
/// </summary>
private void RetrieveData(){
_reply = GetQueryRepley<T>();

Id = _reply.CursorId;

if(_limit < 0)
_limit = _limit * -1;

_isModifiable = false;
}

/// <summary>
Expand Down

0 comments on commit 7b25618

Please sign in to comment.