hyprstream_metrics_api/storage/
mod.rs1#![allow(clippy::result_large_err)]
10
11pub mod view;
12
13use self::view::{ViewDefinition, ViewMetadata};
14use arrow::array::RecordBatch;
15use arrow::datatypes::{DataType, Schema};
16use async_trait::async_trait;
17use std::collections::HashMap;
18use std::sync::Arc;
19use tonic::Status;
20
21#[derive(Debug, Clone, Default)]
23pub struct Credentials {
24 pub username: Option<String>,
25 pub password: Option<String>,
26}
27
28pub struct StorageUtils;
30
31impl StorageUtils {
32 pub fn generate_create_table_sql(table_name: &str, schema: &Schema) -> Result<String, Status> {
34 let mut sql = format!("CREATE TABLE IF NOT EXISTS {table_name} (");
35 let mut first = true;
36
37 for field in schema.fields() {
38 if !first {
39 sql.push_str(", ");
40 }
41 first = false;
42
43 sql.push_str(&format!(
44 "{} {}",
45 field.name(),
46 match field.data_type() {
47 DataType::Int64 => "BIGINT",
48 DataType::Float64 => "DOUBLE PRECISION",
49 DataType::Utf8 => "VARCHAR",
50 DataType::Binary => "BLOB",
51 _ =>
52 return Err(Status::invalid_argument(format!(
53 "Unsupported data type: {:?}",
54 field.data_type()
55 ))),
56 }
57 ));
58 }
59
60 sql.push(')');
61 Ok(sql)
62 }
63
64 pub fn generate_insert_sql(table_name: &str, column_count: usize) -> String {
66 let placeholders = vec!["?"; column_count].join(", ");
67 format!("INSERT INTO {table_name} VALUES ({placeholders})")
68 }
69
70 pub fn generate_select_sql(table_name: &str, projection: Option<Vec<String>>) -> String {
72 let columns = projection
73 .map(|cols| cols.join(", "))
74 .unwrap_or_else(|| "*".to_owned());
75 format!("SELECT {columns} FROM {table_name}")
76 }
77
78 pub fn generate_view_sql(name: &str, definition: &ViewDefinition) -> String {
80 format!("CREATE VIEW {} AS {}", name, definition.to_sql())
81 }
82}
83
84#[async_trait]
86pub trait StorageBackend: Send + Sync + 'static {
87 async fn init(&self) -> Result<(), Status>;
89
90 async fn prepare_sql(&self, query: &str) -> Result<Vec<u8>, Status>;
93
94 async fn query_sql(&self, statement_handle: &[u8]) -> Result<RecordBatch, Status>;
97
98 fn new_with_options(
100 connection_string: &str,
101 options: &HashMap<String, String>,
102 credentials: Option<&Credentials>,
103 ) -> Result<Self, Status>
104 where
105 Self: Sized;
106
107 async fn create_table(&self, table_name: &str, schema: &Schema) -> Result<(), Status>;
109
110 async fn execute_ddl(&self, sql: &str) -> Result<(), Status> {
114 let handle = self.prepare_sql(sql).await?;
115 self.query_sql(&handle).await?;
116 Ok(())
117 }
118
119 async fn insert_into_table(&self, table_name: &str, batch: RecordBatch) -> Result<(), Status>;
121
122 async fn create_view(&self, name: &str, definition: ViewDefinition) -> Result<(), Status>;
124
125 async fn get_view(&self, name: &str) -> Result<ViewMetadata, Status>;
127
128 async fn list_views(&self) -> Result<Vec<String>, Status>;
130
131 async fn list_tables(&self) -> Result<Vec<String>, Status>;
133
134 async fn get_table_schema(&self, table_name: &str) -> Result<Arc<Schema>, Status>;
136
137 async fn drop_view(&self, name: &str) -> Result<(), Status>;
139
140 async fn drop_table(&self, table_name: &str) -> Result<(), Status>;
142
143 async fn export_to_parquet(
145 &self,
146 table_name: &str,
147 path: &std::path::Path,
148 ) -> Result<(), Status>;
149
150 async fn import_from_parquet(
152 &self,
153 table_name: &str,
154 path: &std::path::Path,
155 ) -> Result<(), Status>;
156}