Skip to content

Commit

Permalink
Error handling for existing collection
Browse files Browse the repository at this point in the history
  • Loading branch information
brianreicher committed Oct 10, 2023
1 parent 8b66ef7 commit 2823103
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion ingestion/src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::io::copy;
use std::path::Path;
use zip::write::FileOptions;
use zip::ZipWriter;
use mongo_utils::MongoDriver;


pub struct GitHubDownloader {
client: reqwest::Client,
Expand Down Expand Up @@ -36,7 +38,9 @@ impl GitHubDownloader {
}

let mut file_buf = Vec::new();
response.copy_to(&mut file_buf)?;

// response.copy_to(&mut file_buf)?;
response.read_to_end(&mut file_buf)?;

zip.start_file(file_name, options)?;
zip.write_all(&file_buf)?;
Expand Down
2 changes: 2 additions & 0 deletions ingestion/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ async fn main() -> std::io::Result<()> {
let mongo: MongoDriver = MongoDriver::new("localhost", 8080, "turbine");
mongo.connect();

// create/connect if exists, and flush
mongo.create_collection("github_data");
mongo.flush_collection("github_data");

Ok(())
Expand Down
25 changes: 20 additions & 5 deletions ingestion/src/mongo_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,29 @@ impl MongoDriver {
}


pub async fn create_collection(&mut self, collection_name: &str) -> Result<(), mongodb::error::Error> {
ub async fn create_collection(&mut self, collection_name: &str) -> Result<(), mongodb::error::Error> {
if self.client.is_none() {
self.connect().await?;
}

self.client.as_ref().unwrap().database(&self.db_name).create_collection(collection_name, None).await?;
println!("Created collection {} in {}", collection_name, self.db_name);
Ok(())

match self.client.as_ref().unwrap().database(&self.db_name).create_collection(collection_name, None).await {
Ok(_) => {
println!("Created collection {} in {}", collection_name, self.db_name);
Ok(())
}
Error(err) => {
if let Some(code) = err.code {
if code == 48 {
println!("Collection {} already exists in {}", collection_name, self.db_name);
Ok(())
} else {
Error(err)
}
} else {
Error(err)
}
}
}
}

pub async fn collection_size(&self, collection_name: &str) -> Result<u64, mongodb::error::Error> {
Expand Down

0 comments on commit 2823103

Please sign in to comment.