Resolve a span change's authored semantic IDs under a receiving transaction. The hashed change is never modified. Legacy V1 operations with external IDs cannot be localized because their owner hashes were not recorded.
(
txn: &T,
change: &crate::change::Change,
)
| 253 | /// The hashed change is never modified. Legacy V1 operations with external |
| 254 | /// IDs cannot be localized because their owner hashes were not recorded. |
| 255 | pub fn localize_file_ops<T: crate::pristine::GraphTxnT>( |
| 256 | txn: &T, |
| 257 | change: &crate::change::Change, |
| 258 | ) -> anyhow::Result<Vec<crate::change::FileOps>> { |
| 259 | use crate::pristine::{EffectiveProjectionClosure, ViewMembershipSet}; |
| 260 | use crate::types::NodeId; |
| 261 | use anyhow::{ensure, Context}; |
| 262 | let Some(patch) = &change.hashed.span else { |
| 263 | return Ok(change.file_ops().to_vec()); |
| 264 | }; |
| 265 | patch.validate_content_ranges(change.contents.len().try_into()?)?; |
| 266 | let ids: std::collections::BTreeSet<_> = change |
| 267 | .file_ops() |
| 268 | .iter() |
| 269 | .flat_map(|ops| ops.referenced_node_ids()) |
| 270 | .collect(); |
| 271 | let owners: std::collections::BTreeSet<_> = patch |
| 272 | .semantic_owners |
| 273 | .iter() |
| 274 | .map(|entry| NodeId::new(entry.authored_id)) |
| 275 | .collect(); |
| 276 | ensure!( |
| 277 | ids == owners && patch.semantic_owners.len() == owners.len(), |
| 278 | "span semantic identities lack a complete portable owner mapping" |
| 279 | ); |
| 280 | let deps = change |
| 281 | .dependencies() |
| 282 | .iter() |
| 283 | .map(|hash| { |
| 284 | txn.get_internal(hash)? |
| 285 | .context("semantic dependency missing") |
| 286 | }) |
| 287 | .collect::<anyhow::Result<Vec<_>>>()?; |
| 288 | let closure = EffectiveProjectionClosure::try_from_membership( |
| 289 | txn, |
| 290 | &ViewMembershipSet::from_ordered(deps), |
| 291 | )?; |
| 292 | let mut mapping = std::collections::HashMap::new(); |
| 293 | for entry in &patch.semantic_owners { |
| 294 | let id = txn |
| 295 | .get_internal(&entry.owner)? |
| 296 | .context("semantic owner missing")?; |
| 297 | ensure!( |
| 298 | closure.contains(id) && txn.get_external(id)? == Some(entry.owner), |
| 299 | "span semantic owner is outside its dependency closure" |
| 300 | ); |
| 301 | mapping.insert(NodeId::new(entry.authored_id), id); |
| 302 | } |
| 303 | let mut ops = change.file_ops().to_vec(); |
| 304 | for file in &mut ops { |
| 305 | file.remap_referenced_node_ids(|id| { |
| 306 | mapping |
| 307 | .get(&id) |
| 308 | .copied() |
| 309 | .context("span semantic owner mapping is incomplete") |
| 310 | })?; |
| 311 | } |
| 312 | Ok(ops) |
no test coverage detected