use std::collections::HashMap;
use tokio::sync::RwLock;
use tokio_util::sync::CancellationToken;
#[derive(Default)]
pub struct QueryTracker {
active: RwLock<HashMap<String, CancellationToken>>,
}
impl QueryTracker {
pub fn new() -> Self {
Self {
active: RwLock::new(HashMap::new()),
}
}
pub async fn register(
&self,
query_id: &str,
) -> Result<CancellationToken, crate::error::DatasetEngineError> {
let mut active = self.active.write().await;
if active.contains_key(query_id) {
return Err(crate::error::DatasetEngineError::DuplicateQueryId(
query_id.to_string(),
));
}
let token = CancellationToken::new();
active.insert(query_id.to_string(), token.clone());
Ok(token)
}
pub async fn cancel(&self, query_id: &str) -> bool {
if let Some(token) = self.active.write().await.remove(query_id) {
token.cancel();
true
} else {
false
}
}
pub async fn remove(&self, query_id: &str) {
self.active.write().await.remove(query_id);
}
}
pub struct QueryResult {
pub batches: Vec<arrow_array::RecordBatch>,
pub metadata: QueryExecutionMetadata,
}
#[derive(Debug, Clone)]
pub struct QueryExecutionMetadata {
pub query_id: String,
pub rows_returned: u64,
pub truncated: bool,
pub execution_time_ms: u64,
pub bytes_scanned: Option<u64>,
}