commit
f6ea1ddb15
@ -1,196 +0,0 @@
|
||||
use mongoc::bindings;
|
||||
use bson::Document;
|
||||
|
||||
use super::BsoncError;
|
||||
use super::bsonc::Bsonc;
|
||||
use super::collection::Collection;
|
||||
|
||||
use super::Result;
|
||||
|
||||
pub struct BulkOperation<'a> {
|
||||
_collection: &'a Collection<'a>,
|
||||
inner: *mut bindings::mongoc_bulk_operation_t
|
||||
}
|
||||
|
||||
impl<'a> BulkOperation<'a> {
|
||||
pub fn new(
|
||||
collection: &'a Collection<'a>,
|
||||
inner: *mut bindings::mongoc_bulk_operation_t
|
||||
) -> BulkOperation<'a> {
|
||||
assert!(!inner.is_null());
|
||||
BulkOperation {
|
||||
_collection: collection,
|
||||
inner: inner
|
||||
}
|
||||
}
|
||||
|
||||
/// Queue an insert of a single document into a bulk operation.
|
||||
/// The insert is not performed until `execute` is called.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_bulk_operation_insert.html
|
||||
pub fn insert(
|
||||
&self,
|
||||
document: &Document
|
||||
) -> Result<()> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_bulk_operation_insert(
|
||||
self.inner,
|
||||
try!(Bsonc::from_document(&document)).inner()
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Queue removal of al documents matching selector into a bulk operation.
|
||||
/// The removal is not performed until `execute` is called.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_bulk_operation_remove.html
|
||||
pub fn remove(
|
||||
&self,
|
||||
selector: &Document
|
||||
) -> Result<()> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_bulk_operation_remove(
|
||||
self.inner,
|
||||
try!(Bsonc::from_document(&selector)).inner()
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Queue removal of a single document into a bulk operation.
|
||||
/// The removal is not performed until `execute` is called.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_bulk_operation_remove_one.html
|
||||
pub fn remove_one(
|
||||
&self,
|
||||
selector: &Document
|
||||
) -> Result<()> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_bulk_operation_remove_one(
|
||||
self.inner,
|
||||
try!(Bsonc::from_document(&selector)).inner()
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Queue replacement of a single document into a bulk operation.
|
||||
/// The replacement is not performed until `execute` is called.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_bulk_operation_remove_one.html
|
||||
pub fn replace_one(
|
||||
&self,
|
||||
selector: &Document,
|
||||
document: &Document,
|
||||
upsert: bool
|
||||
) -> Result<()> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_bulk_operation_replace_one(
|
||||
self.inner,
|
||||
try!(Bsonc::from_document(&selector)).inner(),
|
||||
try!(Bsonc::from_document(&document)).inner(),
|
||||
upsert as u8
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Queue update of a single documents into a bulk operation.
|
||||
/// The update is not performed until `execute` is called.
|
||||
///
|
||||
/// TODO: document must only contain fields whose key starts
|
||||
/// with $, these is no error handling for this.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_bulk_operation_update_one.html
|
||||
pub fn update_one(
|
||||
&self,
|
||||
selector: &Document,
|
||||
document: &Document,
|
||||
upsert: bool
|
||||
) -> Result<()> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_bulk_operation_update_one(
|
||||
self.inner,
|
||||
try!(Bsonc::from_document(&selector)).inner(),
|
||||
try!(Bsonc::from_document(&document)).inner(),
|
||||
upsert as u8
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Queue update of multiple documents into a bulk operation.
|
||||
/// The update is not performed until `execute` is called.
|
||||
///
|
||||
/// TODO: document must only contain fields whose key starts
|
||||
/// with $, these is no error handling for this.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_bulk_operation_update_one.html
|
||||
pub fn update(
|
||||
&self,
|
||||
selector: &Document,
|
||||
document: &Document,
|
||||
upsert: bool
|
||||
) -> Result<()> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_bulk_operation_update(
|
||||
self.inner,
|
||||
try!(Bsonc::from_document(&selector)).inner(),
|
||||
try!(Bsonc::from_document(&document)).inner(),
|
||||
upsert as u8
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// This function executes all operations queued into this bulk operation.
|
||||
/// If ordered was set true, forward progress will be stopped upon the first error.
|
||||
///
|
||||
/// This function takes ownership because it is not possible to execute a bulk operation
|
||||
/// multiple times.
|
||||
///
|
||||
/// Returns a document with an overview of the bulk operation if successfull.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_bulk_operation_execute.html
|
||||
pub fn execute(self) -> Result<Document> {
|
||||
// Bsonc to store the reply
|
||||
let mut reply = Bsonc::new();
|
||||
// Empty error that might be filled
|
||||
let mut error = BsoncError::empty();
|
||||
|
||||
// Execute the operation. This returns a non-zero hint of the peer node on
|
||||
// success, otherwise 0 and error is set.
|
||||
let return_value = unsafe {
|
||||
bindings::mongoc_bulk_operation_execute(
|
||||
self.inner,
|
||||
reply.mut_inner(),
|
||||
error.mut_inner()
|
||||
)
|
||||
};
|
||||
|
||||
if return_value != 0 {
|
||||
match reply.as_document() {
|
||||
Ok(document) => return Ok(document),
|
||||
Err(error) => return Err(error.into())
|
||||
}
|
||||
} else {
|
||||
Err(error.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for BulkOperation<'a> {
|
||||
fn drop(&mut self) {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_bulk_operation_destroy(self.inner);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
use std::borrow::Cow;
|
||||
use std::ffi::{CStr,CString};
|
||||
use std::fmt;
|
||||
|
||||
use mongoc::bindings;
|
||||
|
||||
/// Abstraction on top of MongoDB connection URI format.
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_uri_t.html
|
||||
|
||||
pub struct Uri {
|
||||
inner: *mut bindings::mongoc_uri_t
|
||||
}
|
||||
|
||||
impl Uri {
|
||||
/// Parses a string containing a MongoDB style URI connection string.
|
||||
///
|
||||
/// Returns None if the uri is not in the correct format, there is no
|
||||
/// further information available if this is not the case.
|
||||
///
|
||||
/// See: http://api.mongodb.org/c/current/mongoc_uri_new.html
|
||||
pub fn new<T: Into<Vec<u8>>>(uri_string: T) -> Option<Uri> {
|
||||
let uri_cstring = CString::new(uri_string).unwrap();
|
||||
let uri = unsafe { bindings::mongoc_uri_new(uri_cstring.as_ptr()) };
|
||||
if uri.is_null() {
|
||||
None
|
||||
} else {
|
||||
Some(Uri { inner: uri })
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn inner(&self) -> *const bindings::mongoc_uri_t {
|
||||
assert!(!self.inner.is_null());
|
||||
self.inner
|
||||
}
|
||||
|
||||
pub fn as_str<'a>(&'a self) -> Cow<'a, str> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
let cstr = CStr::from_ptr(
|
||||
bindings::mongoc_uri_get_string(self.inner)
|
||||
);
|
||||
String::from_utf8_lossy(cstr.to_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_database<'a>(&'a self) -> Option<Cow<'a, str>> {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
let ptr = bindings::mongoc_uri_get_database(self.inner);
|
||||
if ptr.is_null() {
|
||||
None
|
||||
} else {
|
||||
let cstr = CStr::from_ptr(ptr);
|
||||
Some(String::from_utf8_lossy(cstr.to_bytes()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO add various methods that are available on uri
|
||||
}
|
||||
|
||||
impl PartialEq for Uri {
|
||||
fn eq(&self, other: &Uri) -> bool {
|
||||
self.as_str() == other.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Uri {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Uri {
|
||||
fn clone(&self) -> Uri {
|
||||
Uri::new(self.as_str().into_owned()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Uri {
|
||||
fn drop(&mut self) {
|
||||
assert!(!self.inner.is_null());
|
||||
unsafe {
|
||||
bindings::mongoc_uri_destroy(self.inner);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
use mongo_driver::write_concern::WriteConcern;
|
||||
|
||||
#[test]
|
||||
fn test_write_concern() {
|
||||
let write_concern = WriteConcern::new();
|
||||
fn test_default_write_concern() {
|
||||
let write_concern = WriteConcern::default();
|
||||
assert!(!write_concern.inner().is_null());
|
||||
}
|
||||
|
Loading…
Reference in New Issue