Skip to content

Commit

Permalink
Cursor nextBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Hannan committed Jul 21, 2011
1 parent 5e4a8ae commit 58f8383
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Database/MongoDB/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module Database.MongoDB.Query (
Query(..), QueryOption(NoCursorTimeout), Projector, Limit, Order, BatchSize,
explain, find, findOne, fetch, count, distinct,
-- *** Cursor
Cursor, next, nextN, rest, closeCursor, isCursorClosed,
Cursor, nextBatch, next, nextN, rest, closeCursor, isCursorClosed,
-- ** Group
Group(..), GroupKey(..), group,
-- ** MapReduce
Expand Down Expand Up @@ -472,6 +472,17 @@ newCursor db col batchSize dBatch = do
addMVarFinalizer var (closeCursor cursor)
return cursor

nextBatch :: (MonadMVar m) => Cursor -> Action m [Document]
-- ^ Return next batch of documents in query result, which will be empty if finished.
nextBatch (Cursor fcol batchSize var) = modifyMVar var $ \dBatch -> do
-- Pre-fetch next batch promise from server and return current batch.
Batch limit cid docs <- fulfill dBatch
dBatch' <- if cid /= 0 then nextBatch' limit cid else return $ return (Batch 0 0 [])
return (dBatch', docs)
where
nextBatch' limit cid = request [] (GetMore fcol batchSize' cid, remLimit)
where (batchSize', remLimit) = batchSizeRemainingLimit batchSize limit

next :: (MonadMVar m) => Cursor -> Action m (Maybe Document)
-- ^ Return next document in query result, or Nothing if finished.
next (Cursor fcol batchSize var) = modifyMVar var nextState where
Expand All @@ -482,13 +493,13 @@ next (Cursor fcol batchSize var) = modifyMVar var nextState where
case docs of
doc : docs' -> do
dBatch' <- if null docs' && cid /= 0
then nextBatch limit cid
then nextBatch' limit cid
else return $ return (Batch limit cid docs')
return (dBatch', Just doc)
[] -> if cid == 0
then return (return $ Batch 0 0 [], Nothing) -- finished
else error $ "server returned empty batch but says more results on server"
nextBatch limit cid = request [] (GetMore fcol batchSize' cid, remLimit)
nextBatch' limit cid = request [] (GetMore fcol batchSize' cid, remLimit)
where (batchSize', remLimit) = batchSizeRemainingLimit batchSize limit

nextN :: (MonadMVar m, Functor m) => Int -> Cursor -> Action m [Document]
Expand Down

0 comments on commit 58f8383

Please sign in to comment.