MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / insert_change

Method insert_change

atomic-repository/src/repository/insert.rs:1649–1967  ·  view source on GitHub ↗

Insert a change into the current view. This is the high-level method for inserting a single change into the repository. It loads the change from the change store, validates dependencies, applies atoms to the graph, and updates the view state. # Arguments `hash` - The hash of the change to insert `options` - Options controlling insertion behavior # Returns An `InsertOutcome` containing the new

(
        &self,
        hash: &Hash,
        options: InsertOptions,
    )

Source from the content-addressed store, hash-verified

1647 /// println!("New state: {}", result.new_state.to_base32());
1648 /// ```
1649 pub fn insert_change(
1650 &self,
1651 hash: &Hash,
1652 options: InsertOptions,
1653 ) -> Result<InsertOutcome, RepositoryError> {
1654 let trace_insert = std::env::var_os("ATOMIC_TRACE_INSERT").is_some();
1655 let t0 = std::time::Instant::now();
1656
1657 // Load the change from the store
1658 let change = self.load_change(hash)?;
1659
1660 if trace_insert {
1661 eprintln!(
1662 "[insert_change] hash={} load_change elapsed={:?} hunks={} deps={}",
1663 &hash.to_base32()[..12],
1664 t0.elapsed(),
1665 change.hunks().len(),
1666 change.dependencies().len(),
1667 );
1668 }
1669
1670 // Get write transaction
1671 let mut txn = self
1672 .pristine
1673 .write_txn()
1674 .map_err(|e| RepositoryError::Database(e.to_string()))?;
1675
1676 // Check if the change's edges are already in the global GRAPH.
1677 //
1678 // A change is "already in the global graph" when it is registered
1679 // (has a NodeId) AND at least one of its vertices exists in the
1680 // GRAPH B-tree. `has_change_in_graph` performs a single O(log N)
1681 // range scan — far cheaper and more reliable than the previous
1682 // approach of loading the Change file and probing individual hunks.
1683 //
1684 // This correctly handles:
1685 // - Changes recorded on a Draft view (edges in GRAPH only)
1686 // → returns false, so hunks are re-applied to the global GRAPH
1687 // - Changes already inserted into a Shared view (edges in GRAPH)
1688 // → returns true, so redundant hunk application is skipped
1689 // - Changes with only EdgeUpdate hunks (no FileAdd/DirAdd)
1690 // → correctly detected via the range scan
1691 let t_check = std::time::Instant::now();
1692 let already_in_graph = if let Some(node_id) = txn
1693 .get_internal(hash)
1694 .map_err(|e| RepositoryError::Database(e.to_string()))?
1695 {
1696 let in_graph = txn
1697 .has_change_in_graph(node_id)
1698 .map_err(|e| RepositoryError::Database(e.to_string()))?;
1699 log::debug!(
1700 "insert_change: hash={} node_id={:?} already_in_graph={}",
1701 hash.to_base32(),
1702 node_id,
1703 in_graph
1704 );
1705 in_graph
1706 } else {

Calls 15

collect_tree_opsFunction · 0.85
explicit_emptyFunction · 0.85
apply_name_selectionsFunction · 0.85
write_change_to_graphFunction · 0.85
write_txnMethod · 0.80
register_changeMethod · 0.80
put_change_depsMethod · 0.80
put_treeMethod · 0.80
put_inodeMethod · 0.80
put_directoryMethod · 0.80
commitMethod · 0.80