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,
)
| 1559 | /// println!("New state: {}", result.new_state.to_base32()); |
| 1560 | /// ``` |
| 1561 | pub fn insert_change( |
| 1562 | &self, |
| 1563 | hash: &Hash, |
| 1564 | options: InsertOptions, |
| 1565 | ) -> Result<InsertOutcome, RepositoryError> { |
| 1566 | let trace_insert = std::env::var_os("ATOMIC_TRACE_INSERT").is_some(); |
| 1567 | let t0 = std::time::Instant::now(); |
| 1568 | |
| 1569 | // Load the change from the store |
| 1570 | let change = self.load_change(hash)?; |
| 1571 | |
| 1572 | if trace_insert { |
| 1573 | eprintln!( |
| 1574 | "[insert_change] hash={} load_change elapsed={:?} hunks={} deps={}", |
| 1575 | &hash.to_base32()[..12], |
| 1576 | t0.elapsed(), |
| 1577 | change.hunks().len(), |
| 1578 | change.dependencies().len(), |
| 1579 | ); |
| 1580 | } |
| 1581 | |
| 1582 | // Get write transaction |
| 1583 | let mut txn = self |
| 1584 | .pristine |
| 1585 | .write_txn() |
| 1586 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1587 | |
| 1588 | // Check if the change's edges are already in the global GRAPH. |
| 1589 | // |
| 1590 | // A change is "already in the global graph" when it is registered |
| 1591 | // (has a NodeId) AND at least one of its vertices exists in the |
| 1592 | // GRAPH B-tree. `has_change_in_graph` performs a single O(log N) |
| 1593 | // range scan — far cheaper and more reliable than the previous |
| 1594 | // approach of loading the Change file and probing individual hunks. |
| 1595 | // |
| 1596 | // This correctly handles: |
| 1597 | // - Changes recorded on a Draft view (edges in GRAPH only) |
| 1598 | // → returns false, so hunks are re-applied to the global GRAPH |
| 1599 | // - Changes already inserted into a Shared view (edges in GRAPH) |
| 1600 | // → returns true, so redundant hunk application is skipped |
| 1601 | // - Changes with only EdgeUpdate hunks (no FileAdd/DirAdd) |
| 1602 | // → correctly detected via the range scan |
| 1603 | let t_check = std::time::Instant::now(); |
| 1604 | let already_in_graph = if let Some(node_id) = txn |
| 1605 | .get_internal(hash) |
| 1606 | .map_err(|e| RepositoryError::Database(e.to_string()))? |
| 1607 | { |
| 1608 | let in_graph = txn |
| 1609 | .has_change_in_graph(node_id) |
| 1610 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1611 | log::debug!( |
| 1612 | "insert_change: hash={} node_id={:?} already_in_graph={}", |
| 1613 | hash.to_base32(), |
| 1614 | node_id, |
| 1615 | in_graph |
| 1616 | ); |
| 1617 | in_graph |
| 1618 | } else { |