use rusqlite::{Connection, OptionalExtension, Params, params};
use super::values::Kept;
use crate::Result;
use crate::limits::Limit;
use crate::sqlite::sql_error;
pub(crate) const PARKED_BY: i64 = 1 << 60;
pub(crate) const PARKED_FROM: i64 = 1 << 59;
macro_rules! parked_from {
() => {
"576460752303423488"
};
}
pub(crate) fn due_of(next: i64) -> i64 {
if next >= PARKED_FROM { next - PARKED_BY } else { next }
}
const REGISTER_QUEUE: &str =
"insert into _tinystore_jobs_queues (name, kind) values (?1, ?2) on conflict (name) do nothing";
const QUEUE_NAMED: &str = "select id, kind, in_group from _tinystore_jobs_queues where name = ?1";
const COUNT_WAITING: &str = "select waiting from _tinystore_jobs_queues where id = ?1";
const KEEP_IN_GROUP: &str = "update _tinystore_jobs_queues set in_group = ?2 where id = ?1";
const UNPARK_JOBS: &str =
concat!("update _tinystore_jobs set next = next - ?2 where queue = ?1 and next >= ", parked_from!());
const UNPARK_KEYS: &str =
concat!("update _tinystore_jobs_keys set next = next - ?2 where queue = ?1 and next >= ", parked_from!());
const NEXT_ID: &str = "update _tinystore_jobs_meta set value = value + 1 where name = 'ids'";
const IDS: &str = "select value from _tinystore_jobs_meta where name = 'ids'";
const COUNT_DEAD_ATTEMPTS: &str = "update _tinystore_jobs set attempt = l.attempt from _tinystore_jobs_leases l
where _tinystore_jobs.queue = l.queue and _tinystore_jobs.next = l.next and _tinystore_jobs.id = l.id";
const DROP_DEAD_LEASES: &str = "delete from _tinystore_jobs_leases";
const COUNT_ENDED_ATTEMPT: &str = "update _tinystore_jobs set attempt = l.attempt from _tinystore_jobs_leases l
where l.id = ?1 and _tinystore_jobs.queue = l.queue and _tinystore_jobs.next = l.next and _tinystore_jobs.id = l.id";
const JOB_BY_KEY: &str = "select j.next, j.id, j.again, j.repeat, j.spill
from _tinystore_jobs_keys k join _tinystore_jobs j on j.queue = k.queue and j.next = k.next and j.id = k.id
where k.queue = ?1 and k.key = ?2";
const LEASE_HELD: &str = "select 1 from _tinystore_jobs_leases where id = ?1 and until > ?2";
const FAILED_KEY: &str = "select 1 from _tinystore_jobs_failed where queue = ?1 and key = ?2";
const DONE_KEY: &str = "select 1 from _tinystore_jobs_done where queue = ?1 and key = ?2 and until > ?3";
const INSERT_JOB: &str = "insert into _tinystore_jobs (queue, next, id, key, at, attempt, repeat, value, spill, grp)
values (?1, ?2, ?3, ?4, ?2, 0, ?5, ?6, ?7, ?8)";
const INSERT_SPILLED: &str = "insert into _tinystore_jobs_spilled (id, value) values (?1, ?2)";
const DELETE_SPILLED: &str = "delete from _tinystore_jobs_spilled where id = ?1";
const INSERT_KEY: &str = "insert into _tinystore_jobs_keys (queue, key, next, id) values (?1, ?2, ?3, ?4)
on conflict (queue, key) do update set next = excluded.next, id = excluded.id";
const MOVE_KEY: &str = "update _tinystore_jobs_keys set next = ?3 where queue = ?1 and key = ?2";
const DROP_KEY: &str = "delete from _tinystore_jobs_keys where queue = ?1 and key = ?2";
const RENEW_JOB: &str = "update _tinystore_jobs
set next = ?4, at = ?4, attempt = 0, again = null, error = null, value = ?5, spill = ?6,
repeat = coalesce(?7, repeat), grp = coalesce(?8, grp)
where queue = ?1 and next = ?2 and id = ?3";
const UPDATE_JOB: &str = "update _tinystore_jobs
set next = ?4, at = ?4, value = ?5, spill = ?6, repeat = coalesce(?7, repeat), grp = coalesce(?8, grp)
where queue = ?1 and next = ?2 and id = ?3";
const ASK_AGAIN: &str = "update _tinystore_jobs
set again = ?4, value = ?5, spill = ?6, repeat = coalesce(?7, repeat), grp = coalesce(?8, grp)
where queue = ?1 and next = ?2 and id = ?3";
const DROP_FAILED: &str = "delete from _tinystore_jobs_failed where queue = ?1 and key = ?2 returning spill, grp";
const CANCEL_JOB: &str = "delete from _tinystore_jobs where (queue, next, id) in (
select k.queue, k.next, k.id from _tinystore_jobs_keys k where k.queue = ?1 and k.key = ?2)
returning id, spill, next, grp";
const DROP_LEASE: &str = "delete from _tinystore_jobs_leases where id = ?1";
const DROP_STEPS: &str = "delete from _tinystore_jobs_steps where job = ?1";
const FIRST_PARKED: &str = concat!(
"select next, id, key from _tinystore_jobs indexed by _tinystore_jobs_parked
where queue = ?1 and grp = ?2 and next >= ",
parked_from!(),
" order by next, id limit 1"
);
const MOVE_ROW: &str = "update _tinystore_jobs set next = ?4 where queue = ?1 and next = ?2 and id = ?3";
const MOVE_LEASE: &str = "update _tinystore_jobs_leases set next = ?2 where id = ?1";
#[derive(Clone, Debug)]
pub(crate) struct Row {
pub(crate) next: i64,
pub(crate) id: i64,
pub(crate) again: Option<i64>,
pub(crate) repeat: Option<String>,
pub(crate) spill: Option<i64>,
}
#[derive(Debug)]
pub(crate) struct NewJob<'a> {
pub(crate) queue: i64,
pub(crate) id: i64,
pub(crate) key: Option<&'a str>,
pub(crate) at: i64,
pub(crate) repeat: Option<&'a str>,
pub(crate) group: Option<&'a str>,
pub(crate) value: &'a Kept,
}
#[derive(Debug)]
pub(crate) struct Change<'a> {
pub(crate) next: i64,
pub(crate) repeat: Option<&'a str>,
pub(crate) group: Option<&'a str>,
pub(crate) value: &'a Kept,
}
#[cfg(feature = "replication")]
pub(crate) fn find_queue(c: &Connection, name: &str) -> Result<Option<(i64, String)>> {
c.prepare_cached(QUEUE_NAMED)
.and_then(|mut select| select.query_row([name], |row| Ok((row.get(0)?, row.get(1)?))).optional())
.map_err(|error| sql_error("a queue's registration", error))
}
pub(crate) fn queue(c: &Connection, name: &str, kind: &str) -> Result<(i64, String, i64)> {
let named = || {
c.prepare_cached(QUEUE_NAMED)
.and_then(|mut select| {
select.query_row([name], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?))).optional()
})
.map_err(|error| sql_error("a queue's registration", error))
};
if let Some(found) = named()? {
return Ok(found);
}
execute(c, REGISTER_QUEUE, params![name, kind], "a queue's registration")?;
named()?.ok_or_else(|| crate::Error::internal("a queue registered and not found"))
}
pub(crate) fn keep_group_bound(c: &Connection, queue: i64, was: i64, is: i64) -> Result<()> {
if was == is {
return Ok(());
}
execute(c, UNPARK_JOBS, params![queue, PARKED_BY], "the jobs another group bound parked")?;
execute(c, UNPARK_KEYS, params![queue, PARKED_BY], "the jobs another group bound parked")?;
execute(c, KEEP_IN_GROUP, params![queue, is], "a queue's group bound").map(drop)
}
pub(crate) fn next_id(c: &Connection) -> Result<i64> {
execute(c, NEXT_ID, [], "the next job id")?;
c.prepare_cached(IDS)
.and_then(|mut select| select.query_row([], |row| row.get(0)))
.map_err(|error| sql_error("the next job id", error))
}
pub(crate) fn end_dead_leases(c: &Connection) -> Result<()> {
execute(c, COUNT_DEAD_ATTEMPTS, [], "the leases of a process that died")?;
execute(c, DROP_DEAD_LEASES, [], "the leases of a process that died").map(drop)
}
pub(crate) fn job_by_key(c: &Connection, queue: i64, key: &str) -> Result<Option<Row>> {
c.prepare_cached(JOB_BY_KEY)
.and_then(|mut select| {
select
.query_row(params![queue, key], |row| {
Ok(Row {
next: row.get(0)?,
id: row.get(1)?,
again: row.get(2)?,
repeat: row.get(3)?,
spill: row.get(4)?,
})
})
.optional()
})
.map_err(|error| sql_error("the job under an id", error))
}
pub(crate) fn lease_held(c: &Connection, id: i64, now: i64) -> Result<bool> {
exists(c, LEASE_HELD, params![id, now])
}
pub(crate) fn failed_key(c: &Connection, queue: i64, key: &str) -> Result<bool> {
exists(c, FAILED_KEY, params![queue, key])
}
pub(crate) fn done_key(c: &Connection, queue: i64, key: &str, now: i64) -> Result<bool> {
exists(c, DONE_KEY, params![queue, key, now])
}
pub(crate) fn check_room(c: &Connection, queue: i64, max_waiting: u64) -> Result<()> {
let waiting: i64 = c
.prepare_cached(COUNT_WAITING)
.and_then(|mut select| select.query_row([queue], |row| row.get(0)))
.map_err(|error| sql_error("the jobs a queue holds", error))?;
let waiting = u64::try_from(waiting).unwrap_or(0);
if waiting >= max_waiting {
let held = Limit { name: "waiting jobs", bound: max_waiting };
return Err(held.refuse(waiting + 1, format!("the queue holds {waiting} jobs, its max_waiting")));
}
Ok(())
}
pub(crate) fn insert(c: &Connection, job: &NewJob<'_>) -> Result<()> {
let spill = spill(c, job.id, job.value)?;
let params = params![job.queue, job.at, job.id, job.key, job.repeat, job.value.inline(), spill, job.group];
execute(c, INSERT_JOB, params, "a job's row")?;
if let Some(key) = job.key {
execute(c, INSERT_KEY, params![job.queue, key, job.at, job.id], "a job's id")?;
}
Ok(())
}
pub(crate) fn renew(c: &Connection, queue: i64, key: &str, there: &Row, change: &Change<'_>) -> Result<()> {
end_lease(c, there.id)?;
rewrite(c, queue, there, change, (RENEW_JOB, "a set's job"))?;
execute(c, DROP_STEPS, [there.id], "a set's job: the steps of its last run")?;
follow(c, queue, key, there.next, change.next)
}
pub(crate) fn update(c: &Connection, queue: i64, key: &str, there: &Row, change: &Change<'_>) -> Result<()> {
end_lease(c, there.id)?;
rewrite(c, queue, there, change, (UPDATE_JOB, "an update's job"))?;
execute(c, DROP_STEPS, [there.id], "an update's job: the steps kept for its old value")?;
follow(c, queue, key, there.next, change.next)
}
fn end_lease(c: &Connection, id: i64) -> Result<()> {
execute(c, COUNT_ENDED_ATTEMPT, [id], "the lease a changed job outlived")?;
execute(c, DROP_LEASE, [id], "the lease a changed job outlived").map(drop)
}
fn rewrite(
c: &Connection,
queue: i64,
there: &Row,
change: &Change<'_>,
(statement, what): (&str, &str),
) -> Result<()> {
drop_spill(c, there.spill)?;
let spill = spill(c, there.id, change.value)?;
let params =
params![queue, there.next, there.id, change.next, change.value.inline(), spill, change.repeat, change.group];
execute(c, statement, params, what).map(drop)
}
pub(crate) fn ask_again(
c: &Connection,
queue: i64,
there: &Row,
again: Option<i64>,
change: &Change<'_>,
) -> Result<()> {
drop_spill(c, there.spill)?;
let spill = spill(c, there.id, change.value)?;
let params = params![queue, there.next, there.id, again, change.value.inline(), spill, change.repeat, change.group];
execute(c, ASK_AGAIN, params, "a set of a running job").map(drop)
}
pub(crate) fn drop_failed(c: &Connection, queue: i64, key: &str) -> Result<Option<Option<String>>> {
let dropped: Option<(Option<i64>, Option<String>)> = c
.prepare_cached(DROP_FAILED)
.and_then(|mut delete| delete.query_row(params![queue, key], |row| Ok((row.get(0)?, row.get(1)?))).optional())
.map_err(|error| sql_error("a failed job", error))?;
let Some((spilled, group)) = dropped else {
return Ok(None);
};
drop_spill(c, spilled)?;
Ok(Some(group))
}
#[derive(Debug)]
pub(crate) struct Taken {
pub(crate) id: i64,
pub(crate) group: Option<String>,
pub(crate) placed: bool,
}
pub(crate) fn cancel(c: &Connection, queue: i64, key: &str) -> Result<Option<Taken>> {
let taken: Option<(i64, Option<i64>, i64, Option<String>)> = c
.prepare_cached(CANCEL_JOB)
.and_then(|mut delete| {
delete
.query_row(params![queue, key], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)))
.optional()
})
.map_err(|error| sql_error("a cancel", error))?;
let Some((id, spilled, next, group)) = taken else {
return Ok(None);
};
execute(c, DROP_LEASE, [id], "a cancel: the job's lease")?;
drop_spill(c, spilled)?;
execute(c, DROP_KEY, params![queue, key], "a cancel: the job's id")?;
Ok(Some(Taken { id, group, placed: next < PARKED_FROM }))
}
pub(crate) fn unpark_one(c: &Connection, queue: i64, group: &str) -> Result<()> {
let first: Option<(i64, i64, Option<String>)> = c
.prepare_cached(FIRST_PARKED)
.and_then(|mut select| {
select.query_row(params![queue, group], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?))).optional()
})
.map_err(|error| sql_error("a group's first parked job", error))?;
let Some((next, id, key)) = first else {
return Ok(());
};
move_to(c, queue, (next, id, key.as_deref()), next - PARKED_BY)
}
pub(crate) fn move_to(c: &Connection, queue: i64, (next, id, key): (i64, i64, Option<&str>), to: i64) -> Result<()> {
execute(c, MOVE_ROW, params![queue, next, id, to], "a job's move")?;
execute(c, MOVE_LEASE, params![id, to], "a job's move: its lease")?;
match key {
Some(key) => follow(c, queue, key, next, to),
None => Ok(()),
}
}
pub(crate) fn follow(c: &Connection, queue: i64, key: &str, from: i64, to: i64) -> Result<()> {
if from == to {
return Ok(());
}
execute(c, MOVE_KEY, params![queue, key, to], "a job's id").map(drop)
}
pub(crate) fn drop_spill(c: &Connection, spilled: Option<i64>) -> Result<()> {
match spilled {
Some(id) => execute(c, DELETE_SPILLED, [id], "a large value").map(drop),
None => Ok(()),
}
}
pub(crate) fn drop_steps(c: &Connection, job: i64) -> Result<()> {
execute(c, DROP_STEPS, [job], "the steps of a run that ended").map(drop)
}
fn spill(c: &Connection, id: i64, value: &Kept) -> Result<Option<i64>> {
match value {
Kept::Inline(_) => Ok(None),
Kept::Spilled(bytes) => {
execute(c, INSERT_SPILLED, params![id, bytes], "a large value")?;
Ok(Some(id))
}
}
}
pub(crate) use crate::sqlite::execute_cached as execute;
pub(crate) fn exists(c: &Connection, sql: &str, params: impl Params) -> Result<bool> {
c.prepare_cached(sql).and_then(|mut select| select.exists(params)).map_err(|error| sql_error("a read", error))
}