use falkordb::{FalkorClientBuilder, FalkorResult};
use futures::{StreamExt, TryStreamExt};
#[tokio::main(flavor = "multi_thread")]
async fn main() -> FalkorResult<()> {
let client = FalkorClientBuilder::new_async()
.with_connection_info("falkor://127.0.0.1:6379".try_into()?)
.build()
.await?;
let mut graph = client.select_graph("async_stream_example");
let _ = graph.delete().await;
graph
.query("UNWIND range(1, 5) AS i CREATE (:Movie {year: 1990 + i})")
.execute()
.await?;
let years: Vec<i64> = graph
.query("MATCH (m:Movie) RETURN m.year AS year ORDER BY year")
.execute()
.await?
.data
.map(|row| row?.try_get::<i64>("year"))
.try_collect()
.await?;
println!("years: {years:?}");
let mut next_years: Vec<i64> = graph
.query("MATCH (m:Movie) RETURN m.year AS year")
.execute()
.await?
.data
.map(|row| {
let mut g = graph.clone();
async move {
let year: i64 = row?.try_get("year")?;
let mut r = g
.query(format!("RETURN {year} + 1 AS next"))
.execute()
.await?;
r.data
.try_next()
.await?
.expect("a row")
.try_get::<i64>("next")
}
})
.buffer_unordered(8)
.try_collect()
.await?;
next_years.sort_unstable();
println!("next years: {next_years:?}");
graph.delete().await?;
Ok(())
}