diff --git a/accounts-db/benches/bench_accounts_file.rs b/accounts-db/benches/bench_accounts_file.rs index 808f1a630..3a05b0139 100644 --- a/accounts-db/benches/bench_accounts_file.rs +++ b/accounts-db/benches/bench_accounts_file.rs @@ -58,7 +58,7 @@ fn bench_write_accounts_file(c: &mut Criterion) { || { let path = temp_dir.path().join(format!("append_vec_{accounts_count}")); let file_size = accounts.len() * (space + append_vec::STORE_META_OVERHEAD); - AppendVec::new(&path, true, file_size) + AppendVec::new(path, true, file_size) }, |append_vec| { let res = append_vec.append_accounts(&storable_accounts, 0).unwrap(); diff --git a/accounts-db/src/accounts_db.rs b/accounts-db/src/accounts_db.rs index d172fd4c4..7f29edec1 100644 --- a/accounts-db/src/accounts_db.rs +++ b/accounts-db/src/accounts_db.rs @@ -1034,7 +1034,7 @@ impl AccountStorageEntry { pub fn new(path: &Path, slot: Slot, id: AccountsFileId, file_size: u64) -> Self { let tail = AccountsFile::file_name(slot, id); let path = Path::new(path).join(tail); - let accounts = AccountsFile::AppendVec(AppendVec::new(&path, true, file_size as usize)); + let accounts = AccountsFile::AppendVec(AppendVec::new(path, true, file_size as usize)); Self { id, diff --git a/accounts-db/src/accounts_file.rs b/accounts-db/src/accounts_file.rs index 6371be608..54bf8b76c 100644 --- a/accounts-db/src/accounts_file.rs +++ b/accounts-db/src/accounts_file.rs @@ -67,7 +67,7 @@ impl AccountsFile { /// The second element of the returned tuple is the number of accounts in the /// accounts file. pub fn new_from_file(path: impl AsRef, current_len: usize) -> Result<(Self, usize)> { - let (av, num_accounts) = AppendVec::new_from_file(path, current_len)?; + let (av, num_accounts) = AppendVec::new_from_file(path.as_ref(), current_len)?; Ok((Self::AppendVec(av), num_accounts)) } diff --git a/accounts-db/src/append_vec.rs b/accounts-db/src/append_vec.rs index bf91ca0d1..b2499cb2c 100644 --- a/accounts-db/src/append_vec.rs +++ b/accounts-db/src/append_vec.rs @@ -29,7 +29,7 @@ use { fs::{remove_file, OpenOptions}, io::{Seek, SeekFrom, Write}, mem, - path::{Path, PathBuf}, + path::PathBuf, sync::{ atomic::{AtomicU64, AtomicUsize, Ordering}, Mutex, @@ -237,19 +237,20 @@ impl Drop for AppendVec { } impl AppendVec { - pub fn new(file: &Path, create: bool, size: usize) -> Self { + pub fn new(file: impl Into, create: bool, size: usize) -> Self { + let file = file.into(); let initial_len = 0; AppendVec::sanitize_len_and_size(initial_len, size).unwrap(); if create { - let _ignored = remove_file(file); + let _ignored = remove_file(&file); } let mut data = OpenOptions::new() .read(true) .write(true) .create(create) - .open(file) + .open(&file) .map_err(|e| { panic!( "Unable to {} data file {} in current dir({:?}): {:?}", @@ -282,7 +283,7 @@ impl AppendVec { APPEND_VEC_MMAPPED_FILES_OPEN.fetch_add(1, Ordering::Relaxed); AppendVec { - path: file.to_path_buf(), + path: file, map, // This mutex forces append to be single threaded, but concurrent with reads // See UNSAFE usage in `append_ptr` @@ -347,15 +348,16 @@ impl AppendVec { format!("{slot}.{id}") } - pub fn new_from_file>(path: P, current_len: usize) -> Result<(Self, usize)> { - let new = Self::new_from_file_unchecked(&path, current_len)?; + pub fn new_from_file(path: impl Into, current_len: usize) -> Result<(Self, usize)> { + let path = path.into(); + let new = Self::new_from_file_unchecked(path, current_len)?; let (sanitized, num_accounts) = new.sanitize_layout_and_length(); if !sanitized { // This info show the failing accountvec file path. It helps debugging // the appendvec data corrupution issues related to recycling. return Err(AccountsFileError::AppendVecError( - AppendVecError::IncorrectLayout(path.as_ref().to_path_buf()), + AppendVecError::IncorrectLayout(new.path.clone()), )); } @@ -363,7 +365,8 @@ impl AppendVec { } /// Creates an appendvec from file without performing sanitize checks or counting the number of accounts - pub fn new_from_file_unchecked>(path: P, current_len: usize) -> Result { + pub fn new_from_file_unchecked(path: impl Into, current_len: usize) -> Result { + let path = path.into(); let file_size = std::fs::metadata(&path)?.len(); Self::sanitize_len_and_size(current_len, file_size as usize)?; @@ -384,7 +387,7 @@ impl AppendVec { APPEND_VEC_MMAPPED_FILES_OPEN.fetch_add(1, Ordering::Relaxed); Ok(AppendVec { - path: path.as_ref().to_path_buf(), + path, map, append_lock: Mutex::new(()), current_len: AtomicUsize::new(current_len),