use sqlx::SqliteConnection;
use crate::store::dependencies::apply as dep_apply;
use crate::store::jobs::apply as jobs_apply;
use crate::store::kv;
use crate::store::tasks::apply as task_apply;
use crate::store::{Command, Reply};
pub(crate) async fn run(
cmd: Command,
conn: &mut SqliteConnection,
now: &str,
replicated: bool,
) -> anyhow::Result<Reply> {
if replicated {
cmd.check_replicable()?;
}
Ok(match cmd {
Command::KvSet { key, value } => {
kv::set(conn, now, &key, &value).await?;
Reply::Done
}
Command::KvIncrement { key } => Reply::Count(kv::increment(conn, now, &key).await?),
Command::KvInsertIfAbsent { key, value } => {
Reply::Value(kv::insert_if_absent(conn, now, &key, &value).await?)
}
Command::KvDelete { key } => {
kv::delete(conn, &key).await?;
Reply::Done
}
cmd @ (Command::MoveRepo { .. }
| Command::DepReplace { .. }
| Command::DepLinkKnown { .. }
| Command::DepBlockClaim { .. }) => dep_apply::run(cmd, conn, now).await?,
Command::JobStateUpsert(state) => {
jobs_apply::upsert_job_state(conn, &state).await?;
Reply::Done
}
Command::ChannelSubscribe {
channel,
thread_id,
repo,
topic_id,
} => {
jobs_apply::subscribe(conn, &channel, &thread_id, &repo, &topic_id).await?;
Reply::Done
}
Command::ChannelUnsubscribe {
channel,
thread_id,
repo,
} => {
jobs_apply::unsubscribe(conn, &channel, &thread_id, &repo).await?;
Reply::Done
}
task => task_apply::run(task, conn, now, replicated).await?,
})
}