oxirs_vec/compaction/
types.rs

1//! Core types for compaction system
2
3use serde::{Deserialize, Serialize};
4use std::time::{Duration, SystemTime};
5
6/// Compaction state
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub enum CompactionState {
9    /// Idle, not compacting
10    Idle,
11    /// Currently compacting
12    Running,
13    /// Paused (can be resumed)
14    Paused,
15    /// Failed (error occurred)
16    Failed,
17    /// Completed successfully
18    Completed,
19}
20
21/// Result of a compaction operation
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct CompactionResult {
24    /// Start time of compaction
25    pub start_time: SystemTime,
26    /// End time of compaction
27    pub end_time: SystemTime,
28    /// Duration of compaction
29    pub duration: Duration,
30    /// Number of vectors processed
31    pub vectors_processed: usize,
32    /// Number of vectors removed (deleted/duplicates)
33    pub vectors_removed: usize,
34    /// Bytes reclaimed
35    pub bytes_reclaimed: u64,
36    /// Fragmentation before compaction
37    pub fragmentation_before: f64,
38    /// Fragmentation after compaction
39    pub fragmentation_after: f64,
40    /// Success flag
41    pub success: bool,
42    /// Error message if failed
43    pub error: Option<String>,
44}
45
46/// Compaction statistics
47#[derive(Debug, Clone, Default, Serialize, Deserialize)]
48pub struct CompactionStatistics {
49    /// Total number of compactions performed
50    pub total_compactions: u64,
51    /// Successful compactions
52    pub successful_compactions: u64,
53    /// Failed compactions
54    pub failed_compactions: u64,
55    /// Total vectors processed
56    pub total_vectors_processed: usize,
57    /// Total vectors removed
58    pub total_vectors_removed: usize,
59    /// Total bytes reclaimed
60    pub total_bytes_reclaimed: u64,
61    /// Current fragmentation ratio (0.0 - 1.0)
62    pub current_fragmentation: f64,
63    /// Average compaction duration
64    pub avg_compaction_duration: Duration,
65    /// Last compaction time
66    pub last_compaction_time: Option<SystemTime>,
67    /// Last compaction result
68    pub last_compaction_result: Option<CompactionResult>,
69}
70
71/// Fragment information
72#[derive(Debug, Clone)]
73pub struct FragmentInfo {
74    /// Offset in the index
75    pub offset: usize,
76    /// Size of the fragment
77    pub size: usize,
78    /// Is this fragment free (deleted)?
79    pub is_free: bool,
80    /// Age of the fragment (time since creation)
81    pub age: Duration,
82}
83
84/// Compaction phase
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum CompactionPhase {
87    /// Analyzing fragmentation
88    Analyzing,
89    /// Identifying candidates for compaction
90    IdentifyingCandidates,
91    /// Moving vectors
92    MovingVectors,
93    /// Updating indices
94    UpdatingIndices,
95    /// Reclaiming space
96    ReclaimingSpace,
97    /// Verifying integrity
98    Verifying,
99    /// Completed
100    Completed,
101}
102
103/// Compaction progress
104#[derive(Debug, Clone)]
105pub struct CompactionProgress {
106    /// Current phase
107    pub phase: CompactionPhase,
108    /// Progress within current phase (0.0 - 1.0)
109    pub phase_progress: f64,
110    /// Overall progress (0.0 - 1.0)
111    pub overall_progress: f64,
112    /// Estimated time remaining
113    pub estimated_time_remaining: Option<Duration>,
114    /// Current throughput (vectors/sec)
115    pub throughput: f64,
116}
117
118/// Compaction candidate (vector to be compacted)
119#[derive(Debug, Clone)]
120pub struct CompactionCandidate {
121    /// Vector ID
122    pub vector_id: String,
123    /// Current offset
124    pub current_offset: usize,
125    /// Size in bytes
126    pub size_bytes: usize,
127    /// Priority (higher = more important to compact)
128    pub priority: f64,
129    /// Reason for compaction
130    pub reason: CompactionReason,
131}
132
133/// Reason for compaction
134#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
135pub enum CompactionReason {
136    /// Fragmentation
137    Fragmentation,
138    /// Deleted vector cleanup
139    DeletedCleanup,
140    /// Duplicate removal
141    DuplicateRemoval,
142    /// Size optimization
143    SizeOptimization,
144    /// Manual trigger
145    Manual,
146}
147
148/// Compaction batch
149#[derive(Debug, Clone)]
150pub struct CompactionBatch {
151    /// Batch ID
152    pub batch_id: u64,
153    /// Candidates in this batch
154    pub candidates: Vec<CompactionCandidate>,
155    /// Total size of batch in bytes
156    pub total_size_bytes: usize,
157    /// Estimated processing time
158    pub estimated_duration: Duration,
159}