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

Commit

Permalink
FIX MAYOR BUG: Limit returns wrong result for large resultsets.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed Sep 28, 2010
1 parent 1e98fb5 commit 9b7d930
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
24 changes: 18 additions & 6 deletions source/MongoDB.Tests/IntegrationTests/TestCursor.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TestCursor : MongoTestBase
{ {
public override string TestCollections public override string TestCollections
{ {
get { return "sorts,hintindex,smallreads,reads"; } get { return "sorts,hintindex,smallreads,reads,largereads"; }
} }


public override void OnInit() public override void OnInit()
Expand All @@ -23,6 +23,11 @@ public override void OnInit()
var reads = DB["reads"]; var reads = DB["reads"];
for(var j = 1; j < 10000; j++) for(var j = 1; j < 10000; j++)
reads.Insert(new Document {{"x", 4}, {"h", "hi"}, {"j", j}}); reads.Insert(new Document {{"x", 4}, {"h", "hi"}, {"j", j}});

var properties = Enumerable.Range(1, 500).ToDictionary(x => x.ToString(), x => (object)x).ToArray();
var largereads = DB["largereads"];
largereads.Insert(Enumerable.Range(1, 3000).Select(i => new Document(properties)));

} }


[Test] [Test]
Expand All @@ -36,6 +41,14 @@ public void TestCanLimit()
Assert.AreEqual(5, reads); Assert.AreEqual(5, reads);
} }


[Test]
public void TestCanLimitWithLargeResultSet()
{
var count = DB["largereads"].FindAll().Limit(2000).Documents.Count();

Assert.AreEqual(2000,count);
}

[Test] [Test]
public void TestCanReadAndKillCursor() public void TestCanReadAndKillCursor()
{ {
Expand All @@ -59,11 +72,10 @@ public void TestCanReadMore()
foreach(var doc in c.Documents) foreach(var doc in c.Documents)
{ {
reads++; reads++;
if(c.Id != id) if(c.Id == id)
{ continue;
idchanges++; idchanges++;
id = c.Id; id = c.Id;
}
} }
Assert.IsTrue(reads > 0, "No documents were returned."); Assert.IsTrue(reads > 0, "No documents were returned.");
Assert.IsTrue(idchanges > 0, String.Format("ReadMore message never sent. {0} changes seen", idchanges)); Assert.IsTrue(idchanges > 0, String.Format("ReadMore message never sent. {0} changes seen", idchanges));
Expand Down
13 changes: 11 additions & 2 deletions source/MongoDB/Cursor_1.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -237,18 +237,27 @@ public Document Explain(){
/// </summary> /// </summary>
/// <value>The documents.</value> /// <value>The documents.</value>
public IEnumerable<T> Documents { public IEnumerable<T> Documents {
get { get
{
do do
{ {
_reply = RetrieveData<T>(); _reply = RetrieveData<T>();


if(_reply == null) if(_reply == null)
throw new InvalidOperationException("Expecting reply but get null"); throw new InvalidOperationException("Expecting reply but get null");


if(_limit > 0 && CursorPosition > _limit)
{
foreach(var document in _reply.Documents.Take(_limit - _reply.StartingFrom))
yield return document;

yield break;
}

foreach(var document in _reply.Documents) foreach(var document in _reply.Documents)
yield return document; yield return document;
} }
while(Id > 0 && _limit<CursorPosition); while(Id > 0);


if(!_keepCursor) if(!_keepCursor)
Dispose(true); Dispose(true);
Expand Down

0 comments on commit 9b7d930

Please sign in to comment.