memory_projection_batch.rs (9181B)
1 use super::{Error, State}; 2 use crate::{ 3 event::{EventSourceToken, SourceGeneration}, 4 projection::{ 5 ProjectionCheckpoint, ProjectionDocument, ProjectionGeneration, ProjectionHealth, 6 ProjectionId, ProjectionStatus, 7 batch::{ProjectionDocumentBatch, ProjectionWriteScope}, 8 document_query::{ 9 ProjectionDocumentGenerations, ProjectionDocumentPage, ProjectionDocumentQuery, 10 ProjectionDocumentRecord, 11 }, 12 }, 13 }; 14 use std::ops::Bound::{Excluded, Included, Unbounded}; 15 16 #[derive(Clone, Copy, Eq, PartialEq)] 17 pub(super) enum Phase { 18 Staging, 19 Promoted, 20 Retired, 21 } 22 23 #[derive(Clone)] 24 pub(super) struct Generation { 25 pub(super) scope: ProjectionWriteScope, 26 pub(super) phase: Phase, 27 pub(super) rows: u64, 28 pub(super) bytes: u64, 29 } 30 31 pub(super) fn fence( 32 state: &State, 33 generation: SourceGeneration, 34 source: EventSourceToken, 35 id: &ProjectionId, 36 active: Option<ProjectionGeneration>, 37 ) -> Result<(), Error> { 38 if source != EventSourceToken::new(generation, state.event_mutation_epoch) { 39 return Err(Error::SourceEpochChanged); 40 } 41 let status = state 42 .projections 43 .iter() 44 .find(|value| value.projection_id() == id); 45 if status.map(ProjectionStatus::generation) != active 46 || status.is_some_and(|value| value.health() != ProjectionHealth::Ready) 47 { 48 return Err(Error::ProjectionRevisionConflict); 49 } 50 Ok(()) 51 } 52 53 pub(super) fn read_fence( 54 state: &State, 55 generation: SourceGeneration, 56 source: EventSourceToken, 57 id: &ProjectionId, 58 active: ProjectionGeneration, 59 ) -> Result<(), Error> { 60 fence(state, generation, source, id, Some(active))?; 61 let registered = state 62 .projection_generations 63 .get(&(id.clone(), active)) 64 .filter(|value| value.phase == Phase::Promoted) 65 .ok_or(Error::ProjectionRevisionConflict)?; 66 if registered.scope.source() != source { 67 return Err(Error::SourceEpochChanged); 68 } 69 Ok(()) 70 } 71 72 pub(super) fn write( 73 state: &mut State, 74 source_generation: SourceGeneration, 75 batch: &ProjectionDocumentBatch, 76 checkpoint: Option<ProjectionCheckpoint>, 77 ) -> Result<Option<ProjectionStatus>, Error> { 78 let scope = batch.scope(); 79 fence( 80 state, 81 source_generation, 82 scope.source(), 83 scope.projection_id(), 84 scope.expected_active(), 85 )?; 86 let identity = (scope.projection_id().clone(), scope.generation()); 87 let prior = state.projection_generations.get(&identity); 88 if prior.is_some_and(|value| value.phase != Phase::Staging || value.scope != *scope) { 89 return Err(Error::ProjectionRevisionConflict); 90 } 91 if prior.is_none() 92 && (state 93 .projection_generations 94 .iter() 95 .any(|((id, _), value)| id == scope.projection_id() && value.phase == Phase::Staging) 96 || state 97 .projection_documents 98 .range(( 99 Included((identity.0.clone(), identity.1, String::new())), 100 Unbounded, 101 )) 102 .next() 103 .is_some_and(|((id, generation, _), _)| { 104 id == &identity.0 && generation == &identity.1 105 })) 106 { 107 return Err(Error::ProjectionRevisionConflict); 108 } 109 let mut next = prior.cloned().unwrap_or(Generation { 110 scope: scope.clone(), 111 phase: Phase::Staging, 112 rows: 0, 113 bytes: 0, 114 }); 115 for document in batch.documents() { 116 let key = (identity.0.clone(), identity.1, document.key().to_owned()); 117 let previous = state.projection_documents.get(&key); 118 next.rows = next 119 .rows 120 .checked_add(u64::from(previous.is_none())) 121 .ok_or(Error::SpaceInsufficient)?; 122 next.bytes = next 123 .bytes 124 .checked_sub(previous.map_or(0, |doc| doc.value().len() as u64)) 125 .and_then(|bytes| bytes.checked_add(document.value().len() as u64)) 126 .ok_or(Error::SpaceInsufficient)?; 127 } 128 let promoted = checkpoint 129 .map(|checkpoint| { 130 if checkpoint.projection_id() != scope.projection_id() 131 || checkpoint.generation() != scope.generation() 132 || checkpoint.projected_rows() != next.rows 133 || checkpoint.source_position() != state.events.last().map(|entry| entry.position) 134 { 135 return Err(Error::ProjectionCheckpointMismatch); 136 } 137 ProjectionStatus::new( 138 identity.0.clone(), 139 identity.1, 140 ProjectionHealth::Ready, 141 Some(checkpoint), 142 None, 143 ) 144 }) 145 .transpose()?; 146 let cache_bytes = state 147 .projection_cache 148 .check_bytes(prior.map_or(0, |value| value.bytes), next.bytes)?; 149 state.projection_cache.check_protected(&identity)?; 150 // Every fallible condition is checked before publishing any in-memory changes. 151 let documents: Vec<_> = batch 152 .documents() 153 .iter() 154 .map(|document| { 155 ( 156 (identity.0.clone(), identity.1, document.key().to_owned()), 157 document.clone(), 158 ) 159 }) 160 .collect(); 161 state.projection_cache.value_bytes = cache_bytes; 162 state.projection_cache.protected.insert(identity.clone()); 163 for (key, document) in documents { 164 state.projection_documents.insert(key, document); 165 } 166 if let Some(status) = promoted.as_ref() { 167 next.phase = Phase::Promoted; 168 if let Some(active) = scope.expected_active() { 169 state 170 .projection_cache 171 .protected 172 .remove(&(identity.0.clone(), active)); 173 } 174 if let Some(active) = scope.expected_active() 175 && let Some(previous) = state 176 .projection_generations 177 .get_mut(&(identity.0.clone(), active)) 178 { 179 previous.phase = Phase::Retired; 180 state 181 .projection_cache 182 .retired 183 .entry(identity.0.clone()) 184 .or_default() 185 .insert(active); 186 } 187 if let Some(prior) = state 188 .projections 189 .iter_mut() 190 .find(|value| value.projection_id() == &identity.0) 191 { 192 *prior = status.clone(); 193 } else { 194 state.projections.push(status.clone()); 195 } 196 } 197 state.projection_generations.insert(identity, next); 198 Ok(promoted) 199 } 200 201 pub(super) fn page( 202 state: &State, 203 query: &ProjectionDocumentQuery, 204 ) -> Result<ProjectionDocumentPage, Error> { 205 let id = query.projection_id().clone(); 206 let generation = match query.generations() { 207 ProjectionDocumentGenerations::Exact(generation) => generation, 208 ProjectionDocumentGenerations::All => ProjectionGeneration::new([ 209 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 210 0, 0, 1, 211 ])?, 212 }; 213 // The smallest valid generation is [0,..,1]; zero is never admitted. 214 let start = query.after().map_or_else( 215 || { 216 Included(( 217 id.clone(), 218 generation, 219 query 220 .key_range() 221 .map_or_else(String::new, |range| range.first().to_owned()), 222 )) 223 }, 224 |(generation, key)| Excluded((id.clone(), generation, key.to_owned())), 225 ); 226 let end = query.key_range().map_or(Unbounded, |range| { 227 Included((id.clone(), generation, range.last().to_owned())) 228 }); 229 let selected: Vec<_> = state 230 .projection_documents 231 .range((start, end)) 232 .take_while(|((row_id, generation, _), _)| { 233 row_id == &id 234 && match query.generations() { 235 ProjectionDocumentGenerations::Exact(expected) => *generation == expected, 236 ProjectionDocumentGenerations::All => true, 237 } 238 }) 239 .take(usize::from(query.limit()) + 1) 240 .collect(); 241 let mut more = selected.len() > usize::from(query.limit()); 242 let mut records = Vec::new(); 243 let mut bytes = 0usize; 244 for ((_, generation, _), document) in selected.into_iter().take(usize::from(query.limit())) { 245 if bytes + document.value().len() > query.payload_budget() { 246 if records.is_empty() { 247 return Err(Error::SpaceInsufficient); 248 } 249 more = true; 250 break; 251 } 252 bytes += document.value().len(); 253 records.push(ProjectionDocumentRecord::new( 254 *generation, 255 document.clone(), 256 )?); 257 } 258 ProjectionDocumentPage::new(query, records, more) 259 } 260 261 pub(super) fn validate_key(key: &str) -> Result<(), Error> { 262 if !crate::projection::valid_document_key(key) { 263 return Err(Error::InvalidProjectionDocument); 264 } 265 Ok(()) 266 } 267 268 pub(super) type Documents = 269 std::collections::BTreeMap<(ProjectionId, ProjectionGeneration, String), ProjectionDocument>;