selection_tests.rs (10744B)
1 use super::*; 2 use crate::{OpenMode, OpenOptions, Paths}; 3 use radroots_storage::{ 4 ProjectionStore, 5 event::{EventSourceToken, SourceGeneration}, 6 memory::MemoryStorage, 7 projection::{ 8 InvalidationReason, ProjectionCheckpoint, ProjectionId, ProjectionInvalidation, 9 batch::{ProjectionDocumentBatch, ProjectionWriteScope}, 10 document_query::ProjectionDocumentKeyRange, 11 }, 12 }; 13 use sqlx::Execute; 14 use tempfile::TempDir; 15 16 fn id() -> ProjectionId { 17 ProjectionId::parse("fixture").unwrap() 18 } 19 fn generation(byte: u8) -> ProjectionGeneration { 20 ProjectionGeneration::new([byte; 32]).unwrap() 21 } 22 fn range(first: &str, last: &str) -> ProjectionDocumentKeyRange { 23 ProjectionDocumentKeyRange::new(first.into(), last.into()).unwrap() 24 } 25 fn query(limit: u16) -> ProjectionDocumentQuery { 26 ProjectionDocumentQuery::new( 27 id(), 28 ProjectionDocumentGenerations::Exact(generation(1)), 29 limit, 30 ) 31 .unwrap() 32 } 33 async fn open(temp: &TempDir) -> SqliteStorage { 34 SqliteStorage::open( 35 OpenOptions::new( 36 Paths::from_directory(temp.path()).unwrap(), 37 OpenMode::Create, 38 ) 39 .with_source_generation(SourceGeneration::new([9; 32]).unwrap(), 9) 40 .unwrap(), 41 ) 42 .await 43 .unwrap() 44 } 45 async fn promote(store: &dyn radroots_storage::Storage) -> EventSourceToken { 46 let source = store.source_token().await.unwrap(); 47 let scope = ProjectionWriteScope::new(id(), generation(1), source, None).unwrap(); 48 let documents: Vec<_> = ["a", "m", "z"] 49 .into_iter() 50 .flat_map(|prefix| { 51 (0..100).map(move |number| { 52 ProjectionDocument::new(format!("{prefix}.{number:04}"), vec![1; 4]).unwrap() 53 }) 54 }) 55 .collect(); 56 for chunk in documents.chunks(64) { 57 store 58 .put_projection_batch( 59 ProjectionDocumentBatch::new(scope.clone(), chunk.to_vec()).unwrap(), 60 ) 61 .await 62 .unwrap(); 63 } 64 store 65 .promote_projection_batch( 66 ProjectionDocumentBatch::new( 67 scope, 68 vec![ProjectionDocument::new("zz.end".into(), vec![1]).unwrap()], 69 ) 70 .unwrap(), 71 ProjectionCheckpoint::new(id(), generation(1), None, 301, 9).unwrap(), 72 ) 73 .await 74 .unwrap(); 75 source 76 } 77 78 #[tokio::test] 79 async fn selected_primary_key_pages_match_memory_and_portable_seek_after_reopen() { 80 let temp = TempDir::new().unwrap(); 81 let store = open(&temp).await; 82 let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); 83 let source = promote(&store).await; 84 assert_eq!(promote(&memory).await, source); 85 let q = query(17) 86 .with_key_range(range("m.0003", "m.0098")) 87 .unwrap() 88 .with_payload_budget(68) 89 .unwrap(); 90 let mut selected = q.clone(); 91 let mut found = Vec::new(); 92 loop { 93 let page = store 94 .query_projection_documents_at(selected.clone(), source, generation(1)) 95 .await 96 .unwrap(); 97 assert_eq!( 98 page, 99 memory 100 .query_projection_documents_at(selected.clone(), source, generation(1)) 101 .await 102 .unwrap() 103 ); 104 assert!(page.records().len() <= 17); 105 assert!( 106 page.records() 107 .iter() 108 .map(|row| row.document().unwrap().value().len()) 109 .sum::<usize>() 110 <= 68 111 ); 112 found.extend(page.records().iter().map(|row| row.key().to_owned())); 113 let Some(cursor) = page.next_cursor() else { 114 break; 115 }; 116 selected = q.clone().with_cursor(cursor).unwrap(); 117 assert_eq!( 118 selected, 119 q.clone() 120 .with_after_key(found.last().unwrap().clone()) 121 .unwrap() 122 ); 123 } 124 assert_eq!( 125 found, 126 (3..=98) 127 .map(|number| format!("m.{number:04}")) 128 .collect::<Vec<_>>() 129 ); 130 let resume = q.clone().with_after_key("m.0050".into()).unwrap(); 131 let expected = memory 132 .query_projection_documents_at(resume.clone(), source, generation(1)) 133 .await 134 .unwrap(); 135 store.close().await.unwrap(); 136 let store = SqliteStorage::open(OpenOptions::new( 137 Paths::from_directory(temp.path()).unwrap(), 138 OpenMode::ReadOnly, 139 )) 140 .await 141 .unwrap(); 142 assert_eq!( 143 store 144 .query_projection_documents_at(resume, source, generation(1)) 145 .await 146 .unwrap(), 147 expected 148 ); 149 assert!( 150 store 151 .query_projection_documents_at( 152 q.with_after_key("m.0098".into()).unwrap(), 153 source, 154 generation(1) 155 ) 156 .await 157 .unwrap() 158 .records() 159 .is_empty() 160 ); 161 let mut builder = selection(&selected); 162 let mut actual = builder.build(); 163 let arguments = actual.take_arguments().unwrap().unwrap(); 164 let explanation = format!("EXPLAIN QUERY PLAN {}", actual.sql().as_ref()); 165 // Only a constant prefix decorates the actual bound-value selection SQL. 166 let mut explanation = 167 sqlx::QueryBuilder::<sqlx::Sqlite>::with_arguments(explanation, arguments); 168 let rows = explanation.build().fetch_all(store.pool()).await.unwrap(); 169 let details: Vec<String> = rows.iter().map(|row| row.get("detail")).collect(); 170 assert!( 171 details.iter().any(|value| value.contains("PRIMARY KEY") 172 && value.contains("document_key>") 173 && value.contains("document_key<")), 174 "{details:?}" 175 ); 176 assert!( 177 !details.iter().any(|value| value.starts_with("SCAN ")), 178 "{details:?}" 179 ); 180 store.close().await.unwrap(); 181 } 182 183 #[tokio::test] 184 async fn lower_payload_budget_preflights_valid_values_and_preserves_corruption() { 185 let temp = TempDir::new().unwrap(); 186 let store = open(&temp).await; 187 let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); 188 for backend in [&store as &dyn ProjectionStore, &memory] { 189 for (key, bytes) in [("a", 4), ("b", 6)] { 190 backend 191 .put_projection_document( 192 id(), 193 generation(1), 194 ProjectionDocument::new(key.into(), vec![7; bytes]).unwrap(), 195 ) 196 .await 197 .unwrap(); 198 } 199 let q = query(2).with_payload_budget(4).unwrap(); 200 let first = backend.query_projection_documents(q.clone()).await.unwrap(); 201 assert_eq!(first.records().len(), 1); 202 assert_eq!(first.records()[0].key(), "a"); 203 assert_eq!( 204 backend 205 .query_projection_documents(q.with_cursor(first.next_cursor().unwrap()).unwrap()) 206 .await, 207 Err(Error::SpaceInsufficient) 208 ); 209 assert_eq!( 210 backend 211 .query_projection_documents( 212 query(1) 213 .with_key_range(range("a", "a")) 214 .unwrap() 215 .with_payload_budget(3) 216 .unwrap() 217 ) 218 .await, 219 Err(Error::SpaceInsufficient) 220 ); 221 let exact = backend 222 .query_projection_documents( 223 query(1) 224 .with_key_range(range("b", "b")) 225 .unwrap() 226 .with_payload_budget(6) 227 .unwrap(), 228 ) 229 .await 230 .unwrap(); 231 assert_eq!(exact.records()[0].document().unwrap().value(), &[7; 6]); 232 assert!(exact.next_cursor().is_none()); 233 assert_eq!( 234 backend 235 .projection_document(id(), generation(1), "a".into()) 236 .await 237 .unwrap() 238 .unwrap() 239 .value(), 240 &[7; 4] 241 ); 242 } 243 sqlx::query("UPDATE radroots_runtime_projection_documents SET value_sha256=zeroblob(32) WHERE document_key='b'").execute(store.pool()).await.unwrap(); 244 let b = query(1).with_key_range(range("b", "b")).unwrap(); 245 assert_eq!( 246 store 247 .query_projection_documents(b.clone().with_payload_budget(4).unwrap()) 248 .await, 249 Err(Error::SpaceInsufficient) 250 ); 251 assert!( 252 store 253 .query_projection_documents(b.with_payload_budget(6).unwrap()) 254 .await 255 .unwrap() 256 .records()[0] 257 .document() 258 .is_none() 259 ); 260 store.close().await.unwrap(); 261 } 262 263 #[tokio::test] 264 async fn key_selection_retains_source_active_and_ready_transaction_fences() { 265 let temp = TempDir::new().unwrap(); 266 let store = open(&temp).await; 267 let memory = MemoryStorage::new(SourceGeneration::new([9; 32]).unwrap()); 268 for backend in [&store as &dyn radroots_storage::Storage, &memory] { 269 let source = promote(backend).await; 270 let q = query(1).with_key_range(range("m.0000", "m.0000")).unwrap(); 271 assert_eq!( 272 backend 273 .query_projection_documents_at(q.clone(), source, generation(1)) 274 .await 275 .unwrap() 276 .records()[0] 277 .key(), 278 "m.0000" 279 ); 280 let foreign = EventSourceToken::new(SourceGeneration::new([8; 32]).unwrap(), 0); 281 assert_eq!( 282 backend 283 .query_projection_documents_at(q.clone(), foreign, generation(1)) 284 .await, 285 Err(Error::SourceEpochChanged) 286 ); 287 assert_eq!( 288 backend 289 .query_projection_documents_at(q.clone(), source, generation(2)) 290 .await, 291 Err(Error::InvalidProjectionDocument) 292 ); 293 backend 294 .invalidate( 295 ProjectionInvalidation::new( 296 id(), 297 generation(1), 298 generation(2), 299 InvalidationReason::ProjectionGenerationChanged, 300 10, 301 ) 302 .unwrap(), 303 ) 304 .await 305 .unwrap(); 306 assert_eq!( 307 backend 308 .query_projection_documents_at(q, source, generation(1)) 309 .await, 310 Err(Error::ProjectionRevisionConflict) 311 ); 312 assert_eq!( 313 backend 314 .projection_document(id(), generation(1), "m.0000".into()) 315 .await 316 .unwrap() 317 .unwrap() 318 .value(), 319 &[1; 4] 320 ); 321 } 322 store.close().await.unwrap(); 323 }