Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 9b7d930b26d81b329b0df1584a9895deba50a14c 1 parent 1e98fb5
Steve Wagner authored September 28, 2010
24  source/MongoDB.Tests/IntegrationTests/TestCursor.cs
@@ -9,7 +9,7 @@ public class TestCursor : MongoTestBase
9 9
     {
10 10
         public override string TestCollections
11 11
         {
12  
-            get { return "sorts,hintindex,smallreads,reads"; }
  12
+            get { return "sorts,hintindex,smallreads,reads,largereads"; }
13 13
         }
14 14
 
15 15
         public override void OnInit()
@@ -23,6 +23,11 @@ public override void OnInit()
23 23
             var reads = DB["reads"];
24 24
             for(var j = 1; j < 10000; j++)
25 25
                 reads.Insert(new Document {{"x", 4}, {"h", "hi"}, {"j", j}});
  26
+
  27
+            var properties = Enumerable.Range(1, 500).ToDictionary(x => x.ToString(), x => (object)x).ToArray();
  28
+            var largereads = DB["largereads"];
  29
+            largereads.Insert(Enumerable.Range(1, 3000).Select(i => new Document(properties)));
  30
+
26 31
         }
27 32
 
28 33
         [Test]
@@ -37,6 +42,14 @@ public void TestCanLimit()
37 42
         }
38 43
 
39 44
         [Test]
  45
+        public void TestCanLimitWithLargeResultSet()
  46
+        {
  47
+            var count = DB["largereads"].FindAll().Limit(2000).Documents.Count();
  48
+
  49
+            Assert.AreEqual(2000,count);
  50
+        }
  51
+
  52
+        [Test]
40 53
         public void TestCanReadAndKillCursor()
41 54
         {
42 55
             var c = (Cursor)DB["reads"].FindAll();
@@ -59,11 +72,10 @@ public void TestCanReadMore()
59 72
             foreach(var doc in c.Documents)
60 73
             {
61 74
                 reads++;
62  
-                if(c.Id != id)
63  
-                {
64  
-                    idchanges++;
65  
-                    id = c.Id;
66  
-                }
  75
+                if(c.Id == id)
  76
+                    continue;
  77
+                idchanges++;
  78
+                id = c.Id;
67 79
             }
68 80
             Assert.IsTrue(reads > 0, "No documents were returned.");
69 81
             Assert.IsTrue(idchanges > 0, String.Format("ReadMore message never sent. {0} changes seen", idchanges));
13  source/MongoDB/Cursor_1.cs
@@ -237,7 +237,8 @@ public ICursor<T> KeepCursor(bool value)
237 237
         /// </summary>
238 238
         /// <value>The documents.</value>
239 239
         public IEnumerable<T> Documents {
240  
-            get {
  240
+            get
  241
+            {
241 242
                 do
242 243
                 {
243 244
                     _reply = RetrieveData<T>();
@@ -245,10 +246,18 @@ public ICursor<T> KeepCursor(bool value)
245 246
                     if(_reply == null)
246 247
                         throw new InvalidOperationException("Expecting reply but get null");
247 248
 
  249
+                    if(_limit > 0 && CursorPosition > _limit)
  250
+                    {
  251
+                        foreach(var document in _reply.Documents.Take(_limit - _reply.StartingFrom))
  252
+                            yield return document;
  253
+                        
  254
+                        yield break;
  255
+                    }
  256
+
248 257
                     foreach(var document in _reply.Documents)
249 258
                         yield return document;
250 259
                 }
251  
-                while(Id > 0 && _limit<CursorPosition);
  260
+                while(Id > 0);
252 261
 
253 262
                 if(!_keepCursor)
254 263
                     Dispose(true);

0 notes on commit 9b7d930

Please sign in to comment.
Something went wrong with that request. Please try again.