[GODRIVER-664] Add 'HasNext' method to Cursor type Created: 29/Nov/18  Updated: 03/Dec/18  Resolved: 03/Dec/18

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Minor - P4
Reporter: David Golden Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Because cursors are lazy, there's currently no way to know if the initial query fails until the first document is retrieved.  The shell and some drivers offer a  hasNext method on cursors that eagerly evaluates the query.

There are some cases where knowing the success of a query in advance of iteration simplifies the user's code. For example, one part of the code might execute the query (and handle an error) and pass the iterator to another part for processing.  Without a HasNext type function, one has to retrieve the first document to check for error and pass the first document plus the iterator for processing.

The semantics for HasNext should be:

  • If the query has not been sent, send it.
  • If we didn't just send the query and if the cursorId is non-zero and if the current batch has no documents remaining, send getMore to retrieve another batch.
  • If the cursor is in an error state, return false.
  • If there is at least one document remaining in the current batch, return true
  • Otherwise, return false.

By sending only a single query or getMore, we avoid doing more than one network roundtrip. This is the best answer to the question "what would happen if I called Next"?



 Comments   
Comment by Ian Whalen (Inactive) [ 03/Dec/18 ]

david.golden and kris.brandow talked offline and sorted out the confusion around whether cursors are lazy or not, so closing as Won't Fix.

Generated at Thu Feb 08 08:34:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.