lib

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

projection_batch.rs (9350B)


      1 use futures_executor::block_on;
      2 use radroots_storage::{
      3     Error, EventStore, ProjectionStore,
      4     event::{EventSourceToken, SourceGeneration},
      5     memory::MemoryStorage,
      6     projection::{
      7         ProjectionCheckpoint, ProjectionDocument, ProjectionGeneration, ProjectionId,
      8         batch::{PROJECTION_BATCH_BYTES_MAX, ProjectionDocumentBatch, ProjectionWriteScope},
      9         document_query::{ProjectionDocumentGenerations, ProjectionDocumentQuery},
     10     },
     11 };
     12 fn generation(byte: u8) -> ProjectionGeneration {
     13     ProjectionGeneration::new([byte; 32]).unwrap()
     14 }
     15 fn id() -> ProjectionId {
     16     ProjectionId::parse("fixture").unwrap()
     17 }
     18 fn document(key: &str, size: usize) -> ProjectionDocument {
     19     ProjectionDocument::new(key.into(), vec![7; size]).unwrap()
     20 }
     21 fn scope(
     22     source: EventSourceToken,
     23     active: Option<ProjectionGeneration>,
     24     target: u8,
     25 ) -> ProjectionWriteScope {
     26     ProjectionWriteScope::new(id(), generation(target), source, active).unwrap()
     27 }
     28 fn query(target: u8) -> ProjectionDocumentQuery {
     29     ProjectionDocumentQuery::new(
     30         id(),
     31         ProjectionDocumentGenerations::Exact(generation(target)),
     32         64,
     33     )
     34     .unwrap()
     35 }
     36 
     37 #[test]
     38 fn projection_batch_bounds_duplicates_redaction_and_scope_fail_closed() {
     39     let source = EventSourceToken::new(SourceGeneration::new([9; 32]).unwrap(), 0);
     40     let scope = scope(source, None, 1);
     41     let docs: Vec<_> = (0..64)
     42         .map(|i| document(&format!("key.{i:02}"), 1))
     43         .collect();
     44     let batch = ProjectionDocumentBatch::new(scope.clone(), docs).unwrap();
     45     assert_eq!(batch.documents().len(), 64);
     46     assert!(!format!("{batch:?}").contains("key.00"));
     47     assert_eq!(
     48         ProjectionDocumentBatch::new(
     49             scope.clone(),
     50             (0..65).map(|i| document(&format!("k{i}"), 1)).collect()
     51         ),
     52         Err(Error::InvalidProjectionDocument)
     53     );
     54     assert_eq!(
     55         ProjectionDocumentBatch::new(
     56             scope.clone(),
     57             vec![document("same", 1), document("same", 2)]
     58         ),
     59         Err(Error::InvalidProjectionDocument)
     60     );
     61     let value_max = (PROJECTION_BATCH_BYTES_MAX - 4096 - 256 - 2) / 2;
     62     let exact =
     63         ProjectionDocumentBatch::new(scope.clone(), vec![document("k", value_max)]).unwrap();
     64     assert_eq!(exact.logical_bytes(), PROJECTION_BATCH_BYTES_MAX);
     65     assert_eq!(
     66         ProjectionDocumentBatch::new(scope, vec![document("k", value_max + 1)]),
     67         Err(Error::SpaceInsufficient)
     68     );
     69     assert_eq!(
     70         ProjectionWriteScope::new(id(), generation(1), source, Some(generation(1))),
     71         Err(Error::InvalidProjectionGeneration)
     72     );
     73     assert!(ProjectionDocument::new("\n".into(), vec![1]).is_err());
     74     assert!(ProjectionId::parse("wrong scope").is_err());
     75 }
     76 
     77 #[test]
     78 fn projection_memory_atomic_generation_counts_fences_and_sealing_are_coherent() {
     79     let store = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
     80     let source = block_on(store.source_token()).unwrap();
     81     let first = scope(source, None, 1);
     82     block_on(
     83         store.put_projection_batch(
     84             ProjectionDocumentBatch::new(first.clone(), vec![document("a", 1), document("b", 2)])
     85                 .unwrap(),
     86         ),
     87     )
     88     .unwrap();
     89     assert_eq!(
     90         block_on(ProjectionStore::status(&store, id())).unwrap(),
     91         None
     92     );
     93     let bad = ProjectionCheckpoint::new(id(), generation(1), None, 1, 1).unwrap();
     94     assert_eq!(
     95         block_on(store.promote_projection_batch(
     96             ProjectionDocumentBatch::new(first.clone(), vec![document("c", 3)]).unwrap(),
     97             bad
     98         )),
     99         Err(Error::ProjectionCheckpointMismatch)
    100     );
    101     assert_eq!(
    102         block_on(store.query_projection_documents(query(1)))
    103             .unwrap()
    104             .records()
    105             .len(),
    106         2
    107     );
    108     let good = ProjectionCheckpoint::new(id(), generation(1), None, 3, 1).unwrap();
    109     block_on(store.promote_projection_batch(
    110         ProjectionDocumentBatch::new(first, vec![document("c", 3)]).unwrap(),
    111         good,
    112     ))
    113     .unwrap();
    114     assert_eq!(
    115         block_on(store.query_projection_documents_at(query(1), source, generation(1)))
    116             .unwrap()
    117             .records()
    118             .len(),
    119         3
    120     );
    121     assert_eq!(
    122         block_on(store.put_projection_document(id(), generation(1), document("a", 9))),
    123         Err(Error::ProjectionRevisionConflict)
    124     );
    125     let second = scope(source, Some(generation(1)), 2);
    126     block_on(store.put_projection_batch(
    127         ProjectionDocumentBatch::new(second.clone(), vec![document("x", 1)]).unwrap(),
    128     ))
    129     .unwrap();
    130     assert_eq!(
    131         block_on(store.put_projection_batch(
    132             ProjectionDocumentBatch::new(scope(source, Some(generation(1)), 3), vec![]).unwrap()
    133         )),
    134         Err(Error::ProjectionRevisionConflict)
    135     );
    136     let stale = EventSourceToken::new(source.generation(), source.epoch() + 1);
    137     assert_eq!(
    138         block_on(store.query_projection_documents_at(query(1), stale, generation(1))),
    139         Err(Error::SourceEpochChanged)
    140     );
    141     assert_eq!(
    142         block_on(store.query_projection_documents_at(query(2), source, generation(1))),
    143         Err(Error::InvalidProjectionDocument)
    144     );
    145     block_on(store.promote_projection_batch(
    146         ProjectionDocumentBatch::new(second, vec![document("y", 2)]).unwrap(),
    147         ProjectionCheckpoint::new(id(), generation(2), None, 2, 2).unwrap(),
    148     ))
    149     .unwrap();
    150     assert_eq!(
    151         block_on(store.query_projection_documents_at(query(1), source, generation(1))),
    152         Err(Error::ProjectionRevisionConflict)
    153     );
    154     assert_eq!(
    155         block_on(store.query_projection_documents_at(query(2), source, generation(2)))
    156             .unwrap()
    157             .records()
    158             .len(),
    159         2
    160     );
    161     assert_eq!(
    162         block_on(store.put_projection_batch(
    163             ProjectionDocumentBatch::new(scope(source, None, 4), vec![]).unwrap()
    164         )),
    165         Err(Error::ProjectionRevisionConflict)
    166     );
    167 }
    168 
    169 #[test]
    170 fn projection_memory_updates_empty_generations_and_checkpoint_scopes_are_atomic() {
    171     let store = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap());
    172     let source = block_on(store.source_token()).unwrap();
    173     block_on(store.put_projection_document(id(), generation(1), document("legacy", 1))).unwrap();
    174     assert_eq!(
    175         block_on(store.put_projection_batch(
    176             ProjectionDocumentBatch::new(scope(source, None, 1), vec![]).unwrap()
    177         )),
    178         Err(Error::ProjectionRevisionConflict)
    179     );
    180     assert_eq!(
    181         block_on(store.projection_document(id(), generation(1), "bad\nkey".into())),
    182         Err(Error::InvalidProjectionDocument)
    183     );
    184     let next = scope(source, None, 2);
    185     let rows: Vec<_> = (0..64).map(|i| document(&format!("k{i:02}"), 1)).collect();
    186     block_on(store.put_projection_batch(ProjectionDocumentBatch::new(next.clone(), rows).unwrap()))
    187         .unwrap();
    188     block_on(store.put_projection_batch(
    189         ProjectionDocumentBatch::new(next.clone(), vec![document("k00", 3)]).unwrap(),
    190     ))
    191     .unwrap();
    192     assert_eq!(
    193         block_on(store.projection_document(id(), generation(2), "k00".into()))
    194             .unwrap()
    195             .unwrap()
    196             .value()
    197             .len(),
    198         3
    199     );
    200     for checkpoint in [
    201         ProjectionCheckpoint::new(
    202             ProjectionId::parse("foreign").unwrap(),
    203             generation(2),
    204             None,
    205             64,
    206             1,
    207         )
    208         .unwrap(),
    209         ProjectionCheckpoint::new(id(), generation(3), None, 64, 1).unwrap(),
    210         ProjectionCheckpoint::new(
    211             id(),
    212             generation(2),
    213             Some(radroots_storage::event::EventPosition::new(
    214                 source.generation(),
    215                 radroots_storage::event::EventSequence::new(1).unwrap(),
    216             )),
    217             64,
    218             1,
    219         )
    220         .unwrap(),
    221     ] {
    222         assert_eq!(
    223             block_on(store.promote_projection_batch(
    224                 ProjectionDocumentBatch::new(next.clone(), vec![document("k00", 4)]).unwrap(),
    225                 checkpoint
    226             )),
    227             Err(Error::ProjectionCheckpointMismatch)
    228         );
    229         assert_eq!(
    230             block_on(store.projection_document(id(), generation(2), "k00".into()))
    231                 .unwrap()
    232                 .unwrap()
    233                 .value()
    234                 .len(),
    235             3
    236         );
    237     }
    238     block_on(store.promote_projection_batch(
    239         ProjectionDocumentBatch::new(next, vec![]).unwrap(),
    240         ProjectionCheckpoint::new(id(), generation(2), None, 64, 1).unwrap(),
    241     ))
    242     .unwrap();
    243     let empty = scope(source, Some(generation(2)), 3);
    244     block_on(store.promote_projection_batch(
    245         ProjectionDocumentBatch::new(empty, vec![]).unwrap(),
    246         ProjectionCheckpoint::new(id(), generation(3), None, 0, 2).unwrap(),
    247     ))
    248     .unwrap();
    249     assert!(
    250         block_on(store.query_projection_documents_at(query(3), source, generation(3)))
    251             .unwrap()
    252             .records()
    253             .is_empty()
    254     );
    255     let foreign = EventSourceToken::new(SourceGeneration::new([8; 32]).unwrap(), 0);
    256     assert_eq!(
    257         block_on(store.put_projection_batch(
    258             ProjectionDocumentBatch::new(scope(foreign, Some(generation(3)), 4), vec![]).unwrap()
    259         )),
    260         Err(Error::SourceEpochChanged)
    261     );
    262 }