MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / open_with_policy

Method open_with_policy

nodedb-vector/src/mmap_segment/reader.rs:81–111  ·  view source on GitHub ↗

Open an existing segment with an explicit drop policy.

(path: &Path, policy: VectorSegmentDropPolicy)

Source from the content-addressed store, hash-verified

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 ///

Callers

nothing calls this directly

Calls 4

openMethod · 0.45
readMethod · 0.45
lenMethod · 0.45
as_raw_fdMethod · 0.45

Tested by

no test coverage detected