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])
| 216 | /// legacy object with the current writer would invalidate those references. |
| 217 | /// Verification precedes both capability declaration and persistence. |
| 218 | pub fn import_change_bytes(&self, hash: &Hash, bytes: &[u8]) -> Result<Hash, RepositoryError> { |
| 219 | let (change, computed) = Change::deserialize(&mut std::io::Cursor::new(bytes)) |
| 220 | .map_err(|error| RepositoryError::Serialization(error.to_string()))?; |
| 221 | if computed != *hash { |
| 222 | return Err(RepositoryError::Serialization(format!( |
| 223 | "Hash mismatch: expected {}, got {}", |
| 224 | hash.to_base32(), |
| 225 | computed.to_base32() |
| 226 | ))); |
| 227 | } |
| 228 | self.fence_legacy_span_storage(&change)?; |
| 229 | let projected = change.file_ops().iter().any(|ops| { |
| 230 | matches!( |
| 231 | ops.trunk_op(), |
| 232 | Some(atomic_core::crdt::TrunkOp::ProjectedSpanEdit { .. }) |
| 233 | ) |
| 234 | }); |
| 235 | if projected && change.hashed.span.is_none() { |
| 236 | return Err(RepositoryError::InvalidOperation { |
| 237 | message: "projected semantic edit requires a span change".into(), |
| 238 | }); |
| 239 | } |
| 240 | if change.hashed.span.is_some() { |
| 241 | use atomic_core::pristine::{CapabilityTxnT, SPAN_RANGE_CAPABILITY}; |
| 242 | let txn = self.pristine.read_txn()?; |
| 243 | let required = txn.required_capability_version(SPAN_RANGE_CAPABILITY.id())?; |
| 244 | drop(txn); |
| 245 | match required { |
| 246 | None => self.pristine.require_repository_capability(SPAN_RANGE_CAPABILITY)?, |
| 247 | Some(version) if version == SPAN_RANGE_CAPABILITY.minimum_version() => {} |
| 248 | Some(_) => return Err(RepositoryError::InvalidOperation { |
| 249 | message: "existing span capability requires migration before importing new span changes".into(), |
| 250 | }), |
| 251 | } |
| 252 | } |
| 253 | if projected { |
| 254 | self.pristine.require_repository_capability( |
| 255 | atomic_core::pristine::SPAN_PROJECTED_SEMANTICS_CAPABILITY, |
| 256 | )?; |
| 257 | } |
| 258 | let stored = self.save_change_bytes(hash, bytes, &change)?; |
| 259 | self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; |
| 260 | if !change.kind().is_snapshot() { |
| 261 | let hashes = self |
| 262 | .change_store |
| 263 | .iter_changes() |
| 264 | .collect::<Result<Vec<_>, _>>() |
| 265 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 266 | let mut txn = self.pristine.write_txn()?; |
| 267 | if txn.kg_writes_enabled()? { |
| 268 | self.project_change_kg(&mut txn, hash, &change)?; |
| 269 | self.repair_change_dependencies_kg(&mut txn, hash)?; |
| 270 | self.project_vault_kg(&mut txn, hashes)?; |
| 271 | } |
| 272 | txn.commit()?; |
| 273 | } |
| 274 | Ok(stored) |
| 275 | } |