batch.rs (3884B)
1 //! Bounded opaque writes; callers retain policy and serialization ownership. 2 use super::{Error, ProjectionDocument, ProjectionGeneration, ProjectionId}; 3 use crate::event::EventSourceToken; 4 use std::collections::BTreeSet; 5 6 pub const PROJECTION_BATCH_ROWS_MAX: usize = 64; 7 pub const PROJECTION_BATCH_BYTES_MAX: usize = 16_777_216; 8 9 /// A content-derived generation binds caller policy/schema and the canonical source. 10 #[derive(Clone, Debug, Eq, PartialEq)] 11 pub struct ProjectionWriteScope { 12 projection_id: ProjectionId, 13 generation: ProjectionGeneration, 14 source: EventSourceToken, 15 expected_active: Option<ProjectionGeneration>, 16 } 17 impl ProjectionWriteScope { 18 pub fn new( 19 projection_id: ProjectionId, 20 generation: ProjectionGeneration, 21 source: EventSourceToken, 22 expected_active: Option<ProjectionGeneration>, 23 ) -> Result<Self, Error> { 24 ProjectionId::parse(projection_id.as_str())?; 25 ProjectionGeneration::new(*generation.as_bytes())?; 26 crate::event::SourceGeneration::new(*source.generation().as_bytes())?; 27 if let Some(active) = expected_active { 28 ProjectionGeneration::new(*active.as_bytes())?; 29 if active == generation { 30 return Err(Error::InvalidProjectionGeneration); 31 } 32 } 33 Ok(Self { 34 projection_id, 35 generation, 36 source, 37 expected_active, 38 }) 39 } 40 pub const fn projection_id(&self) -> &ProjectionId { 41 &self.projection_id 42 } 43 pub const fn generation(&self) -> ProjectionGeneration { 44 self.generation 45 } 46 pub const fn source(&self) -> EventSourceToken { 47 self.source 48 } 49 pub const fn expected_active(&self) -> Option<ProjectionGeneration> { 50 self.expected_active 51 } 52 } 53 54 #[derive(Clone, Eq, PartialEq)] 55 pub struct ProjectionDocumentBatch { 56 scope: ProjectionWriteScope, 57 documents: Vec<ProjectionDocument>, 58 logical_bytes: usize, 59 } 60 impl ProjectionDocumentBatch { 61 /// Empty batches allow publishing a genuinely empty or already staged generation. 62 pub fn new( 63 scope: ProjectionWriteScope, 64 documents: Vec<ProjectionDocument>, 65 ) -> Result<Self, Error> { 66 if documents.len() > PROJECTION_BATCH_ROWS_MAX { 67 return Err(Error::InvalidProjectionDocument); 68 } 69 let mut keys = BTreeSet::new(); 70 let mut charge = 4096usize; 71 for document in &documents { 72 if !keys.insert(document.key()) { 73 return Err(Error::InvalidProjectionDocument); 74 } 75 charge = charge 76 .checked_add( 77 document 78 .value() 79 .len() 80 .checked_mul(2) 81 .and_then(|size| size.checked_add(document.key().len() * 2 + 256)) 82 .ok_or(Error::SpaceInsufficient)?, 83 ) 84 .ok_or(Error::SpaceInsufficient)?; 85 if charge > PROJECTION_BATCH_BYTES_MAX { 86 return Err(Error::SpaceInsufficient); 87 } 88 } 89 Ok(Self { 90 scope, 91 documents, 92 logical_bytes: charge, 93 }) 94 } 95 pub const fn scope(&self) -> &ProjectionWriteScope { 96 &self.scope 97 } 98 pub fn documents(&self) -> &[ProjectionDocument] { 99 &self.documents 100 } 101 pub const fn logical_bytes(&self) -> usize { 102 self.logical_bytes 103 } 104 } 105 impl std::fmt::Debug for ProjectionDocumentBatch { 106 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 107 formatter 108 .debug_struct("ProjectionDocumentBatch") 109 .field("scope", &self.scope) 110 .field("rows", &self.documents.len()) 111 .field("logical_bytes", &self.logical_bytes) 112 .finish() 113 } 114 }