MCPcopy Create free account
hub / github.com/ScriptedAlchemy/tracedecay / write_entry_inner

Function write_entry_inner

src/monitor.rs:108–164  ·  view source on GitHub ↗
(
    mmap_path: &Path,
    prefix: &str,
    project: &str,
    tool_name: &str,
    delta: u64,
    before: u64,
)

Source from the content-addressed store, hash-verified

106}
107
108fn write_entry_inner(
109 mmap_path: &Path,
110 prefix: &str,
111 project: &str,
112 tool_name: &str,
113 delta: u64,
114 before: u64,
115) -> std::io::Result<()> {
116 let file = std::fs::OpenOptions::new()
117 .read(true)
118 .write(true)
119 .create(true)
120 .truncate(false)
121 .open(mmap_path)?;
122
123 // Exclusive lock for concurrent writer safety.
124 file.lock_exclusive()?;
125
126 let len = file.metadata()?.len() as usize;
127 if len < FILE_SIZE {
128 file.set_len(FILE_SIZE as u64)?;
129 }
130
131 let mut mmap = unsafe { memmap2::MmapMut::map_mut(&file)? };
132
133 // Read current write_idx.
134 let write_idx = u64::from_le_bytes(
135 mmap[OFF_WRITE_IDX..OFF_WRITE_IDX + 8]
136 .try_into()
137 .unwrap_or([0; 8]),
138 );
139 let slot = (write_idx as usize) % RING_CAPACITY;
140 let off = HEADER_SIZE + slot * ENTRY_SIZE;
141
142 // Write string fields.
143 write_str(&mut mmap, off + EOFF_PREFIX, prefix);
144 write_str(&mut mmap, off + EOFF_PROJECT, project);
145 write_str(&mut mmap, off + EOFF_TOOL, tool_name);
146
147 // Write numeric fields.
148 mmap[off + EOFF_DELTA..off + EOFF_DELTA + 8].copy_from_slice(&delta.to_le_bytes());
149 mmap[off + EOFF_BEFORE..off + EOFF_BEFORE + 8].copy_from_slice(&before.to_le_bytes());
150
151 let timestamp = std::time::SystemTime::now()
152 .duration_since(std::time::UNIX_EPOCH)
153 .unwrap_or_default()
154 .as_secs();
155 mmap[off + EOFF_TIMESTAMP..off + EOFF_TIMESTAMP + 8].copy_from_slice(&timestamp.to_le_bytes());
156
157 // Increment write_idx (reader sees this last).
158 let new_idx = write_idx + 1;
159 mmap[OFF_WRITE_IDX..OFF_WRITE_IDX + 8].copy_from_slice(&new_idx.to_le_bytes());
160
161 mmap.flush()?;
162 file.unlock()?;
163 Ok(())
164}
165

Callers 2

write_entryFunction · 0.85
write_entry_toFunction · 0.85

Calls 6

write_strFunction · 0.85
metadataMethod · 0.80
openMethod · 0.45
writeMethod · 0.45
readMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected