briefcase_core/storage/
mod.rs1use crate::models::{DecisionSnapshot, Snapshot};
2#[cfg(feature = "async")]
3use async_trait::async_trait;
4use chrono::{DateTime, Utc};
5use std::collections::HashMap;
6use thiserror::Error;
7
8pub mod buffered;
9#[cfg(feature = "sqlite-storage")]
10pub mod sqlite;
11pub mod sync;
12#[cfg(feature = "vcs-storage")]
13pub mod vcs;
14
15#[cfg(feature = "sqlite-storage")]
16pub use sqlite::SqliteBackend;
17#[cfg(feature = "sqlite-storage")]
18pub use sync::SyncSqliteBackend;
19pub use sync::{MemoryStorageBackend, SyncStorageBackend};
20
21#[cfg(feature = "async")]
25#[async_trait]
26pub trait StorageBackend: Send + Sync {
27 async fn save(&self, snapshot: &Snapshot) -> Result<String, StorageError>;
29
30 async fn save_decision(&self, decision: &DecisionSnapshot) -> Result<String, StorageError>;
32
33 async fn save_decisions(
39 &self,
40 decisions: &[DecisionSnapshot],
41 ) -> Result<Vec<String>, StorageError> {
42 let mut ids = Vec::with_capacity(decisions.len());
43 for decision in decisions {
44 ids.push(self.save_decision(decision).await?);
45 }
46 Ok(ids)
47 }
48
49 async fn load(&self, snapshot_id: &str) -> Result<Snapshot, StorageError>;
51
52 async fn load_decision(&self, decision_id: &str) -> Result<DecisionSnapshot, StorageError>;
54
55 async fn query(&self, query: SnapshotQuery) -> Result<Vec<Snapshot>, StorageError>;
57
58 async fn delete(&self, snapshot_id: &str) -> Result<bool, StorageError>;
60
61 async fn flush(&self) -> Result<FlushResult, StorageError>;
63
64 async fn health_check(&self) -> Result<bool, StorageError>;
66}
67
68#[derive(Debug, Clone, Default)]
69pub struct SnapshotQuery {
70 pub function_name: Option<String>,
71 pub module_name: Option<String>,
72 pub model_name: Option<String>,
73 pub start_time: Option<DateTime<Utc>>,
74 pub end_time: Option<DateTime<Utc>>,
75 pub tags: Option<HashMap<String, String>>,
76 pub limit: Option<usize>,
77 pub offset: Option<usize>,
78}
79
80impl SnapshotQuery {
81 pub fn new() -> Self {
82 Self::default()
83 }
84
85 pub fn with_function_name(mut self, function_name: impl Into<String>) -> Self {
86 self.function_name = Some(function_name.into());
87 self
88 }
89
90 pub fn with_module_name(mut self, module_name: impl Into<String>) -> Self {
91 self.module_name = Some(module_name.into());
92 self
93 }
94
95 pub fn with_model_name(mut self, model_name: impl Into<String>) -> Self {
96 self.model_name = Some(model_name.into());
97 self
98 }
99
100 pub fn with_time_range(mut self, start: DateTime<Utc>, end: DateTime<Utc>) -> Self {
101 self.start_time = Some(start);
102 self.end_time = Some(end);
103 self
104 }
105
106 pub fn with_tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
107 if self.tags.is_none() {
108 self.tags = Some(HashMap::new());
109 }
110 self.tags.as_mut().unwrap().insert(key.into(), value.into());
111 self
112 }
113
114 pub fn with_limit(mut self, limit: usize) -> Self {
115 self.limit = Some(limit);
116 self
117 }
118
119 pub fn with_offset(mut self, offset: usize) -> Self {
120 self.offset = Some(offset);
121 self
122 }
123}
124
125#[derive(Debug, Clone)]
126pub struct FlushResult {
127 pub snapshots_written: usize,
128 pub bytes_written: usize,
129 pub checkpoint_id: Option<String>,
130}
131
132#[derive(Error, Debug, Clone, PartialEq)]
133pub enum StorageError {
134 #[error("Not found: {0}")]
135 NotFound(String),
136 #[error("Connection error: {0}")]
137 ConnectionError(String),
138 #[error("Serialization error: {0}")]
139 SerializationError(String),
140 #[error("Permission denied: {0}")]
141 PermissionDenied(String),
142 #[error("Quota exceeded")]
143 QuotaExceeded,
144 #[error("Invalid query: {0}")]
145 InvalidQuery(String),
146 #[error("IO error: {0}")]
147 IoError(String),
148}
149
150impl From<serde_json::Error> for StorageError {
151 fn from(err: serde_json::Error) -> Self {
152 StorageError::SerializationError(err.to_string())
153 }
154}
155
156impl From<std::io::Error> for StorageError {
157 fn from(err: std::io::Error) -> Self {
158 StorageError::IoError(err.to_string())
159 }
160}