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)
| 633 | /// files are reported in the result rather than aborting the rebuild. |
| 634 | /// Returns `(indexed_count, skipped_existing, corrupt_count)`. |
| 635 | pub fn rebuild_session_index(&self) -> Result<(usize, usize, usize), RepositoryError> { |
| 636 | self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; |
| 637 | let mut indexed = 0usize; |
| 638 | let mut skipped = 0usize; |
| 639 | let mut corrupt = 0usize; |
| 640 | |
| 641 | // Collect (hash, graph) pairs first so each write txn is short-lived. |
| 642 | let mut graphs: Vec<(Hash, atomic_core::change::ProvenanceGraph)> = Vec::new(); |
| 643 | for result in self.change_store.iter_provenance_graphs() { |
| 644 | match result { |
| 645 | Ok(hash) => match self.load_provenance_graph(&hash) { |
| 646 | Ok(graph) => graphs.push((hash, graph)), |
| 647 | Err(_) => corrupt += 1, |
| 648 | }, |
| 649 | Err(_) => corrupt += 1, |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | // Group by session. The core index derives canonical turn order from |
| 654 | // the complete set, independent of this ingestion order. |
| 655 | let mut by_session: std::collections::BTreeMap< |
| 656 | String, |
| 657 | Vec<(Hash, atomic_core::change::ProvenanceGraph)>, |
| 658 | > = std::collections::BTreeMap::new(); |
| 659 | let mut head_manifests = std::collections::BTreeMap::new(); |
| 660 | let mut fork_parents = std::collections::BTreeMap::new(); |
| 661 | for (hash, graph) in graphs { |
| 662 | by_session |
| 663 | .entry(graph.session_id.clone()) |
| 664 | .or_default() |
| 665 | .push((hash, graph)); |
| 666 | } |
| 667 | // Forked children can contain only inherited turns, whose provenance |
| 668 | // files still name the parent session. Include both existing ledgers |
| 669 | // and portable manifests so rebuild can create or migrate the child. |
| 670 | { |
| 671 | let txn = self |
| 672 | .pristine |
| 673 | .read_txn() |
| 674 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 675 | for record in txn |
| 676 | .list_session_records() |
| 677 | .map_err(|e| RepositoryError::Database(e.to_string()))? |
| 678 | { |
| 679 | if record.turn_count > 0 { |
| 680 | by_session.entry(record.session_id).or_default(); |
| 681 | } |
| 682 | } |
| 683 | for (head_session_id, head) in txn |
| 684 | .list_session_heads() |
| 685 | .map_err(|e| RepositoryError::Database(e.to_string()))? |
| 686 | { |
| 687 | match txn.get_session_manifest(&head) { |
| 688 | Ok(Some(manifest)) if manifest.session_id == head_session_id => { |
| 689 | if let (Some(parent_hash), Some(fork_turn)) = |
| 690 | (manifest.parent_session, manifest.fork_turn) |
| 691 | { |
| 692 | if let Ok(Some(parent)) = txn.get_session_manifest(&parent_hash) { |
nothing calls this directly
no test coverage detected