Build a `TxClass` from a static write task slice. Extracts `(collection, surrogate)` pairs from each write task to build `EngineKeySet`s, constructs the `ReadWriteSet`, msgpack-encodes plans into `Vec `, and calls `TxClass::new`. Returns `Err(SequencerUnavailable)` if msgpack encoding of plans fails.
(
tasks: &[PhysicalTask],
tenant_id: TenantId,
)
| 178 | /// |
| 179 | /// Returns `Err(SequencerUnavailable)` if msgpack encoding of plans fails. |
| 180 | pub fn build_static_tx_class( |
| 181 | tasks: &[PhysicalTask], |
| 182 | tenant_id: TenantId, |
| 183 | ) -> crate::Result<TxClass> { |
| 184 | use std::collections::HashMap; |
| 185 | |
| 186 | // Collect surrogates per collection for write tasks. |
| 187 | let mut doc_surrogates: HashMap<String, Vec<u32>> = HashMap::new(); |
| 188 | |
| 189 | for task in tasks { |
| 190 | if !is_write_plan(&task.plan) { |
| 191 | continue; |
| 192 | } |
| 193 | let collection = collection_name_from_plan(&task.plan); |
| 194 | let surrogate = surrogate_from_plan(&task.plan); |
| 195 | doc_surrogates |
| 196 | .entry(collection) |
| 197 | .or_default() |
| 198 | .push(surrogate); |
| 199 | } |
| 200 | |
| 201 | // Build write set — one EngineKeySet per collection, sorted for |
| 202 | // determinism. |
| 203 | let mut write_sets: Vec<EngineKeySet> = doc_surrogates |
| 204 | .into_iter() |
| 205 | .map(|(collection, surrogates)| EngineKeySet::Document { |
| 206 | collection, |
| 207 | surrogates: SortedVec::new(surrogates), |
| 208 | }) |
| 209 | .collect(); |
| 210 | // Sort by collection name for determinism. |
| 211 | write_sets.sort_by(|a, b| a.collection().cmp(b.collection())); |
| 212 | |
| 213 | let write_set = ReadWriteSet::new(write_sets); |
| 214 | let read_set = ReadWriteSet::new(vec![]); |
| 215 | |
| 216 | // Encode all plans as msgpack bytes. |
| 217 | let plans: Vec<&PhysicalPlan> = tasks.iter().map(|t| &t.plan).collect(); |
| 218 | let plans_bytes = zerompk::to_msgpack_vec(&plans).map_err(|e| Error::Serialization { |
| 219 | format: "msgpack".to_owned(), |
| 220 | detail: format!("failed to encode PhysicalPlan vec for Calvin TxClass: {e}"), |
| 221 | })?; |
| 222 | |
| 223 | TxClass::new(read_set, write_set, plans_bytes, tenant_id, None).map_err(|e| Error::BadRequest { |
| 224 | detail: format!("invalid TxClass: {e}"), |
| 225 | }) |
| 226 | } |
| 227 | |
| 228 | /// Build a `TxClass` for a dependent-read (OLLP) transaction. |
| 229 | /// |
no test coverage detected