Open an existing segment with an explicit drop policy.
(path: &Path, policy: VectorSegmentDropPolicy)
| 79 | |
| 80 | /// Open an existing segment with an explicit drop policy. |
| 81 | pub fn open_with_policy(path: &Path, policy: VectorSegmentDropPolicy) -> std::io::Result<Self> { |
| 82 | let fd = std::fs::OpenOptions::new().read(true).open(path)?; |
| 83 | let file_size = fd.metadata()?.len() as usize; |
| 84 | |
| 85 | let min_size = HEADER_SIZE + FOOTER_SIZE; |
| 86 | if file_size < min_size { |
| 87 | return Err(std::io::Error::new( |
| 88 | std::io::ErrorKind::InvalidData, |
| 89 | format!("segment file too small: {file_size} < {min_size} bytes"), |
| 90 | )); |
| 91 | } |
| 92 | |
| 93 | let base = unsafe { |
| 94 | libc::mmap( |
| 95 | std::ptr::null_mut(), |
| 96 | file_size, |
| 97 | libc::PROT_READ, |
| 98 | libc::MAP_PRIVATE, |
| 99 | fd.as_raw_fd(), |
| 100 | 0, |
| 101 | ) |
| 102 | }; |
| 103 | if base == libc::MAP_FAILED { |
| 104 | return Err(std::io::Error::last_os_error()); |
| 105 | } |
| 106 | let base = base as *const u8; |
| 107 | |
| 108 | Self::validate_and_build(fd, base, file_size, path, policy, None).inspect_err(|_e| { |
| 109 | unsafe { libc::munmap(base as *mut libc::c_void, file_size) }; |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | /// Open an existing segment with a memory governor. |
| 114 | /// |