Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit bea146b

Browse files
authored
fix default batch size (#211)
1 parent fb204d6 commit bea146b

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

src/cursor.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ use wire_protocol::operations::Message;
3636

3737
use std::collections::vec_deque::VecDeque;
3838

39-
pub const DEFAULT_BATCH_SIZE: i32 = 20;
39+
// Allows the server to decide the batch size.
40+
pub const DEFAULT_BATCH_SIZE: i32 = 0;
4041

4142
/// Maintains a connection to the server and lazily returns documents from a
4243
/// query.
@@ -457,14 +458,31 @@ impl Cursor {
457458
Ok(vec)
458459
}
459460

461+
/// # Return value
462+
///
463+
/// Returns a vector containing the BSON documents that were read.
464+
#[deprecated(since="0.2.8", note="this method uses 20 as the default batch size instead of letting the server decide; using `drain_current_batch` is recommended instead")]
465+
pub fn next_batch(&mut self) -> Result<Vec<bson::Document>> {
466+
let batch_size = if self.batch_size == 0 {
467+
20
468+
} else {
469+
self.batch_size
470+
};
471+
472+
self.next_n(batch_size)
473+
}
474+
460475
/// Attempts to read a batch of BSON documents from the cursor.
461476
///
462477
/// # Return value
463478
///
464479
/// Returns a vector containing the BSON documents that were read.
465-
pub fn next_batch(&mut self) -> Result<Vec<bson::Document>> {
466-
let n = self.batch_size;
467-
self.next_n(n)
480+
pub fn drain_current_batch(&mut self) -> Result<Vec<bson::Document>> {
481+
if self.buffer.is_empty() {
482+
self.get_from_stream()?;
483+
}
484+
485+
Ok(self.buffer.drain(..).collect())
468486
}
469487

470488
/// Checks whether there are any more documents for the cursor to return.

src/gridfs/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ impl FileCursor {
7676
}
7777

7878
/// Returns the next batch of files.
79-
pub fn next_batch(&mut self) -> Result<Vec<File>> {
80-
let docs = try!(self.cursor.next_batch());
79+
pub fn drain_current_batch(&mut self) -> Result<Vec<File>> {
80+
let docs = try!(self.cursor.drain_current_batch());
8181
Ok(docs.into_iter()
8282
.map(|doc| File::with_doc(self.store.clone(), doc))
8383
.collect())

tests/client/cursor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn cursor_features() {
4343
Err(s) => panic!("{}", s),
4444
};
4545

46-
let batch = cursor.next_batch().expect("Failed to get next batch from cursor.");
46+
let batch = cursor.drain_current_batch().expect("Failed to get current batch from cursor.");
4747

4848
assert_eq!(batch.len(), 3 as usize);
4949

tests/client/gridfs.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn put_get() {
5656
// Check chunks
5757
let mut cursor = fschunks.find(Some(doc!{"files_id" => (id.clone())}), Some(opts)).unwrap();
5858

59-
let chunks = cursor.next_batch().expect("Failed to get next batch");
59+
let chunks = cursor.drain_current_batch().expect("Failed to get current batch");
6060
assert_eq!(3, chunks.len());
6161

6262
for (i, chunk) in chunks.iter().enumerate().take(3) {
@@ -114,14 +114,14 @@ fn remove() {
114114
assert!(fsfiles.find_one(Some(doc!{"_id" => (id.clone())}), None).unwrap().is_some());
115115

116116
let mut cursor = fschunks.find(Some(doc!{"files_id" => (id.clone())}), None).unwrap();
117-
let results = cursor.next_batch().unwrap();
117+
let results = cursor.drain_current_batch().unwrap();
118118
assert_eq!(2, results.len());
119119

120120
fs.remove(name.to_owned()).unwrap();
121121
assert!(fsfiles.find_one(Some(doc!{"_id" => (id.clone())}), None).unwrap().is_none());
122122

123123
let mut cursor = fschunks.find(Some(doc!{"files_id" => (id.clone())}), None).unwrap();
124-
let results = cursor.next_batch().unwrap();
124+
let results = cursor.drain_current_batch().unwrap();
125125
assert_eq!(0, results.len());
126126
}
127127

@@ -141,12 +141,12 @@ fn remove_id() {
141141
assert!(fsfiles.find_one(Some(doc!{"_id" => (id.clone())}), None).unwrap().is_some());
142142

143143
let mut cursor = fschunks.find(Some(doc!{"files_id" => (id.clone())}), None).unwrap();
144-
let results = cursor.next_batch().unwrap();
144+
let results = cursor.drain_current_batch().unwrap();
145145
assert_eq!(2, results.len());
146146

147147
fs.remove_id(id.clone()).unwrap();
148148
let mut cursor = fschunks.find(Some(doc!{"files_id" => (id.clone())}), None).unwrap();
149-
let results = cursor.next_batch().unwrap();
149+
let results = cursor.drain_current_batch().unwrap();
150150
assert_eq!(0, results.len());
151151
}
152152

@@ -171,7 +171,7 @@ fn find() {
171171
grid_file2.close().unwrap();
172172

173173
let mut cursor = fs.find(None, None).unwrap();
174-
let results = cursor.next_batch().unwrap();
174+
let results = cursor.drain_current_batch().unwrap();
175175
assert_eq!(2, results.len());
176176
assert_eq!(name, results[0].name.as_ref().unwrap());
177177
assert_eq!(name2, results[1].name.as_ref().unwrap());

0 commit comments

Comments
 (0)