Verify and store a received ATOM object without changing its identity. Remote manifests reference the original serialized hash. Re-encoding a legacy object with the current writer would invalidate those references. Verification precedes both capability declaration and persistence.
(&self, hash: &Hash, bytes: &[u8])
| 193 | /// legacy object with the current writer would invalidate those references. |
| 194 | /// Verification precedes both capability declaration and persistence. |
| 195 | pub fn import_change_bytes(&self, hash: &Hash, bytes: &[u8]) -> Result<Hash, RepositoryError> { |
| 196 | let (change, computed) = Change::deserialize(&mut std::io::Cursor::new(bytes)) |
| 197 | .map_err(|error| RepositoryError::Serialization(error.to_string()))?; |
| 198 | if computed != *hash { |
| 199 | return Err(RepositoryError::Serialization(format!( |
| 200 | "Hash mismatch: expected {}, got {}", |
| 201 | hash.to_base32(), |
| 202 | computed.to_base32() |
| 203 | ))); |
| 204 | } |
| 205 | let projected = change.file_ops().iter().any(|ops| { |
| 206 | matches!( |
| 207 | ops.trunk_op(), |
| 208 | Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) |
| 209 | ) |
| 210 | }); |
| 211 | if projected && cfg!(not(unix)) { |
| 212 | return Err(RepositoryError::InvalidOperation { |
| 213 | message: "projected semantic change import is not supported on this platform" |
| 214 | .into(), |
| 215 | }); |
| 216 | } |
| 217 | if projected && change.hashed.span.is_none() { |
| 218 | return Err(RepositoryError::InvalidOperation { |
| 219 | message: "projected semantic edit requires a span change".into(), |
| 220 | }); |
| 221 | } |
| 222 | if change.hashed.span.is_some() { |
| 223 | use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; |
| 224 | let txn = self.pristine.read_txn()?; |
| 225 | let required = txn.required_capability_version(SPAN_RANGE_CAPABILITY.id())?; |
| 226 | drop(txn); |
| 227 | match required { |
| 228 | None => self.pristine.require_repository_capability(SPAN_RANGE_CAPABILITY)?, |
| 229 | Some(version) if version == SPAN_RANGE_CAPABILITY.minimum_version() => {} |
| 230 | Some(_) => return Err(RepositoryError::InvalidOperation { |
| 231 | message: "existing span capability requires migration before importing new span changes".into(), |
| 232 | }), |
| 233 | } |
| 234 | } |
| 235 | if projected { |
| 236 | self.pristine.require_repository_capability( |
| 237 | atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY, |
| 238 | )?; |
| 239 | } |
| 240 | let stored = self.save_change_bytes(hash, bytes, &change)?; |
| 241 | self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; |
| 242 | if !change.kind().is_snapshot() { |
| 243 | let hashes = self |
| 244 | .change_store |
| 245 | .iter_changes() |
| 246 | .collect::<Result<Vec<_>, _>>() |
| 247 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 248 | let mut txn = self.pristine.write_txn()?; |
| 249 | if txn.kg_writes_enabled()? { |
| 250 | self.project_change_kg(&mut txn, hash, &change)?; |
| 251 | self.repair_change_dependencies_kg(&mut txn, hash)?; |
| 252 | self.project_vault_kg(&mut txn, hashes)?; |