cache.rs (3303B)
1 //! Explicit derived-cache budgets and exact, bounded retirement requests. 2 use super::{Error, ProjectionGeneration, ProjectionId}; 3 use crate::event::EventSourceToken; 4 5 pub const PROJECTION_CACHE_COLLECTION_ROWS_MAX: usize = 64; 6 7 /// Caller-owned positive limits. Configuring a cache never silently raises them. 8 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 9 pub struct ProjectionCacheLimits { 10 value_bytes: u64, 11 protected_generations: u64, 12 legacy_snapshots: u64, 13 legacy_age_ms: u64, 14 } 15 impl ProjectionCacheLimits { 16 pub fn new( 17 value_bytes: u64, 18 protected_generations: u64, 19 legacy_snapshots: u64, 20 legacy_age_ms: u64, 21 ) -> Result<Self, Error> { 22 if [ 23 value_bytes, 24 protected_generations, 25 legacy_snapshots, 26 legacy_age_ms, 27 ] 28 .iter() 29 .any(|value| *value == 0 || *value > i64::MAX as u64) 30 { 31 return Err(Error::InvalidProjectionCacheLimits); 32 } 33 Ok(Self { 34 value_bytes, 35 protected_generations, 36 legacy_snapshots, 37 legacy_age_ms, 38 }) 39 } 40 pub const fn value_bytes(self) -> u64 { 41 self.value_bytes 42 } 43 pub const fn protected_generations(self) -> u64 { 44 self.protected_generations 45 } 46 pub const fn legacy_snapshots(self) -> u64 { 47 self.legacy_snapshots 48 } 49 pub const fn legacy_age_ms(self) -> u64 { 50 self.legacy_age_ms 51 } 52 } 53 54 /// An exact cache owner/generation and the source/active identity observed by its caller. 55 #[derive(Clone, Debug, Eq, PartialEq)] 56 pub struct ProjectionCacheScope { 57 id: ProjectionId, 58 generation: ProjectionGeneration, 59 source: EventSourceToken, 60 active: Option<ProjectionGeneration>, 61 } 62 impl ProjectionCacheScope { 63 pub fn new( 64 id: ProjectionId, 65 generation: ProjectionGeneration, 66 source: EventSourceToken, 67 active: Option<ProjectionGeneration>, 68 ) -> Result<Self, Error> { 69 ProjectionId::parse(id.as_str())?; 70 ProjectionGeneration::new(*generation.as_bytes())?; 71 crate::event::SourceGeneration::new(*source.generation().as_bytes())?; 72 if let Some(value) = active { 73 ProjectionGeneration::new(*value.as_bytes())?; 74 } 75 Ok(Self { 76 id, 77 generation, 78 source, 79 active, 80 }) 81 } 82 pub const fn projection_id(&self) -> &ProjectionId { 83 &self.id 84 } 85 pub const fn generation(&self) -> ProjectionGeneration { 86 self.generation 87 } 88 pub const fn source(&self) -> EventSourceToken { 89 self.source 90 } 91 pub const fn expected_active(&self) -> Option<ProjectionGeneration> { 92 self.active 93 } 94 } 95 96 /// Cached logical usage; physical database and WAL allocation are separate. 97 #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] 98 pub struct ProjectionCacheUsage { 99 pub value_bytes: u64, 100 pub protected_generations: u64, 101 pub legacy_snapshots: u64, 102 } 103 104 /// Actual collection progress. A pass removes at most64 total derived rows. 105 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 106 pub struct ProjectionCacheCollection { 107 pub removed_rows: u64, 108 pub removed_value_bytes: u64, 109 pub more: bool, 110 pub usage: ProjectionCacheUsage, 111 }