Add AppendVec::new_from_file_unchecked (#26795)

new_from_file_unchecked
This commit is contained in:
apfitzge 2022-07-28 14:57:26 -05:00 committed by GitHub
parent 123f61ccad
commit c1f7d1a367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 26 deletions

View File

@ -321,32 +321,7 @@ impl AppendVec {
}
pub fn new_from_file<P: AsRef<Path>>(path: P, current_len: usize) -> io::Result<(Self, usize)> {
let data = OpenOptions::new()
.read(true)
.write(true)
.create(false)
.open(&path)?;
let file_size = std::fs::metadata(&path)?.len();
AppendVec::sanitize_len_and_size(current_len, file_size as usize)?;
let map = unsafe {
let result = MmapMut::map_mut(&data);
if result.is_err() {
// for vm.max_map_count, error is: {code: 12, kind: Other, message: "Cannot allocate memory"}
info!("memory map error: {:?}. This may be because vm.max_map_count is not set correctly.", result);
}
result?
};
let new = AppendVec {
path: path.as_ref().to_path_buf(),
map,
append_lock: Mutex::new(()),
current_len: AtomicUsize::new(current_len),
file_size,
remove_on_drop: true,
};
let new = Self::new_from_file_unchecked(path, current_len)?;
let (sanitized, num_accounts) = new.sanitize_layout_and_length();
if !sanitized {
@ -359,6 +334,39 @@ impl AppendVec {
Ok((new, num_accounts))
}
/// Creates an appendvec from file without performing sanitize checks or counting the number of accounts
pub fn new_from_file_unchecked<P: AsRef<Path>>(
path: P,
current_len: usize,
) -> io::Result<Self> {
let file_size = std::fs::metadata(&path)?.len();
Self::sanitize_len_and_size(current_len, file_size as usize)?;
let data = OpenOptions::new()
.read(true)
.write(true)
.create(false)
.open(&path)?;
let map = unsafe {
let result = MmapMut::map_mut(&data);
if result.is_err() {
// for vm.max_map_count, error is: {code: 12, kind: Other, message: "Cannot allocate memory"}
info!("memory map error: {:?}. This may be because vm.max_map_count is not set correctly.", result);
}
result?
};
Ok(AppendVec {
path: path.as_ref().to_path_buf(),
map,
append_lock: Mutex::new(()),
current_len: AtomicUsize::new(current_len),
file_size,
remove_on_drop: true,
})
}
fn sanitize_layout_and_length(&self) -> (bool, usize) {
let mut offset = 0;