[CSHARP-24] Make Cursors reusable Created: 09/Mar/10  Updated: 19/Oct/16  Resolved: 14/May/10

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Sam Corder Assignee: Steve Wagner
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Using some of the Linq extensions like Count() will exhaust the cursor and cause unpredictable behavior if it is then used in a foreach loop.



 Comments   
Comment by Steve Wagner [ 14/May/10 ]

Fixed for next stable version. Development code can be found here. http://github.com/lanwin/mongodb-csharp/tree/typedcollections

Comment by Serge Knecht [ 05/May/10 ]

Problem summary:
When a query is performed a cursor is returned. In some cases when iterating through the cursor 2 times the 2nd time not all all documents are returned. Therefore the cursor.Documents from a cursor can only be used once in its lifetime.

Example : First time the following is done. cursor.Documents.Count() will return 677.
The 2nd and next times the same method is invoked on the same cursor instance cursor.Documents.Count() will return for example 511.

Reason:
when we iterate through the cursor.Documents the retrieval can be doen in several chunks (maybe because the data is in different physical files ?).
The first time upon entering the et from public IEnumerable<Document> Documents the is.reply == null and the method RetrieveData(); will be called.
This retrieves the first chunk of the dataset.
The remaining data (chuncks of data) will be retrieved using RetrieveMoreData();

BUG:
when the program now iterates a 2nd time this iteration by calling the cursor.Documents ... not the full dataset is returned.
This is because upon entering the get from public IEnumerable<Document> Documents the 2nd time this.reply is no longer NULL.
It contains the reply from the previous iteration.
Therefor RetrieveData(); is NOT run ... and only RetrieveMoreData(); is run ...
as a consequence it's only returning the documents from the last 'chunk'.

Suggestion to fix:
Probably the test on this.reply == null is done as a performance improvement however in this case it has an unwanted problem.
When iterating through the documents there should be a proper reset of its state or ... maybe simply remove the test on this.reply ? or ... ?

Comment by Steve Wagner [ 12/Apr/10 ]

What exactly that means?

Generated at Wed Feb 07 21:35:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.