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

Method evict_from_highest_overshoot

nodedb/src/engine/sparse/doc_cache.rs:350–374  ·  view source on GitHub ↗

Evict one entry from the shard with the highest overshoot ratio. Among shards with equal overshoot ratios, `hint_db_id` (the inserting shard) is preferred so that a cold shard at its proportional share is never displaced in favour of the hot inserting shard. Returns `false` if all shards are empty.

(&mut self, hint_db_id: u64)

Source from the content-addressed store, hash-verified

348 ///
349 /// Returns `false` if all shards are empty.
350 fn evict_from_highest_overshoot(&mut self, hint_db_id: u64) -> bool {
351 let total_weight = self.total_weight();
352 let db_id = self
353 .shards
354 .iter()
355 .filter(|(_, s)| !s.entries.is_empty())
356 .max_by_key(|(id, s)| {
357 let score = s.overshoot_score(total_weight);
358 // Prefer the inserting shard on ties so equal-ratio cold shards
359 // are not evicted in its place.
360 let is_hint = u8::from(**id == hint_db_id);
361 (score, is_hint)
362 })
363 .map(|(&id, _)| id);
364 match db_id {
365 Some(id) => {
366 let removed = self.shards.get_mut(&id).expect("just found").evict_one();
367 if removed {
368 self.total = self.total.saturating_sub(1);
369 }
370 removed
371 }
372 None => false,
373 }
374 }
375}
376
377#[cfg(test)]

Callers 1

putMethod · 0.80

Calls 7

total_weightMethod · 0.80
overshoot_scoreMethod · 0.80
evict_oneMethod · 0.80
iterMethod · 0.45
is_emptyMethod · 0.45
expectMethod · 0.45
get_mutMethod · 0.45

Tested by

no test coverage detected