use async_trait::async_trait;
use chrono::{DateTime, Utc};
use neo4rs::{Graph, query};
use super::neo4j::{column, connect, count, detached, rows, run_schema, store_error};
use crate::error::Result;
use crate::watermark::CronWatermarkStore;
const ADVANCE: &str = "\
MERGE (w:AdkKernelCronWatermark {source: $source}) \
ON CREATE SET w.cursor_us = $cursor_us \
SET w.cursor_us = CASE WHEN w.cursor_us < $cursor_us THEN $cursor_us ELSE w.cursor_us END \
RETURN w.cursor_us AS cursor_us";
#[derive(Clone)]
pub struct Neo4jCronWatermarkStore {
graph: Graph,
}
impl std::fmt::Debug for Neo4jCronWatermarkStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Neo4jCronWatermarkStore").finish_non_exhaustive()
}
}
impl Neo4jCronWatermarkStore {
pub fn from_graph(graph: Graph) -> Self {
Self { graph }
}
pub async fn connect(uri: &str, user: &str, password: &str) -> Result<Self> {
Ok(Self::from_graph(connect(uri, user, password).await?))
}
pub fn graph(&self) -> &Graph {
&self.graph
}
pub async fn migrate(&self) -> Result<()> {
let graph = self.graph.clone();
detached(async move {
run_schema(
&graph,
&["CREATE CONSTRAINT adk_kernel_cron_watermark_source IF NOT EXISTS \
FOR (w:AdkKernelCronWatermark) REQUIRE w.source IS UNIQUE"],
)
.await
})
.await
}
}
fn instant(micros: i64) -> Result<DateTime<Utc>> {
DateTime::from_timestamp_micros(micros).ok_or_else(|| {
store_error("read a cron watermark", &format!("the cursor {micros} is out of range"))
})
}
#[async_trait]
impl CronWatermarkStore for Neo4jCronWatermarkStore {
async fn read(&self, source: &str) -> Result<Option<DateTime<Utc>>> {
let statement = query(
"MATCH (w:AdkKernelCronWatermark {source: $source}) RETURN w.cursor_us AS cursor_us",
)
.param("source", source);
let graph = self.graph.clone();
detached(async move {
let rows = rows(&graph, statement, "read a cron watermark").await?;
match rows.first() {
Some(row) => column::<Option<i64>>(row, "cursor_us")?.map(instant).transpose(),
None => Ok(None),
}
})
.await
}
async fn advance(&self, source: &str, cursor: DateTime<Utc>) -> Result<DateTime<Utc>> {
let statement =
query(ADVANCE).param("source", source).param("cursor_us", cursor.timestamp_micros());
let graph = self.graph.clone();
detached(async move {
let rows = rows(&graph, statement, "advance a cron watermark").await?;
let stored = match rows.first() {
Some(row) => column::<Option<i64>>(row, "cursor_us")?,
None => None,
};
stored.map(instant).transpose()?.ok_or_else(|| {
store_error("advance a cron watermark", &"the statement returned no cursor")
})
})
.await
}
async fn delete(&self, source: &str) -> Result<bool> {
let statement = query(
"MATCH (w:AdkKernelCronWatermark {source: $source}) DELETE w \
RETURN count(*) AS deleted",
)
.param("source", source);
let graph = self.graph.clone();
detached(async move {
Ok(count(&rows(&graph, statement, "delete a cron watermark").await?, "deleted")? > 0)
})
.await
}
}