MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / truncate

Method truncate

crates/commitlog/src/index/indexfile.rs:212–249  ·  view source on GitHub ↗

Truncates the index file starting from the entry with a key greater than or equal to the given key. If successful, `key` will no longer be in the index.

(&mut self, key: Key)

Source from the content-addressed store, hash-verified

210 ///
211 /// If successful, `key` will no longer be in the index.
212 pub(crate) fn truncate(&mut self, key: Key) -> Result<(), IndexError> {
213 let key = key.into();
214 let (found_key, index) = self
215 .find_index(Key::from(key))
216 .map(|(found, index)| (found.into(), index))
217 .or_else(|e| {
218 match e {
219 // If key is smaller than first entry, truncate all entries
220 IndexError::KeyNotFound => Ok((key, 0)),
221 _ => Err(e),
222 }
223 })?;
224
225 // If returned key is smaller than asked key, truncate from next entry
226 self.num_entries = if found_key == key {
227 index as usize
228 } else {
229 index as usize + 1
230 };
231
232 let start = self.num_entries * ENTRY_SIZE;
233 trace!(
234 "truncate key={} found={} index={} num-entries={} start={}",
235 key,
236 found_key,
237 index,
238 self.num_entries,
239 start
240 );
241
242 if start < self.inner.len() {
243 self.inner[start..].fill(0);
244 }
245
246 self.inner.flush()?;
247
248 Ok(())
249 }
250
251 /// Obtain an iterator over the entries of the index.
252 pub fn entries(&self) -> Entries<'_, Key> {

Callers 14

normalize_baseFunction · 0.80
fromMethod · 0.80
lock_innerMethod · 0.80
pid_fileMethod · 0.80
row_iter_bsatn_advanceFunction · 0.80
datastore_insert_bsatnFunction · 0.80
datastore_update_bsatnFunction · 0.80
ftruncateMethod · 0.80
test_truncateFunction · 0.80
mountFunction · 0.80

Calls 7

find_indexMethod · 0.80
fillMethod · 0.80
OkFunction · 0.50
ErrFunction · 0.50
mapMethod · 0.45
lenMethod · 0.45
flushMethod · 0.45

Tested by 3

test_truncateFunction · 0.64
mountFunction · 0.64