lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

memory_projection_cache_retired.rs (1795B)


      1 use super::{Error, State, projection_batch, projection_cache};
      2 use crate::{
      3     event::{EventSourceToken, SourceGeneration},
      4     projection::{
      5         ProjectionGeneration, ProjectionId,
      6         cache::{ProjectionCacheCollection, ProjectionCacheScope},
      7     },
      8 };
      9 
     10 fn next(
     11     state: &State,
     12     id: &ProjectionId,
     13     active: Option<ProjectionGeneration>,
     14 ) -> Option<ProjectionGeneration> {
     15     state
     16         .projection_cache
     17         .retired
     18         .get(id)
     19         .and_then(|generations| {
     20             generations
     21                 .iter()
     22                 .find(|generation| Some(**generation) != active)
     23                 .copied()
     24         })
     25 }
     26 
     27 pub(super) fn collect(
     28     state: &mut State,
     29     generation: SourceGeneration,
     30     id: ProjectionId,
     31     source: EventSourceToken,
     32     active: Option<ProjectionGeneration>,
     33     now: u64,
     34 ) -> Result<ProjectionCacheCollection, Error> {
     35     projection_cache::fence_owner(state, generation, &id, source, active)?;
     36     if !state.projection_cache.owners.contains(&id) {
     37         return Err(Error::ProjectionRevisionConflict);
     38     }
     39     let Some(retired) = next(state, &id, active) else {
     40         return Ok(ProjectionCacheCollection {
     41             removed_rows: 0,
     42             removed_value_bytes: 0,
     43             more: false,
     44             usage: state.projection_cache.usage(),
     45         });
     46     };
     47     if state
     48         .projection_generations
     49         .get(&(id.clone(), retired))
     50         .is_none_or(|value| value.phase != projection_batch::Phase::Retired)
     51     {
     52         return Err(Error::CorruptProjectionRecord);
     53     }
     54     let scope = ProjectionCacheScope::new(id.clone(), retired, source, active)?;
     55     let mut result = projection_cache::collect(state, generation, scope, now)?;
     56     result.more |= next(state, &id, active).is_some();
     57     Ok(result)
     58 }