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

Method reinsert_change

atomic-core/src/pristine/txn/write/mod.rs:1515–1624  ·  view source on GitHub ↗
(
        &mut self,
        view: &mut ViewState,
        change_id: NodeId,
        change_hash: &Hash,
        at_sequence: u64,
    )

Source from the content-addressed store, hash-verified

1513 }
1514
1515 fn reinsert_change(
1516 &mut self,
1517 view: &mut ViewState,
1518 change_id: NodeId,
1519 change_hash: &Hash,
1520 at_sequence: u64,
1521 ) -> PristineResult<()> {
1522 // Clamp sequence to valid range
1523 let insert_at = at_sequence.min(view.change_count);
1524
1525 // Shift all changes from insert_at onwards up by 1
1526 // Work backwards to avoid overwriting
1527 for s in (insert_at..view.change_count).rev() {
1528 // Get the change_id at this sequence
1529 let cid = {
1530 let table = self.txn.open_table(VIEW_CHANGES)?;
1531 let key = encode_view_seq(view.id, s);
1532 let result = table.get(&key)?;
1533 match result {
1534 Some(v) => {
1535 let id = NodeId::new(v.value());
1536 drop(v);
1537 id
1538 }
1539 None => continue,
1540 }
1541 };
1542
1543 // Remove old entry
1544 {
1545 let mut table = self.txn.open_table(VIEW_CHANGES)?;
1546 let key = encode_view_seq(view.id, s);
1547 table.remove(&key)?;
1548 }
1549
1550 // Insert at new sequence (s + 1)
1551 {
1552 let mut table = self.txn.open_table(VIEW_CHANGES)?;
1553 let key = encode_view_seq(view.id, s + 1);
1554 table.insert(&key, cid.get())?;
1555 }
1556
1557 // Update reverse mapping
1558 {
1559 let mut table = self.txn.open_table(REV_VIEW_CHANGES)?;
1560 let key = encode_view_seq(view.id, cid.get());
1561 table.insert(&key, s + 1)?;
1562 }
1563 }
1564
1565 // Insert the new change at the specified position
1566 {
1567 let mut table = self.txn.open_table(VIEW_CHANGES)?;
1568 let key = encode_view_seq(view.id, insert_at);
1569 table.insert(&key, change_id.get())?;
1570 }
1571
1572 // Add reverse mapping

Callers 3

test_reinsert_changeFunction · 0.45

Calls 9

encode_view_seqFunction · 0.85
encode_view_merkleFunction · 0.85
valueMethod · 0.80
getMethod · 0.65
removeMethod · 0.65
insertMethod · 0.45
get_externalMethod · 0.45
nextMethod · 0.45
as_bytesMethod · 0.45

Tested by 3

test_reinsert_changeFunction · 0.36