Add Client get_server_status

pull/3/head
Thijs Cadier 9 years ago
parent 9d299055e7
commit 82852b98ce

@ -1,11 +1,18 @@
use std::fmt;
use std::ffi::CString;
use std::ptr;
use mongo_c_driver_wrapper::bindings;
use bson::Document;
use super::Result;
use super::BsoncError;
use super::bsonc::Bsonc;
use super::collection::{Collection,CreatedBy};
use super::database::Database;
use super::uri::Uri;
use super::read_prefs::ReadPrefs;
// TODO: We're using a sort of poor man's Arc here
// with this root bool, there must be a better way.
@ -116,6 +123,39 @@ impl<'a> Client<'a> {
};
Database::new(self, coll)
}
/// Queries the server for the current server status.
///
/// See: http://api.mongodb.org/c/current/mongoc_client_get_server_status.html
pub fn get_server_status(&self, read_prefs: Option<ReadPrefs>) -> Result<Document> {
assert!(!self.inner.is_null());
// Bsonc to store the reply
let mut reply = Bsonc::new();
// Empty error that might be filled
let mut error = BsoncError::empty();
let success = unsafe {
bindings::mongoc_client_get_server_status(
self.inner,
match read_prefs {
Some(ref prefs) => prefs.mut_inner(),
None => ptr::null_mut()
},
reply.mut_inner(),
error.mut_inner()
)
};
if success == 1 {
match reply.as_document() {
Ok(document) => return Ok(document),
Err(error) => return Err(error.into())
}
} else {
Err(error.into())
}
}
}
impl<'a> Drop for Client<'a> {
@ -169,4 +209,16 @@ mod tests {
guard1.join().unwrap();
guard2.join().unwrap();
}
#[test]
fn test_get_server_status() {
let uri = Uri::new("mongodb://localhost:27017/");
let pool = ClientPool::new(uri);
let client = pool.pop();
let status = client.get_server_status(None).unwrap();
assert!(status.contains_key("host"));
assert!(status.contains_key("version"));
}
}

@ -38,6 +38,11 @@ impl ReadPrefs {
assert!(!self.inner.is_null());
self.inner
}
pub fn mut_inner(&self) -> *mut bindings::mongoc_read_prefs_t {
assert!(!self.inner.is_null());
self.inner as *mut bindings::mongoc_read_prefs_t
}
}
impl Drop for ReadPrefs {

Loading…
Cancel
Save