diff --git a/.gitignore b/.gitignore index f806ce4..4a95729 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ target Cargo.lock mongoc-sys/mongo-c-driver* ssl_env_vars + .idea/ .DS_Store \ No newline at end of file diff --git a/src/cursor.rs b/src/cursor.rs index 30b7955..5f716d3 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -257,6 +257,15 @@ impl<'a> Iterator for TailingCursor<'a> { type DocArray = VecDeque; 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> { 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> { 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; fn next(&mut self) -> Option { - // (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 } - - -} \ No newline at end of file +} diff --git a/src/database.rs b/src/database.rs index a681b8c..19c0f9f 100644 --- a/src/database.rs +++ b/src/database.rs @@ -26,7 +26,6 @@ pub enum CreatedBy<'a> { OwnedClient(Client<'a>) } -#[doc(hidden)] fn get_coll_name_from_doc(doc: &Document) -> Result { 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>>( &self, name: S diff --git a/src/lib.rs b/src/lib.rs index 3343ab3..12c233a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,6 @@ extern crate log; extern crate serde_derive; extern crate serde; - use std::ffi::CStr; use std::ptr; use std::result; diff --git a/tests/database.rs b/tests/database.rs index 8769a64..492a462 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -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()); +} \ No newline at end of file