use serde::Serialize;
use std::sync::Arc;
use super::call::Asked;
use super::engine::Jobs;
use super::queue::{Change, Queue};
use super::read::Row;
use super::read::named::Which;
use super::repeat::Asked as RepeatAsked;
use super::state::Kind;
use crate::replication::{In, Out};
use crate::{Error, ErrorKind, Result, Store};
pub(crate) fn make(store: &Store) -> Result<()> {
super::engine::Jobs::of(store).map(drop)
}
pub(crate) fn make_in(home: &crate::engine::Home) -> Result<()> {
Jobs::at(home).map(drop)
}
pub(crate) use super::engine::FILE;
pub(crate) fn pass_on_write<V: Serialize>(
store: &Store,
(name, database): (&str, Option<&str>),
change: Change,
asked: &Asked,
value: &V,
) -> Result<bool> {
let json = serde_json::to_vec(value).map_err(|error| Error::invalid(format!("a job that is no JSON: {error}")))?;
let mut out = head(
(name, database),
match change {
Change::Add => 0,
Change::Set => 1,
Change::Update => 2,
},
);
asked.write(&mut out);
out.bytes(&json);
answered(store, out)
}
pub(crate) fn pass_on_cancel(store: &Store, queue: (&str, Option<&str>), which: &Which) -> Result<bool> {
let (call, named) = match which {
Which::Id(id) => (3, id.clone()),
Which::Row(row) => (6, row.to_string()),
};
let mut out = head(queue, call);
out.text(&named);
answered(store, out)
}
pub(crate) fn pass_on_schedule(store: &Store, name: &str, repeat: &RepeatAsked) -> Result<()> {
let mut out = head((name, None), 5);
repeat.write(&mut out);
answered(store, out).map(drop)
}
pub(crate) fn pass_on_open(store: &Store, queue: (&str, Option<&str>)) -> Result<()> {
answered(store, head(queue, 4)).map(drop)
}
fn head((name, database): (&str, Option<&str>), call: u8) -> Out {
let mut out = Out(vec![crate::replication::JOBS]);
out.text(name).maybe_text(database).byte(call);
out
}
fn answered(store: &Store, out: Out) -> Result<bool> {
let mut head = In { bytes: &out.0, at: 1 };
let (_, database) = (head.text()?, head.maybe_text()?);
let file = database.map_or_else(|| FILE.to_owned(), |database| crate::replication::database_file(&database));
let answer = crate::replication::pass_on(store, &file, out.0)?;
In { bytes: &answer, at: 0 }.byte().map(|changed| changed == 1)
}
pub(crate) fn run(store: &Store, lenders: crate::replication::Lenders<'_>, bytes: &[u8]) -> Result<(Vec<u8>, String)> {
let mut at = In { bytes, at: 0 };
let (name, database, call) = (at.text()?, at.maybe_text()?, at.byte()?);
let (jobs, file) = match database {
Some(database) => (Jobs::at(&lenders(&database)?)?, crate::replication::database_file(&database)),
None => (Jobs::of(store)?, FILE.to_owned()),
};
let kind = if call == 5 { Kind::Schedule } else { Kind::Queue };
let state = jobs.queue_as_open(&name, kind)?;
let queue: Queue<serde_json::Value> = Queue::of_state(Arc::clone(&jobs), state);
let changed = match call {
0..=2 => {
let asked = Asked::read(&mut at)?;
let value: serde_json::Value = serde_json::from_slice(at.bytes()?).map_err(|_| corrupt("a job's JSON"))?;
let change = [Change::Add, Change::Set, Change::Update][usize::from(call)];
queue.write_asked(change, asked, &value)?
}
3 => queue.cancel(at.text()?)?,
6 => {
let row = Row::parse(&at.text()?).map_err(|_| corrupt("a job's row"))?;
queue.cancel_in(&Which::Row(row), None)?
}
4 => true,
5 => {
let repeat = RepeatAsked::read(at.byte()?, &mut at)?;
super::schedule::keep_repeat(&queue, &repeat.of(&name)?)?;
true
}
_ => return Err(corrupt("a call")),
};
if !at.done() {
return Err(corrupt("a call that holds more than its fields"));
}
Ok((vec![u8::from(changed)], file))
}
pub(crate) fn corrupt(what: &str) -> Error {
Error::new(ErrorKind::Corrupt, format!("a jobs write passed on: {what}"))
}