use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::Stream;
use sqlx::Row;
use sqlx::SqlitePool;
use tokio::sync::broadcast;
use uuid::Uuid;
use ff_core::backend::ScannerFilter;
use ff_core::engine_error::{EngineError, ValidationKind};
use ff_core::stream_events::{CompletionEvent, CompletionOutcome, CompletionSubscription};
use ff_core::stream_subscribe::{
StreamCursor, decode_postgres_event_cursor, encode_postgres_event_cursor,
};
use ff_core::types::{ExecutionId, TimestampMs};
use crate::outbox_cursor::{self, OutboxCursorConfig, OutboxCursorStream, RowDecoder};
use crate::pubsub::OutboxEvent;
pub(crate) const SELECT_COMPLETION_EVENTS_SQL: &str = r#"
SELECT event_id, execution_id, outcome, occurred_at_ms,
partition_key, namespace, instance_tag
FROM ff_completion_event
WHERE partition_key = ?1 AND event_id > ?2
ORDER BY event_id ASC
LIMIT ?3
"#;
const PARTITION_KEY: i64 = 0;
const REPLAY_BATCH: i64 = 256;
pub(crate) async fn subscribe(
pool: SqlitePool,
wakeup: broadcast::Receiver<OutboxEvent>,
cursor: StreamCursor,
filter: ScannerFilter,
) -> Result<CompletionSubscription, EngineError> {
let start = decode_postgres_event_cursor(&cursor).map_err(|_| EngineError::Validation {
kind: ValidationKind::InvalidInput,
detail: "invalid event_id cursor for sqlite.subscribe_completion".into(),
})?;
let last_seen: i64 = match start {
Some(v) => v,
None => sqlx::query_scalar(
"SELECT COALESCE(MAX(event_id), 0) FROM ff_completion_event \
WHERE partition_key = ?1",
)
.bind(PARTITION_KEY)
.fetch_one(&pool)
.await
.map_err(|_| EngineError::Unavailable {
op: "sqlite.subscribe_completion",
})?,
};
let stream = outbox_cursor::spawn(OutboxCursorConfig {
pool,
select_sql: SELECT_COMPLETION_EVENTS_SQL,
partition_key: PARTITION_KEY,
cursor: last_seen,
batch_size: REPLAY_BATCH,
wakeup,
decoder: make_decoder(filter),
row_event_id: extract_event_id,
});
Ok(Box::pin(TypedStream { inner: stream }))
}
struct TypedStream {
inner: OutboxCursorStream<CompletionEvent>,
}
impl Stream for TypedStream {
type Item = Result<CompletionEvent, EngineError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.inner).poll_next(cx)
}
}
fn extract_event_id(row: &sqlx::sqlite::SqliteRow) -> Result<i64, EngineError> {
row.try_get("event_id")
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("ff_completion_event.event_id: {e}"),
})
}
fn make_decoder(filter: ScannerFilter) -> RowDecoder<CompletionEvent> {
Box::new(move |row| decode_row(row, &filter))
}
fn decode_row(
row: &sqlx::sqlite::SqliteRow,
filter: &ScannerFilter,
) -> Result<Option<CompletionEvent>, EngineError> {
let event_id: i64 = row
.try_get("event_id")
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("event_id: {e}"),
})?;
let namespace: Option<String> = row
.try_get::<Option<String>, _>("namespace")
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("namespace: {e}"),
})?;
let instance_tag: Option<String> = row
.try_get::<Option<String>, _>("instance_tag")
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("instance_tag: {e}"),
})?;
if !passes_filter(filter, namespace.as_deref(), instance_tag.as_deref()) {
return Ok(None);
}
let partition_key: i64 = row
.try_get("partition_key")
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("partition_key: {e}"),
})?;
let exec_uuid: Uuid = row
.try_get("execution_id")
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("execution_id: {e}"),
})?;
let execution_id = ExecutionId::parse(&format!("{{fp:{partition_key}}}:{exec_uuid}"))
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("ff_completion_event.execution_id: {e}"),
})?;
let outcome: String = row.try_get("outcome").map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("outcome: {e}"),
})?;
let occurred_at_ms: i64 = row
.try_get("occurred_at_ms")
.map_err(|e| EngineError::Validation {
kind: ValidationKind::Corruption,
detail: format!("occurred_at_ms: {e}"),
})?;
let cursor = encode_postgres_event_cursor(event_id);
Ok(Some(CompletionEvent::new(
cursor,
execution_id,
CompletionOutcome::from_wire(&outcome),
TimestampMs::from_millis(occurred_at_ms),
)))
}
pub(crate) fn passes_filter(
filter: &ScannerFilter,
row_namespace: Option<&str>,
row_instance_tag: Option<&str>,
) -> bool {
if let Some(ref want_ns) = filter.namespace {
match row_namespace {
Some(have) if have == want_ns.as_str() => {}
_ => return false,
}
}
if let Some((_, ref want_value)) = filter.instance_tag {
match row_instance_tag {
Some(have) if have == want_value.as_str() => {}
_ => return false,
}
}
true
}