Permalink
Browse files

FIX MAYOR BUG: Limit returns wrong result for large resultsets.

  • Loading branch information...
1 parent 1e98fb5 commit 9b7d930b26d81b329b0df1584a9895deba50a14c @lanwin lanwin committed Sep 28, 2010
Showing with 29 additions and 8 deletions.
  1. +18 −6 source/MongoDB.Tests/IntegrationTests/TestCursor.cs
  2. +11 −2 source/MongoDB/Cursor_1.cs
@@ -9,7 +9,7 @@ public class TestCursor : MongoTestBase
{
public override string TestCollections
{
- get { return "sorts,hintindex,smallreads,reads"; }
+ get { return "sorts,hintindex,smallreads,reads,largereads"; }
}
public override void OnInit()
@@ -23,6 +23,11 @@ public override void OnInit()
var reads = DB["reads"];
for(var j = 1; j < 10000; 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]
@@ -37,6 +42,14 @@ public void TestCanLimit()
}
[Test]
+ public void TestCanLimitWithLargeResultSet()
+ {
+ var count = DB["largereads"].FindAll().Limit(2000).Documents.Count();
+
+ Assert.AreEqual(2000,count);
+ }
+
+ [Test]
public void TestCanReadAndKillCursor()
{
var c = (Cursor)DB["reads"].FindAll();
@@ -59,11 +72,10 @@ public void TestCanReadMore()
foreach(var doc in c.Documents)
{
reads++;
- if(c.Id != id)
- {
- idchanges++;
- id = c.Id;
- }
+ if(c.Id == id)
+ continue;
+ idchanges++;
+ id = c.Id;
}
Assert.IsTrue(reads > 0, "No documents were returned.");
Assert.IsTrue(idchanges > 0, String.Format("ReadMore message never sent. {0} changes seen", idchanges));
View
@@ -237,18 +237,27 @@ public ICursor<T> KeepCursor(bool value)
/// </summary>
/// <value>The documents.</value>
public IEnumerable<T> Documents {
- get {
+ get
+ {
do
{
_reply = RetrieveData<T>();
if(_reply == 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)
yield return document;
}
- while(Id > 0 && _limit<CursorPosition);
+ while(Id > 0);
if(!_keepCursor)
Dispose(true);

0 comments on commit 9b7d930

Please sign in to comment.