diff --git a/core/mvcc/database/tests.rs b/core/mvcc/database/tests.rs index 1f29160e4..54476eb04 100644 --- a/core/mvcc/database/tests.rs +++ b/core/mvcc/database/tests.rs @@ -7,6 +7,7 @@ use parking_lot::RwLock; pub(crate) struct MvccTestDbNoConn { pub(crate) db: Option>, path: Option, + log_path: Option, // Stored mainly to not drop the temp dir before the test is done. _temp_dir: Option, } @@ -37,6 +38,7 @@ impl MvccTestDbNoConn { Self { db: Some(db), path: None, + log_path: None, _temp_dir: None, } } @@ -52,9 +54,17 @@ impl MvccTestDbNoConn { println!("path: {}", path.as_os_str().to_str().unwrap()); let db = Database::open_file(io.clone(), path.as_os_str().to_str().unwrap(), true, true) .unwrap(); + let mut log_path = path.clone(); + let log_path_filename = log_path + .file_name() + .and_then(|name| name.to_str()) + .map(|s| format!("{s}-lg")) + .unwrap(); + log_path.set_file_name(log_path_filename); Self { db: Some(db), path: Some(path.to_str().unwrap().to_string()), + log_path: Some(log_path.to_str().unwrap().to_string()), _temp_dir: Some(temp_dir), } } @@ -79,6 +89,10 @@ impl MvccTestDbNoConn { pub fn get_mvcc_store(&self) -> Arc> { self.get_db().mv_store.as_ref().unwrap().clone() } + + pub fn get_log_path(&self) -> &str { + self.log_path.as_ref().unwrap().as_str() + } } pub(crate) fn generate_simple_string_row(table_id: u64, id: i64, data: &str) -> Row { diff --git a/core/mvcc/persistent_storage/logical_log.rs b/core/mvcc/persistent_storage/logical_log.rs index 4977b1338..b64cb5acc 100644 --- a/core/mvcc/persistent_storage/logical_log.rs +++ b/core/mvcc/persistent_storage/logical_log.rs @@ -470,7 +470,7 @@ pub fn load_logical_log( #[cfg(test)] mod tests { - use std::{collections::HashSet, path::PathBuf, sync::Arc}; + use std::{collections::HashSet, sync::Arc}; use rand::{thread_rng, Rng}; use rand_chacha::{ @@ -498,7 +498,7 @@ mod tests { // Load a transaction // let's not drop db as we don't want files to be removed let db = MvccTestDbNoConn::new_with_random_db(); - let (db_path, io, pager) = { + let (io, pager) = { let conn = db.connect(); let pager = conn.pager.read().clone(); let mvcc_store = db.get_mvcc_store(); @@ -509,22 +509,13 @@ mod tests { commit_tx(mvcc_store.clone(), &conn, tx_id).unwrap(); conn.close().unwrap(); let db = db.get_db(); - (db.path.clone(), db.io.clone(), pager) + (db.io.clone(), pager) }; // Now try to read it back - let db_path = PathBuf::from(db_path); - let mut log_file = db_path.clone(); - let filename = log_file - .file_name() - .and_then(|name| name.to_str()) - .map(|s| format!("{s}-lg")) - .unwrap(); - log_file.set_file_name(filename); + let log_file = db.get_log_path(); - let file = io - .open_file(log_file.to_str().unwrap(), OpenFlags::ReadOnly, false) - .unwrap(); + let file = io.open_file(log_file, OpenFlags::ReadOnly, false).unwrap(); let mvcc_store = Arc::new(MvStore::new(LocalClock::new(), Storage::new(file.clone()))); load_logical_log(&mvcc_store, file, &io, &pager).unwrap(); let tx = mvcc_store.begin_tx(pager.clone()).unwrap(); @@ -545,7 +536,7 @@ mod tests { .collect::>(); // let's not drop db as we don't want files to be removed let db = MvccTestDbNoConn::new_with_random_db(); - let (db_path, io, pager) = { + let (io, pager) = { let conn = db.connect(); let pager = conn.pager.read().clone(); let mvcc_store = db.get_mvcc_store(); @@ -560,22 +551,13 @@ mod tests { conn.close().unwrap(); let db = db.get_db(); - (db.path.clone(), db.io.clone(), pager) + (db.io.clone(), pager) }; // Now try to read it back - let db_path = PathBuf::from(db_path); - let mut log_file = db_path.clone(); - let filename = log_file - .file_name() - .and_then(|name| name.to_str()) - .map(|s| format!("{s}-lg")) - .unwrap(); - log_file.set_file_name(filename); + let log_file = db.get_log_path(); - let file = io - .open_file(log_file.to_str().unwrap(), OpenFlags::ReadOnly, false) - .unwrap(); + let file = io.open_file(log_file, OpenFlags::ReadOnly, false).unwrap(); let mvcc_store = Arc::new(MvStore::new(LocalClock::new(), Storage::new(file.clone()))); load_logical_log(&mvcc_store, file, &io, &pager).unwrap(); for (rowid, value) in &values { @@ -636,7 +618,7 @@ mod tests { } // let's not drop db as we don't want files to be removed let db = MvccTestDbNoConn::new_with_random_db(); - let (db_path, io, pager) = { + let (io, pager) = { let conn = db.connect(); let pager = conn.pager.read().clone(); let mvcc_store = db.get_mvcc_store(); @@ -660,22 +642,13 @@ mod tests { conn.close().unwrap(); let db = db.get_db(); - (db.path.clone(), db.io.clone(), pager) + (db.io.clone(), pager) }; // Now try to read it back - let db_path = PathBuf::from(db_path); - let mut log_file = db_path.clone(); - let filename = log_file - .file_name() - .and_then(|name| name.to_str()) - .map(|s| format!("{s}-lg")) - .unwrap(); - log_file.set_file_name(filename); + let log_file = db.get_log_path(); - let file = io - .open_file(log_file.to_str().unwrap(), OpenFlags::ReadOnly, false) - .unwrap(); + let file = io.open_file(log_file, OpenFlags::ReadOnly, false).unwrap(); let mvcc_store = Arc::new(MvStore::new(LocalClock::new(), Storage::new(file.clone()))); load_logical_log(&mvcc_store, file, &io, &pager).unwrap();