-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(dev): gridfs storage default to handle large taxa model fields
- Loading branch information
Showing
8 changed files
with
196 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::collections::HashMap; | ||
|
||
use cerebro_pipe::taxa::taxon::Taxon; | ||
use futures::io::{AsyncWriteExt, AsyncReadExt}; | ||
|
||
use mongodb::gridfs::GridFsBucket; | ||
|
||
|
||
/// Uploads the taxa HashMap to GridFS as a JSON-encoded file. | ||
/// | ||
/// # Arguments | ||
/// * `bucket` - A GridFsBucket instance to perform the upload. | ||
/// * `taxa` - A reference to the taxa HashMap to be stored. | ||
/// * `filename` - A string slice for the filename to be stored in GridFS. | ||
/// | ||
/// # Returns | ||
/// * On success, returns the GridFS file’s ObjectId. | ||
/// | ||
/// **Note:** In v3 you open an upload stream, write the data to it, and then close the stream | ||
/// to finalize the upload. | ||
pub async fn upload_taxa_to_gridfs( | ||
bucket: GridFsBucket, | ||
taxa: &HashMap<String, Taxon>, | ||
filename: &str, | ||
) -> Result<String, Box<dyn std::error::Error>> { | ||
|
||
// Serialize taxa to JSON bytes. | ||
let taxa_bytes = serde_json::to_vec(taxa)?; | ||
|
||
// Open an upload stream for the given filename. | ||
let mut upload_stream = bucket.open_upload_stream(filename).await?; | ||
|
||
// Write all the bytes to the upload stream. | ||
upload_stream.write_all(&taxa_bytes).await?; | ||
|
||
// Close the stream to finalize the upload | ||
upload_stream.close().await?; | ||
|
||
Ok(filename.to_string()) | ||
} | ||
|
||
/// Downloads the taxa HashMap from GridFS using the provided ObjectId. | ||
/// | ||
/// # Arguments | ||
/// * `bucket` - A GridFsBucket instance to perform the download. | ||
/// * `taxa_id` - The ObjectId referencing the stored taxa data in GridFS. | ||
/// | ||
/// # Returns | ||
/// * On success, returns the taxa HashMap. | ||
/// | ||
/// **Note:** open_download_stream returns a GridFsDownloadStream which implements Stream over | ||
/// chunks of data. | ||
pub async fn download_taxa_from_gridfs( | ||
bucket: GridFsBucket, | ||
filename: &str, | ||
) -> Result<HashMap<String, Taxon>, Box<dyn std::error::Error>> { | ||
// Open the download stream using the taxa_id. | ||
let mut download_stream = bucket.open_download_stream_by_name(filename).await?; | ||
|
||
let mut bytes: Vec<u8> = Vec::new(); | ||
let _ = download_stream.read_to_end(&mut bytes).await?; | ||
|
||
// Deserialize the bytes back into a HashMap. | ||
let taxa: HashMap<String, Taxon> = serde_json::from_slice(&bytes)?; | ||
|
||
Ok(taxa) | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//! Cerebro module for all sample data related models and route handlers | ||
pub mod mongo; | ||
pub mod handler; | ||
pub mod handler; | ||
pub mod gridfs; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters