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

Method rebuild_session_index

atomic-repository/src/repository/changes.rs:684–863  ·  view source on GitHub ↗

Rebuild session indexes from stored provenance graphs. Idempotent: existing `(session, provenance)` pairs are skipped, so a rebuilt repository never gains duplicate turns. Corrupt provenance files are reported in the result rather than aborting the rebuild. Returns `(indexed_count, skipped_existing, corrupt_count)`.

(&self)

Source from the content-addressed store, hash-verified

682 /// files are reported in the result rather than aborting the rebuild.
683 /// Returns `(indexed_count, skipped_existing, corrupt_count)`.
684 pub fn rebuild_session_index(&self) -> Result<(usize, usize, usize), RepositoryError> {
685 let _review_boundary = self.review_evidence_lock()?;
686 self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?;
687 let mut indexed = 0usize;
688 let mut skipped = 0usize;
689 let mut corrupt = 0usize;
690
691 // Collect (hash, graph) pairs first so each write txn is short-lived.
692 let mut graphs: Vec<(Hash, atomic_core::change::ProvenanceGraph)> = Vec::new();
693 for result in self.change_store.iter_provenance_graphs() {
694 match result {
695 Ok(hash) => match self.load_provenance_graph(&hash) {
696 Ok(graph) => graphs.push((hash, graph)),
697 Err(_) => corrupt += 1,
698 },
699 Err(_) => corrupt += 1,
700 }
701 }
702
703 // Group by session. The core index derives canonical turn order from
704 // the complete set, independent of this ingestion order.
705 let mut by_session: std::collections::BTreeMap<
706 String,
707 Vec<(Hash, atomic_core::change::ProvenanceGraph)>,
708 > = std::collections::BTreeMap::new();
709 let mut head_manifests = std::collections::BTreeMap::new();
710 let mut fork_parents = std::collections::BTreeMap::new();
711 for (hash, graph) in graphs {
712 by_session
713 .entry(graph.session_id.clone())
714 .or_default()
715 .push((hash, graph));
716 }
717 // Forked children can contain only inherited turns, whose provenance
718 // files still name the parent session. Include both existing ledgers
719 // and portable manifests so rebuild can create or migrate the child.
720 {
721 let txn = self
722 .pristine
723 .read_txn()
724 .map_err(|e| RepositoryError::Database(e.to_string()))?;
725 for record in txn
726 .list_session_records()
727 .map_err(|e| RepositoryError::Database(e.to_string()))?
728 {
729 if record.turn_count > 0 {
730 by_session.entry(record.session_id).or_default();
731 }
732 }
733 for (head_session_id, head) in txn
734 .list_session_heads()
735 .map_err(|e| RepositoryError::Database(e.to_string()))?
736 {
737 match txn.get_session_manifest(&head) {
738 Ok(Some(manifest)) if manifest.session_id == head_session_id => {
739 if let (Some(parent_hash), Some(fork_turn)) =
740 (manifest.parent_session, manifest.fork_turn)
741 {

Callers

nothing calls this directly

Calls 15

epoch_millisFunction · 0.85
review_evidence_lockMethod · 0.80
read_txnMethod · 0.80
list_session_recordsMethod · 0.80
list_session_headsMethod · 0.80
get_session_recordMethod · 0.80
get_session_turnsMethod · 0.80
write_txnMethod · 0.80
index_session_turnMethod · 0.80
index_inherited_turnMethod · 0.80

Tested by

no test coverage detected