-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from giuseppe-g-gelardi/separate-stuff
start separate database operations -- io, util, queries, etc
- Loading branch information
Showing
11 changed files
with
276 additions
and
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,3 @@ tracing-test = "0.2.5" | |
[lib] | ||
path = "src/lib.rs" | ||
|
||
# [[bin]] | ||
# name = "cargobase" | ||
# path = "main.rs" |
File renamed without changes.
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,7 @@ | ||
pub mod columns; | ||
pub mod row; | ||
pub mod table; | ||
|
||
pub use columns::{Column, Columns}; | ||
pub use row::Row; | ||
pub use table::Table; |
File renamed without changes.
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,74 @@ | ||
use std::path::Path; | ||
|
||
use crate::Database; | ||
|
||
impl Database { | ||
pub(crate) async fn save_to_file(&self) -> Result<(), tokio::io::Error> { | ||
let json_data = serde_json::to_string_pretty(&self)?; | ||
tokio::fs::write(&self.file_name, json_data).await?; | ||
tracing::info!("Database saved to file: {:?}", self.file_name); | ||
Ok(()) | ||
} | ||
|
||
pub(crate) async fn load_from_file<P: AsRef<Path>>( | ||
file_name: P, | ||
) -> Result<Self, tokio::io::Error> { | ||
let json_data = tokio::fs::read_to_string(file_name.as_ref()).await?; | ||
let db: Database = serde_json::from_str(&json_data)?; | ||
tracing::info!( | ||
"Database loaded from file: {:?}", | ||
file_name.as_ref().display() | ||
); | ||
Ok(db) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
||
mod tests { | ||
|
||
use super::*; | ||
use std::collections::HashMap; | ||
|
||
#[tokio::test] | ||
async fn test_save_to_file() { | ||
use tempfile::NamedTempFile; | ||
|
||
let temp_file = NamedTempFile::new().expect("Failed to create a temporary file"); | ||
let db_path = temp_file.path().to_path_buf(); | ||
|
||
let db = Database { | ||
name: "test_db".to_string(), | ||
file_name: db_path.clone(), | ||
tables: HashMap::new(), | ||
}; | ||
|
||
db.save_to_file().await.expect("Failed to save database"); | ||
let loaded_db = Database::load_from_file(&db_path) | ||
.await | ||
.expect("Failed to load database"); | ||
assert_eq!(db, loaded_db); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_load_from_file() { | ||
use tempfile::NamedTempFile; | ||
|
||
let temp_file = NamedTempFile::new().expect("Failed to create a temporary file"); | ||
let db_path = temp_file.path().to_path_buf(); | ||
|
||
let db = Database { | ||
name: "test_db".to_string(), | ||
file_name: db_path.clone(), | ||
tables: HashMap::new(), | ||
}; | ||
|
||
db.save_to_file().await.expect("Failed to save database"); | ||
|
||
let loaded_db = Database::load_from_file(&db_path) | ||
.await | ||
.expect("Failed to load database"); | ||
|
||
assert_eq!(db, loaded_db); | ||
} | ||
} |
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,18 @@ | ||
pub mod core; | ||
pub mod io; | ||
pub mod query; | ||
pub mod utils; | ||
|
||
use crate::Table; | ||
|
||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] | ||
pub struct Database { | ||
pub(crate) name: String, | ||
pub(crate) file_name: PathBuf, | ||
pub(crate) tables: HashMap<String, Table>, | ||
} | ||
|
Oops, something went wrong.