Skip to content

RUST-1046 Fix iteration of cursors when batchSize doesn't divide result size #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/cursor/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub struct SessionCursor<T>
where
T: DeserializeOwned + Unpin,
{
exhausted: bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue here is that the GenericCursor created in stream was using the info.id field to determine if the cursor was exhausted rather than this exhausted field, which would cause it to iterate past the end and return an error. This was fixed by using info.id as the single source of truth as to whether the SessionCursor is exhausted or not.

client: Client,
info: CursorInformation,
buffer: VecDeque<T>,
Expand All @@ -79,10 +78,7 @@ where
spec: CursorSpecification<T>,
pinned: Option<PinnedConnectionHandle>,
) -> Self {
let exhausted = spec.id() == 0;

Self {
exhausted,
client,
info: spec.info,
buffer: spec.initial_buffer,
Expand Down Expand Up @@ -198,12 +194,25 @@ where
}
}

impl<T> SessionCursor<T>
where
T: DeserializeOwned + Unpin,
{
fn mark_exhausted(&mut self) {
self.info.id = 0;
}

fn is_exhausted(&self) -> bool {
self.info.id == 0
}
}

impl<T> Drop for SessionCursor<T>
where
T: DeserializeOwned + Unpin,
{
fn drop(&mut self) {
if self.exhausted {
if self.is_exhausted() {
return;
}

Expand Down Expand Up @@ -254,7 +263,9 @@ where
fn drop(&mut self) {
// Update the parent cursor's state based on any iteration performed on this handle.
self.session_cursor.buffer = self.generic_cursor.take_buffer();
self.session_cursor.exhausted = self.generic_cursor.is_exhausted();
if self.generic_cursor.is_exhausted() {
self.session_cursor.mark_exhausted();
}
}
}

Expand Down
43 changes: 43 additions & 0 deletions src/test/coll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,46 @@ async fn collection_generic_bounds() {
.collection(function_name!());
let _result = coll.insert_one(Bar {}, None).await;
}

/// Verify that a cursor with multiple batches whose last batch isn't full
/// iterates without errors.
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
async fn cursor_batch_size() {
let _guard: RwLockReadGuard<()> = LOCK.run_concurrently().await;

let client = TestClient::new().await;
let coll = client
.init_db_and_coll("cursor_batch_size", "cursor_batch_size")
.await;

let doc = Document::new();
coll.insert_many(vec![&doc; 10], None).await.unwrap();

let opts = FindOptions::builder().batch_size(3).build();
let cursor_no_session = coll.find(doc! {}, opts.clone()).await.unwrap();
let docs: Vec<_> = cursor_no_session.try_collect().await.unwrap();
assert_eq!(docs.len(), 10);

// test session cursors
if client.is_standalone() {
return;
}
let mut session = client.start_session(None).await.unwrap();
let mut cursor = coll
.find_with_session(doc! {}, opts.clone(), &mut session)
.await
.unwrap();
let mut docs = Vec::new();
while let Some(doc) = cursor.next(&mut session).await {
docs.push(doc.unwrap());
}
assert_eq!(docs.len(), 10);

let mut cursor = coll
.find_with_session(doc! {}, opts, &mut session)
.await
.unwrap();
let docs: Vec<_> = cursor.stream(&mut session).try_collect().await.unwrap();
assert_eq!(docs.len(), 10);
}