hyprstream_metrics/storage/
table_manager.rs1use crate::aggregation::{AggregateFunction, GroupBy, TimeWindow};
2use duckdb::arrow::datatypes::Schema;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::sync::Arc;
6use tokio::sync::RwLock;
7use tonic::Status;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct AggregationView {
12 pub source_table: String,
13 pub function: AggregateFunction,
14 pub group_by: GroupBy,
15 pub window: TimeWindow,
16 pub aggregate_columns: Vec<String>,
17}
18
19#[derive(Debug)]
20pub struct TableManager {
21 tables: Arc<RwLock<HashMap<String, Schema>>>,
22 views: Arc<RwLock<HashMap<String, AggregationView>>>,
23}
24
25impl Clone for TableManager {
26 fn clone(&self) -> Self {
27 Self {
28 tables: self.tables.clone(),
29 views: self.views.clone(),
30 }
31 }
32}
33
34impl Default for TableManager {
35 fn default() -> Self {
36 Self::new()
37 }
38}
39
40impl TableManager {
41 pub fn new() -> Self {
42 Self {
43 tables: Arc::new(RwLock::new(HashMap::new())),
44 views: Arc::new(RwLock::new(HashMap::new())),
45 }
46 }
47
48 pub async fn create_table(&self, name: String, schema: Schema) -> Result<(), Status> {
49 let mut tables = self.tables.write().await;
50 if tables.contains_key(&name) {
51 return Err(Status::already_exists(format!(
52 "Table {name} already exists"
53 )));
54 }
55 tables.insert(name, schema);
56 Ok(())
57 }
58
59 pub async fn get_table_schema(&self, name: &str) -> Result<Schema, Status> {
60 let tables = self.tables.read().await;
61 tables
62 .get(name)
63 .cloned()
64 .ok_or_else(|| Status::not_found(format!("Table {name} not found")))
65 }
66
67 pub async fn create_aggregation_view(
68 &self,
69 name: String,
70 source_table: String,
71 function: AggregateFunction,
72 group_by: GroupBy,
73 window: TimeWindow,
74 aggregate_columns: Vec<String>,
75 ) -> Result<(), Status> {
76 {
78 let tables = self.tables.read().await;
79 let schema = tables.get(&source_table).ok_or_else(|| {
80 Status::not_found(format!("Source table {source_table} not found"))
81 })?;
82
83 for col in &aggregate_columns {
85 if !schema.fields().iter().any(|f| f.name() == col) {
86 return Err(Status::invalid_argument(format!(
87 "Column {col} not found in source table {source_table}"
88 )));
89 }
90 }
91 }
92
93 let view = AggregationView {
94 source_table,
95 function,
96 group_by,
97 window,
98 aggregate_columns,
99 };
100
101 let mut views = self.views.write().await;
102 if views.contains_key(&name) {
103 return Err(Status::already_exists(format!(
104 "View {name} already exists"
105 )));
106 }
107 views.insert(name, view);
108 Ok(())
109 }
110
111 pub async fn get_aggregation_view(&self, name: &str) -> Result<AggregationView, Status> {
112 let views = self.views.read().await;
113 views
114 .get(name)
115 .cloned()
116 .ok_or_else(|| Status::not_found(format!("View {name} not found")))
117 }
118
119 pub async fn list_tables(&self) -> Vec<String> {
120 let tables = self.tables.read().await;
121 tables.keys().cloned().collect()
122 }
123
124 pub async fn list_aggregation_views(&self) -> Vec<String> {
125 let views = self.views.read().await;
126 views.keys().cloned().collect()
127 }
128
129 pub async fn drop_table(&self, name: &str) -> Result<(), Status> {
130 let mut tables = self.tables.write().await;
131 if tables.remove(name).is_none() {
132 return Err(Status::not_found(format!("Table {name} not found")));
133 }
134 Ok(())
135 }
136
137 pub async fn drop_aggregation_view(&self, name: &str) -> Result<(), Status> {
138 let mut views = self.views.write().await;
139 if views.remove(name).is_none() {
140 return Err(Status::not_found(format!("View {name} not found")));
141 }
142 Ok(())
143 }
144}