Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-1851

Restore cursor's ability to be iterated more than once

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 2.10.0.rc0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Fully Compatible

      One of the Mongoid query cache tests broke when the cursor implementation was changed to allow iterating the cursor one time only. We need to restore the old behavior where the cursor could be iterated more than once.

      The old behavior was buggy when a getMore was issued, because the following would happen:

      1. The driver would return documents from the initial result.
      2. The driver would issue another getMore to advance the cursor forward and return those documents.

      Any documents in the batches that had been iterated via prior getMore calls would be lost on subsequent restarted iterations.

      The change in this ticket is to reinstate repeated iteration as long as only the first batch is iterated. Trying to restart iteration after a get_more was invoked on a cursor object would now raise a NotImplementedError.

      In a future version of the driver we may restore the "early 2.10" behavior of always iterating forward from cursor position. The alternative is to support always restarting iteration from the beginning, but this requires issuing initial query again which is more complicated and perhaps outside the scope of the cursor.

      To further clarify, the case in question is as follows:

      cursor = ...
      cursor.each do |doc|
        # retrieve one document and stop
        break
      end
      cursor.to_a
      

      In pre-2.10 drivers, to_a would return the document already retrieved by #each. In "early 2.10" drivers, to_a would skip that document. After this ticket is done, the final 2.10 driver would return these documents again.

      The second case:

      cursor = ... # batch size = 2
      cursor.each do |doc|
        # retrieve three documents and stop
        if ...
           break  
        end
      end
      cursor.to_a
      

      In pre-2.10 drivers, this would drop the second batch of results (documents #3 and #4). In 2.10 final, this would raise NotImplementedError.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: