Attach fields bson to lifetime of cursor

pull/9/head
Thijs Cadier 9 years ago
parent ddb8d32e30
commit c77cc42024

@ -129,8 +129,9 @@ impl<'a> Collection<'a> {
let default_options = CommandAndFindOptions::default();
let options = options.unwrap_or(&default_options);
let fields_bsonc = options.fields_bsonc();
let inner = unsafe {
let cursor_ptr = unsafe {
bindings::mongoc_collection_command(
self.inner,
options.query_flags.flags(),
@ -138,10 +139,8 @@ impl<'a> Collection<'a> {
options.limit,
options.batch_size,
try!(Bsonc::from_document(&command)).inner(),
match options.fields {
Some(ref f) => {
try!(Bsonc::from_document(f)).inner()
},
match fields_bsonc {
Some(ref f) => f.inner(),
None => ptr::null()
},
match options.read_prefs {
@ -151,11 +150,15 @@ impl<'a> Collection<'a> {
)
};
if inner.is_null() {
if cursor_ptr.is_null() {
return Err(InvalidParamsError.into())
}
Ok(Cursor::new(cursor::CreatedBy::Collection(self), inner))
Ok(Cursor::new(
cursor::CreatedBy::Collection(self),
cursor_ptr,
fields_bsonc
))
}
pub fn count(
@ -240,8 +243,9 @@ impl<'a> Collection<'a> {
let default_options = CommandAndFindOptions::default();
let options = options.unwrap_or(&default_options);
let fields_bsonc = options.fields_bsonc();
let inner = unsafe {
let cursor_ptr = unsafe {
bindings::mongoc_collection_find(
self.inner,
options.query_flags.flags(),
@ -249,10 +253,8 @@ impl<'a> Collection<'a> {
options.limit,
options.batch_size,
try!(Bsonc::from_document(query)).inner(),
match options.fields {
Some(ref f) => {
try!(Bsonc::from_document(f)).inner()
},
match fields_bsonc {
Some(ref f) => f.inner(),
None => ptr::null()
},
match options.read_prefs {
@ -262,11 +264,15 @@ impl<'a> Collection<'a> {
)
};
if inner.is_null() {
if cursor_ptr.is_null() {
return Err(InvalidParamsError.into())
}
Ok(Cursor::new(cursor::CreatedBy::Collection(self), inner))
Ok(Cursor::new(
cursor::CreatedBy::Collection(self),
cursor_ptr,
fields_bsonc
))
}
pub fn get_name(&self) -> Cow<str> {

@ -25,20 +25,25 @@ pub struct Cursor<'a> {
_created_by: CreatedBy<'a>,
inner: *mut bindings::mongoc_cursor_t,
tailing: bool,
tail_wait_time_ms: u32
tail_wait_time_ms: u32,
// Become owner of bsonc because the cursor needs it
// to be allocated for it's entire lifetime
_fields: Option<bsonc::Bsonc>
}
impl<'a> Cursor<'a> {
pub fn new(
created_by: CreatedBy<'a>,
inner: *mut bindings::mongoc_cursor_t
inner: *mut bindings::mongoc_cursor_t,
fields: Option<bsonc::Bsonc>
) -> Cursor<'a> {
assert!(!inner.is_null());
Cursor {
_created_by: created_by,
inner: inner,
tailing: false,
tail_wait_time_ms: 0
tail_wait_time_ms: 0,
_fields: fields
}
}

@ -45,8 +45,9 @@ impl<'a> Database<'a> {
let default_options = CommandAndFindOptions::default();
let options = options.unwrap_or(&default_options);
let fields_bsonc = options.fields_bsonc();
let inner = unsafe {
let cursor_ptr = unsafe {
bindings::mongoc_database_command(
self.inner,
options.query_flags.flags(),
@ -54,10 +55,8 @@ impl<'a> Database<'a> {
options.limit,
options.batch_size,
try!(Bsonc::from_document(&command)).inner(),
match options.fields {
Some(ref f) => {
try!(Bsonc::from_document(f)).inner()
},
match fields_bsonc {
Some(ref f) => f.inner(),
None => ptr::null()
},
match options.read_prefs {
@ -67,11 +66,15 @@ impl<'a> Database<'a> {
)
};
if inner.is_null() {
if cursor_ptr.is_null() {
return Err(InvalidParamsError.into())
}
Ok(Cursor::new(cursor::CreatedBy::Database(self), inner))
Ok(Cursor::new(
cursor::CreatedBy::Database(self),
cursor_ptr,
fields_bsonc
))
}
pub fn create_collection<S: Into<Vec<u8>>>(

@ -58,6 +58,13 @@ impl CommandAndFindOptions {
read_prefs: None
}
}
fn fields_bsonc(&self) -> Option<bsonc::Bsonc> {
match self.fields {
Some(ref f) => Some(bsonc::Bsonc::from_document(f).unwrap()),
None => None
}
}
}
#[cfg(test)]

Loading…
Cancel
Save