Skip to content

Commit

Permalink
copy files that are not locked by segments
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPleshkov committed Jan 15, 2024
1 parent 0dd89b7 commit 3148553
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
39 changes: 34 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crossbeam_channel::{Receiver, Sender};
use fs4::FileExt;
use log::{debug, info, trace, warn};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::fs::{self, File};
use std::io::{Error, ErrorKind, Result};
Expand Down Expand Up @@ -472,14 +473,42 @@ impl Wal {
self.truncate(self.first_index())
}

/// copy all files to the given path directory
pub fn save_to_path<P>(&self, path: P) -> Result<()>
/// Copy all files to the given path directory. directory should exist and be empty
pub fn copy_to_path<P>(&self, path: P) -> Result<()>
where
P: AsRef<Path>,
{
self.open_segment.segment.save_to_path(&path)?;
for segment in &self.closed_segments {
segment.segment.save_to_path(&path)?;
if fs::read_dir(path.as_ref())?.next().is_some() {
return Err(Error::new(
ErrorKind::AlreadyExists,
format!("path {:?} not empty", path.as_ref()),
));
};

let open_segment_file = self.open_segment.segment.path().file_name().unwrap();
let close_segment_files: HashMap<_, _> = self
.closed_segments
.iter()
.map(|segment| (segment.segment.path().file_name().unwrap(), &segment.segment))
.collect();

for entry in fs::read_dir(self.path())? {
let entry = entry?;
if !entry.metadata()?.is_file() {
continue;
}

// if file is locked by any Segment, call copy_to_path on it
let entry_file_name = entry.file_name();
let dst_path = path.as_ref().to_owned().join(entry_file_name.clone());
if entry_file_name == open_segment_file {
self.open_segment.segment.copy_to_path(&dst_path)?;
} else if let Some(segment) = close_segment_files.get(entry_file_name.as_os_str()) {
segment.copy_to_path(&dst_path)?;
} else {
// if file is not locked by any Segment, just copy it
fs::copy(&entry.path(), &dst_path)?;
}
}
Ok(())
}
Expand Down
6 changes: 2 additions & 4 deletions src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,11 @@ impl Segment {
}
}

pub fn save_to_path<P>(&self, path: P) -> Result<()>
pub(crate) fn copy_to_path<P>(&self, path: P) -> Result<()>
where
P: AsRef<Path>,
{
let file_name = self.path.file_name().unwrap();
let dst_path = path.as_ref().to_owned().join(file_name);
let mut other = Self::create(dst_path, self.capacity())?;
let mut other = Self::create(path, self.capacity())?;
unsafe {
other
.mmap
Expand Down

0 comments on commit 3148553

Please sign in to comment.