Skip to content

Commit

Permalink
refactor: generalize PersistenceFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Devdutt Shenoi committed Dec 8, 2023
1 parent 5f50d03 commit 89a89fc
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,30 @@ fn get_file_ids(path: &Path) -> Result<VecDeque<u64>, Error> {
Ok(file_ids)
}

/// A handle to describe a persistence file on disk
pub struct PersistenceFile<'a> {
/// Path to the persistence directory
dir: &'a Path,
file_id: u64,
/// Name of the file e.g. `backup@1`
file_name: String,
}

impl<'a> PersistenceFile<'a> {
pub fn new(dir: &'a Path, file_id: u64) -> Result<Self, Error> {
Ok(Self { dir, file_id })
pub fn new(dir: &'a Path, file_name: String) -> Result<Self, Error> {
Ok(Self { dir, file_name })
}

/// Path of persistence file when stored on disk
pub fn path(&self) -> PathBuf {
let file_name = format!("backup@{}", self.file_id);
self.dir.join(file_name)
self.dir.join(&self.file_name)
}

// Moves the corrupt persistence file into special directory
fn handle_corrupt_file(&self) -> Result<(), Error> {
let path_src = self.path();
let dest_dir = self.dir.join("corrupted");
fs::create_dir_all(&dest_dir)?;

let file_name = path_src.file_name().unwrap();
let path_dest = dest_dir.join(file_name);
let path_dest = dest_dir.join(&self.file_name);

warn!("Moving corrupted file from {path_src:?} to {path_dest:?}");
fs::rename(path_src, path_dest)?;
Expand Down Expand Up @@ -329,7 +329,8 @@ impl Persistence {

/// Removes a persistence file with provided id
fn remove(&mut self, id: u64) -> Result<PathBuf, Error> {
let mut file = PersistenceFile::new(&self.path, id)?;
let file_name = format!("backup@{}", id);
let mut file = PersistenceFile::new(&self.path, file_name)?;
let path = file.path();

self.bytes_occupied -= file.delete()? as usize;
Expand Down Expand Up @@ -368,14 +369,16 @@ impl Persistence {
None
};

Ok(NextFile { file: PersistenceFile::new(&self.path, next_file_id)?, deleted })
let file_name = format!("backup@{}", next_file_id);
Ok(NextFile { file: PersistenceFile::new(&self.path, file_name)?, deleted })
}

/// Load the next persistence file to be read into memory
fn load_next_read_file(&mut self, current_read_file: &mut BytesMut) -> Result<(), Error> {
// Len always > 0 because of above if. Doesn't panic
let id = self.backlog_files.pop_front().unwrap();
let mut file = PersistenceFile::new(&self.path, id)?;
let file_name = format!("backup@{}", id);
let mut file = PersistenceFile::new(&self.path, file_name)?;

// Load file into memory and store its id for deleting in the future
file.read(current_read_file)?;
Expand Down

0 comments on commit 89a89fc

Please sign in to comment.