Skip to main content

linkrs_core/
index_events.rs

1use std::sync::Arc;
2
3/// Rebuild phase for progress reporting and crash diagnosis. Shared by the
4/// fulltext and vector online rebuild drivers.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum RebuildPhase {
7    Preparing,
8    Backfilling,
9    CatchingUp,
10    Publishing,
11    Completed,
12    Failed,
13}
14
15impl RebuildPhase {
16    pub fn as_str(self) -> &'static str {
17        match self {
18            RebuildPhase::Preparing => "preparing",
19            RebuildPhase::Backfilling => "backfilling",
20            RebuildPhase::CatchingUp => "catching_up",
21            RebuildPhase::Publishing => "publishing",
22            RebuildPhase::Completed => "completed",
23            RebuildPhase::Failed => "failed",
24        }
25    }
26}
27
28/// Mutable rebuild counters owned by the index managers and updated by the
29/// sync-side rebuild drivers. `docs_*` count documents for fulltext and
30/// point operations for vector rebuilds.
31#[derive(Debug, Clone)]
32pub struct RebuildProgress {
33    pub space_id: u64,
34    pub tag_name: String,
35    pub field_name: String,
36    pub generation: u64,
37    pub phase: RebuildPhase,
38    pub docs_scanned: u64,
39    pub docs_applied: u64,
40    pub docs_skipped: u64,
41    pub started_at: chrono::DateTime<chrono::Utc>,
42}
43
44/// Fulltext/vector index lifecycle notification.
45///
46/// Emission is best-effort and synchronous on the index management path.
47/// Observers must return quickly and must not call back into the index
48/// manager while handling an event.
49#[derive(Debug, Clone)]
50pub enum IndexEvent {
51    FulltextBuildStarted {
52        index_name: String,
53    },
54    FulltextBuildCompleted {
55        index_name: String,
56        docs_count: u64,
57    },
58    FulltextRefresh {
59        index_name: String,
60    },
61    FulltextDropped {
62        index_name: String,
63    },
64    FulltextRebuildStarted {
65        index_name: String,
66        generation: u64,
67    },
68    /// Emitted on rebuild phase transitions (not per batch) with running
69    /// counters; per-batch progress is queryable via rebuild progress APIs.
70    FulltextRebuildProgress {
71        index_name: String,
72        generation: u64,
73        phase: String,
74        docs_applied: u64,
75    },
76    FulltextRebuildCompleted {
77        index_name: String,
78        generation: u64,
79        docs_count: u64,
80    },
81    FulltextRebuildFailed {
82        index_name: String,
83        generation: u64,
84        reason: String,
85    },
86    VectorBuildStarted {
87        index_name: String,
88    },
89    VectorBuildCompleted {
90        index_name: String,
91        vectors_count: u64,
92    },
93    VectorRebuildStarted {
94        index_name: String,
95        generation: u64,
96    },
97    /// Emitted on vector rebuild phase transitions (not per batch).
98    VectorRebuildProgress {
99        index_name: String,
100        generation: u64,
101        phase: String,
102        vectors_applied: u64,
103    },
104    VectorRebuildCompleted {
105        index_name: String,
106        generation: u64,
107        vectors_count: u64,
108    },
109    VectorRebuildFailed {
110        index_name: String,
111        generation: u64,
112        reason: String,
113    },
114    VectorDropped {
115        index_name: String,
116    },
117    IndexMergeStarted {
118        index_name: String,
119        segments: usize,
120    },
121    IndexMergeCompleted {
122        index_name: String,
123    },
124}
125
126/// Runtime observer for index lifecycle events.
127pub type IndexEventCallback = Arc<dyn Fn(&IndexEvent) + Send + Sync>;