diff --git a/source/MongoDB/Cursor_1.cs b/source/MongoDB/Cursor_1.cs index 8ee28aab..a7705afd 100644 --- a/source/MongoDB/Cursor_1.cs +++ b/source/MongoDB/Cursor_1.cs @@ -34,7 +34,6 @@ public class Cursor : ICursor where T : class public Cursor(ISerializationFactory serializationFactory, Connection connection, string fullCollectionName) { //Todo: should be internal - Id = -1; _connection = connection; FullCollectionName = fullCollectionName; _serializationFactory = serializationFactory; @@ -173,11 +172,10 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection, /// /// public Document Explain(){ - //Fixme Return a single Document and not T TryModify(); _specOpts["$explain"] = true; - var explainResult = GetQueryRepley(); + var explainResult = RetrieveData(); try { var explain = explainResult.Documents.FirstOrDefault(); @@ -207,33 +205,39 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection, /// /// The documents. public IEnumerable Documents { - get { - if (_reply == null) - RetrieveData(); - if (_reply == null) - throw new InvalidOperationException("Expecting reply but get null"); - - var documents = _reply.Documents; - var documentCount = 0; - var shouldBreak = false; - - while (!shouldBreak) { - foreach (var document in documents) - if ((_limit == 0) || (_limit != 0 && documentCount < _limit)) { - documentCount++; - yield return document; - } else - yield break; - - if (Id != 0) { - RetrieveMoreData(); - documents = _reply.Documents; - if (documents == null) - shouldBreak = true; - } else - shouldBreak = true; - } + get { + do + { + _reply = RetrieveData(); + + if(_reply == null) + throw new InvalidOperationException("Expecting reply but get null"); + + //if(_limit < 0) + //_limit = _limit * -1; + + Id = _reply.CursorId; + + foreach(var document in _reply.Documents) + yield return document; + } + while(Id > 0 && _limit + /// Gets the cursor position. + /// + /// The cursor position. + public int CursorPosition + { + get + { + if(_reply == null) + return 0; + + return _reply.StartingFrom + _reply.NumberReturned; + } } /// @@ -241,13 +245,23 @@ public Cursor(ISerializationFactory serializationFactory, Connection connection, /// public void Dispose() { - if (Id == 0) - //All server side resources disposed of. - return; + if(Id == 0) + return; //All server side resources disposed of. KillCursor(Id); } + /// + /// Optionses the specified options. + /// + /// The options. + /// + public ICursor Options(QueryOptions options){ + TryModify(); + _options = options; + return this; + } + /// /// Kills the cursor. /// @@ -256,87 +270,54 @@ private void KillCursor(long cursorId) var killCursorsMessage = new KillCursorsMessage(cursorId); try { - _connection.SendMessage(killCursorsMessage); + _connection.SendMessage(killCursorsMessage); Id = 0; } catch (IOException exception) { throw new MongoConnectionException("Could not read data, communication failure", _connection, exception); } } - /// - /// Optionses the specified options. - /// - /// The options. - /// - public ICursor Options(QueryOptions options){ - TryModify(); - _options = options; - return this; - } - /// - /// Gets the query repley. + /// Retrieves the data. /// /// The type of the reply. /// - private ReplyMessage GetQueryRepley() where TReply : class + private ReplyMessage RetrieveData() where TReply : class { - var writerSettings = _serializationFactory.GetBsonWriterSettings(typeof(T)); + _isModifiable = false; + + IRequestMessage message; - var query = new QueryMessage(writerSettings) + if(Id <= 0) { - FullCollectionName = FullCollectionName, - Query = BuildSpec(), - NumberToReturn = _limit, - NumberToSkip = _skip, - Options = _options - }; + var writerSettings = _serializationFactory.GetBsonWriterSettings(typeof(T)); - if(_fields != null) - query.ReturnFieldSelector = _fields; + message = new QueryMessage(writerSettings) + { + FullCollectionName = FullCollectionName, + Query = BuildSpec(), + NumberToReturn = _limit, + NumberToSkip = _skip, + Options = _options, + ReturnFieldSelector = _fields + }; + } + else + { + message = new GetMoreMessage(FullCollectionName, Id, _limit); + } var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T)); try { - return _connection.SendTwoWayMessage(query, readerSettings); + return _connection.SendTwoWayMessage(message, readerSettings); } catch(IOException exception) { throw new MongoConnectionException("Could not read data, communication failure", _connection, exception); } } - - /// - /// Retrieves the data. - /// - private void RetrieveData(){ - _reply = GetQueryRepley(); - - Id = _reply.CursorId; - - if(_limit < 0) - _limit = _limit * -1; - - _isModifiable = false; - } - - /// - /// Retrieves the more data. - /// - private void RetrieveMoreData(){ - var getMoreMessage = new GetMoreMessage(FullCollectionName, Id, _limit); - - var readerSettings = _serializationFactory.GetBsonReaderSettings(typeof(T)); - - try { - _reply = _connection.SendTwoWayMessage(getMoreMessage, readerSettings); - Id = _reply.CursorId; - } catch (IOException exception) { - Id = 0; - throw new MongoConnectionException("Could not read data, communication failure", _connection, exception); - } - } /// /// Tries the modify. diff --git a/source/MongoDB/Obsolete/Cursor.cs b/source/MongoDB/Obsolete/Cursor.cs index c6c686de..30ad7dd8 100644 --- a/source/MongoDB/Obsolete/Cursor.cs +++ b/source/MongoDB/Obsolete/Cursor.cs @@ -4,7 +4,7 @@ namespace MongoDB { /// /// - /// + /// public class Cursor : ICursor { private readonly ICursor _cursor;