* Improve documentation

* add has_collection test
* fix white spaces
pull/39/head
Michael Jansen 6 years ago
parent f6a4cefb64
commit 77cbcf340d

1
.gitignore vendored

@ -2,5 +2,6 @@ target
Cargo.lock
mongoc-sys/mongo-c-driver*
ssl_env_vars
.idea/
.DS_Store

@ -257,6 +257,15 @@ impl<'a> Iterator for TailingCursor<'a> {
type DocArray = VecDeque<Document>;
type CursorId = i64;
/// BatchCursor let's you iterate though batches of results
/// in a natural way without having to deal parsing each block
/// of 100 results from mongo.
///
/// Specifically, this cursor hides the complexity of having to call
/// https://docs.mongodb.com/manual/reference/command/getMore/. This
/// allows you to have much cleaner user code. Only commands which
/// return batches work with this cursor. For example, find, aggregate,
/// and listIndexes all return batches.
pub struct BatchCursor<'a> {
cursor: Cursor<'a>,
db: &'a Database<'a>,
@ -281,6 +290,8 @@ impl<'a> BatchCursor<'a> {
}
}
// internal function to reach the next batch of results from the mongo cursor
// and store them in the DocArray buffer
fn get_cursor_next(&mut self) -> Option<Result<Document>> {
let item_opt = self.cursor.next();
if let Some(item_res) = item_opt {
@ -299,6 +310,8 @@ impl<'a> BatchCursor<'a> {
None
}
// internal function for pulling the next document from the documents buffer.
// this is the in memory representation of the documents we receive from each batch (DocArray)
fn get_next_doc(&mut self) -> Option<Result<Document>> {
if let Some(ref mut docs) = self.documents {
if docs.len() > 0 {
@ -344,7 +357,6 @@ impl<'a> Iterator for BatchCursor<'a> {
type Item = Result<Document>;
fn next(&mut self) -> Option<Self::Item> {
// (1) try the local document buffer
let res = self.get_next_doc();
if res.is_some() {return res;}
@ -368,6 +380,4 @@ impl<'a> Iterator for BatchCursor<'a> {
}
None
}
}
}

@ -26,7 +26,6 @@ pub enum CreatedBy<'a> {
OwnedClient(Client<'a>)
}
#[doc(hidden)]
fn get_coll_name_from_doc(doc: &Document) -> Result<String> {
const VALID_COMMANDS: &'static [&'static str] = &["find", "aggregate", "listIndexes"];
for s in VALID_COMMANDS {
@ -60,8 +59,8 @@ impl<'a> Database<'a> {
/// Execute a command on the database.
/// This is performed lazily and therefore requires calling `next` on the resulting cursor.
/// Results are returned in batches as per the mongoc driver.
/// To get the next batch: https://docs.mongodb.com/manual/reference/command/getMore/
/// if your are using a command like find or aggregate `command_batch` is likely
/// more convenient for you.
pub fn command(
&'a self,
command: Document,
@ -103,7 +102,7 @@ impl<'a> Database<'a> {
))
}
/// Execute a command on the database.
/// Execute a command on the database and returns a `BatchCursor`
/// Automates the process of getting the next batch from getMore
/// and parses the batch so only the result documents are returned.
/// I am unsure of the best practices of when to use this or the CRUD function.
@ -221,7 +220,7 @@ impl<'a> Database<'a> {
String::from_utf8_lossy(cstr.to_bytes())
}
/// Create a new collection in this database.
/// This function checks to see if a collection exists on the MongoDB server within database.
pub fn has_collection<S: Into<Vec<u8>>>(
&self,
name: S

@ -35,7 +35,6 @@ extern crate log;
extern crate serde_derive;
extern crate serde;
use std::ffi::CStr;
use std::ptr;
use std::result;

@ -54,3 +54,20 @@ fn test_create_collection() {
assert_eq!("created_collection", collection.get_name().to_mut());
}
#[test]
fn test_has_collection() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap();
let pool = ClientPool::new(uri, None);
let client = pool.pop();
let database = client.get_database("rust_test");
database.get_collection("created_collection").drop().unwrap_or(());
let collection = database.create_collection(
"created_collection",
None
).unwrap();
assert_eq!("created_collection", collection.get_name().to_mut());
assert!(database.has_collection("created_collection").unwrap());
}
Loading…
Cancel
Save