Skip to main content

hyprstream_metrics/checkpoint/
state.rs

1//! Checkpoint state types and metadata.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7/// Metadata for a checkpoint
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct CheckpointMetadata {
10    /// Unique checkpoint identifier (git commit hash)
11    pub id: String,
12    /// Human-readable checkpoint name/tag
13    pub name: Option<String>,
14    /// Timestamp when checkpoint was created
15    pub created_at: DateTime<Utc>,
16    /// Tables included in this checkpoint
17    pub tables: Vec<TableCheckpoint>,
18    /// Total size of checkpoint data in bytes
19    pub total_size: u64,
20    /// Additional metadata
21    pub metadata: HashMap<String, String>,
22}
23
24/// Information about a checkpointed table
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct TableCheckpoint {
27    /// Table name
28    pub name: String,
29    /// Number of rows in the table
30    pub row_count: u64,
31    /// Size of the Parquet file in bytes
32    pub file_size: u64,
33    /// Path to the Parquet file within the checkpoint
34    pub file_path: String,
35}
36
37/// A checkpoint instance
38#[derive(Debug, Clone)]
39pub struct Checkpoint {
40    /// Git commit ID for this checkpoint
41    pub commit_id: String,
42    /// Checkpoint metadata
43    pub metadata: CheckpointMetadata,
44}
45
46impl Checkpoint {
47    /// Create a new checkpoint
48    pub fn new(commit_id: String, metadata: CheckpointMetadata) -> Self {
49        Self {
50            commit_id,
51            metadata,
52        }
53    }
54
55    /// Get the checkpoint ID (git commit hash)
56    pub fn id(&self) -> &str {
57        &self.commit_id
58    }
59
60    /// Get the checkpoint name if set
61    pub fn name(&self) -> Option<&str> {
62        self.metadata.name.as_deref()
63    }
64
65    /// Get when the checkpoint was created
66    pub fn created_at(&self) -> DateTime<Utc> {
67        self.metadata.created_at
68    }
69
70    /// Get the list of tables in this checkpoint
71    pub fn tables(&self) -> &[TableCheckpoint] {
72        &self.metadata.tables
73    }
74
75    /// Get total checkpoint size in bytes
76    pub fn total_size(&self) -> u64 {
77        self.metadata.total_size
78    }
79}
80
81impl CheckpointMetadata {
82    /// Create new checkpoint metadata
83    pub fn new(tables: Vec<TableCheckpoint>) -> Self {
84        let total_size = tables.iter().map(|t| t.file_size).sum();
85        Self {
86            id: String::new(), // Will be set after commit
87            name: None,
88            created_at: Utc::now(),
89            tables,
90            total_size,
91            metadata: HashMap::new(),
92        }
93    }
94
95    /// Set the checkpoint name
96    pub fn with_name(mut self, name: impl Into<String>) -> Self {
97        self.name = Some(name.into());
98        self
99    }
100
101    /// Add custom metadata
102    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
103        self.metadata.insert(key.into(), value.into());
104        self
105    }
106}
107
108impl TableCheckpoint {
109    /// Create a new table checkpoint entry
110    pub fn new(name: String, row_count: u64, file_size: u64, file_path: String) -> Self {
111        Self {
112            name,
113            row_count,
114            file_size,
115            file_path,
116        }
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    fn test_checkpoint_metadata_creation() {
126        let tables = vec![
127            TableCheckpoint::new(
128                "metrics".to_owned(),
129                1000,
130                50000,
131                "tables/metrics.parquet".to_owned(),
132            ),
133            TableCheckpoint::new(
134                "events".to_owned(),
135                500,
136                25000,
137                "tables/events.parquet".to_owned(),
138            ),
139        ];
140
141        let metadata = CheckpointMetadata::new(tables)
142            .with_name("checkpoint-1")
143            .with_metadata("version", "1.0");
144
145        assert_eq!(metadata.name, Some("checkpoint-1".to_owned()));
146        assert_eq!(metadata.total_size, 75000);
147        assert_eq!(metadata.tables.len(), 2);
148        assert_eq!(metadata.metadata.get("version"), Some(&"1.0".to_owned()));
149    }
150
151    #[test]
152    fn test_checkpoint_creation() {
153        let tables = vec![TableCheckpoint::new(
154            "test".to_owned(),
155            100,
156            5000,
157            "tables/test.parquet".to_owned(),
158        )];
159
160        let metadata = CheckpointMetadata::new(tables);
161        let checkpoint = Checkpoint::new("abc123".to_owned(), metadata);
162
163        assert_eq!(checkpoint.id(), "abc123");
164        assert_eq!(checkpoint.tables().len(), 1);
165        assert_eq!(checkpoint.total_size(), 5000);
166    }
167}