moped icon indicating copy to clipboard operation
moped copied to clipboard

Add @cache to Moped::Cursor for out-of-block iteration

Open fancyremarker opened this issue 11 years ago • 4 comments

Caveat: This is potentially very risky, so feel free to reject. I'm not familiar enough with Mongoid to understand all the implications of this change. That said, we've been using it in production for some time without any apparent ill effects.

The Mongo Ruby driver (and thus Mongoid 2.x) supported iterating over a cursor outside a block, using such operations as take(). For example:

batch_size = 10
cursor = session[:users].find.cursor
while (users = cursor.take(batch_size)).present?
  # Do stuff...
end
# Do more stuff...

This was a convenient pattern for implementing lots of algorithms in a more readable way than if they were written using each(). This patch reintroduces this convenience.

cc: @dblock

fancyremarker avatar Nov 27 '13 17:11 fancyremarker

Coverage Status

Coverage remained the same when pulling 2d854a0b518459c841d292ec6f3ca03d43e789ef on fancyremarker:cursor-cache-updated into 073e30bbc49f10d71a1ff3c8462f779f457e28f0 on mongoid:master.

coveralls avatar Nov 27 '13 17:11 coveralls

Coverage Status

Coverage decreased (-0.0%) when pulling 2d854a0b518459c841d292ec6f3ca03d43e789ef on fancyremarker:cursor-cache-updated into 073e30bbc49f10d71a1ff3c8462f779f457e28f0 on mongoid:master.

coveralls avatar Nov 27 '13 17:11 coveralls

@fancyremarker looks like Travis build failed, not sure if this is introduced by this change

dblock avatar Dec 02 '13 02:12 dblock

@dblock: I couldn't see how it was connected, but that's because of my own ignorance. FWIW, specs did pass locally for me.

fancyremarker avatar Dec 02 '13 02:12 fancyremarker