databuild/databuild/event_log/storage.rs
Stuart Axelbrooke f4c52cacc3
Some checks failed
/ setup (push) Has been cancelled
Big bump
2025-08-14 22:55:49 -07:00

75 lines
No EOL
2.6 KiB
Rust

use crate::*;
use async_trait::async_trait;
use super::Result;
/// Simple stdout storage backend for debugging
pub struct StdoutBELStorage;
impl StdoutBELStorage {
pub fn new() -> Self {
Self
}
}
#[async_trait]
impl BELStorage for StdoutBELStorage {
async fn append_event(&self, event: BuildEvent) -> Result<i64> {
let json = serde_json::to_string(&event)
.map_err(|e| BuildEventLogError::SerializationError(e.to_string()))?;
println!("BUILD_EVENT: {}", json);
Ok(0) // Return dummy index for stdout
}
async fn list_events(&self, _since_idx: i64, _filter: EventFilter) -> Result<EventPage> {
// Stdout implementation doesn't support querying
Err(BuildEventLogError::QueryError(
"Stdout storage backend doesn't support querying".to_string()
))
}
async fn initialize(&self) -> Result<()> {
Ok(()) // Nothing to initialize for stdout
}
}
/// Minimal append-only interface optimized for sequential scanning
#[async_trait]
pub trait BELStorage: Send + Sync {
/// Append a single event, returns the sequential index
async fn append_event(&self, event: BuildEvent) -> Result<i64>;
/// List events with filtering, starting from a given index
async fn list_events(&self, since_idx: i64, filter: EventFilter) -> Result<EventPage>;
/// Initialize storage backend (create tables, etc.)
async fn initialize(&self) -> Result<()>;
}
/// Factory function to create storage backends from URI
pub async fn create_bel_storage(uri: &str) -> Result<Box<dyn BELStorage>> {
if uri == "stdout" {
Ok(Box::new(StdoutBELStorage::new()))
} else if uri.starts_with("sqlite://") {
let path = &uri[9..]; // Remove "sqlite://" prefix
let storage = crate::event_log::sqlite_storage::SqliteBELStorage::new(path)?;
storage.initialize().await?;
Ok(Box::new(storage))
} else if uri.starts_with("postgres://") {
// TODO: Implement PostgresBELStorage
Err(BuildEventLogError::ConnectionError(
"PostgreSQL storage backend not yet implemented".to_string()
))
} else {
Err(BuildEventLogError::ConnectionError(
format!("Unsupported build event log URI: {}", uri)
))
}
}
/// Factory function to create query engine from URI
pub async fn create_bel_query_engine(uri: &str) -> Result<std::sync::Arc<crate::event_log::query_engine::BELQueryEngine>> {
let storage = create_bel_storage(uri).await?;
let storage_arc = std::sync::Arc::from(storage);
Ok(std::sync::Arc::new(crate::event_log::query_engine::BELQueryEngine::new(storage_arc)))
}