Skip to content

Commit

Permalink
Merge pull request #68 from qdrant/copy-wal-files-using-memcpy
Browse files Browse the repository at this point in the history
Copy wal files using memcpy
  • Loading branch information
IvanPleshkov committed Jan 17, 2024
2 parents 03443c3 + 4cae86c commit 3f69dff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
46 changes: 46 additions & 0 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 @@ -471,6 +472,51 @@ impl Wal {
pub fn clear(&mut self) -> Result<()> {
self.truncate(self.first_index())
}

/// 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>,
{
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(())
}
}

impl fmt::Debug for Wal {
Expand Down
21 changes: 21 additions & 0 deletions src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,27 @@ impl Segment {
}
}
}

pub(crate) fn copy_to_path<P>(&self, path: P) -> Result<()>
where
P: AsRef<Path>,
{
if path.as_ref().exists() {
return Err(Error::new(
ErrorKind::AlreadyExists,
format!("Path {:?} already exists", path.as_ref()),
));
}

let mut other = Self::create(path, self.capacity())?;
unsafe {
other
.mmap
.as_mut_slice()
.copy_from_slice(self.mmap.as_slice());
}
Ok(())
}
}

impl fmt::Debug for Segment {
Expand Down

0 comments on commit 3f69dff

Please sign in to comment.