use std::{num::NonZeroUsize, ops::ControlFlow};
use uqa_storage::{
notifications::{NotificationQueueEntryRef, NotificationQueueScan},
read_control::StorageReadControl,
StorageBackendResult,
};
use super::control;
use super::{nonnegative_u64, registry_error, sqlite_integer, NotificationRegistryTransaction};
impl NotificationRegistryTransaction {
pub fn visit_entries_from(
&self,
from_sequence: u64,
max_entries: NonZeroUsize,
control: &StorageReadControl,
visit: &mut dyn FnMut(
NotificationQueueEntryRef<'_>,
) -> StorageBackendResult<ControlFlow<()>>,
) -> StorageBackendResult<NotificationQueueScan> {
let _operation =
control::operation_with(&self.connection, self.control.as_ref(), Some(control))?;
control.check()?;
let mut statement = self
.connection
.prepare_cached(
"SELECT sequence, process_id, channel, payload FROM queue_entries WHERE sequence >= ?1 ORDER BY sequence LIMIT ?2",
)
.map_err(|error| registry_error("prepare queue scan", &error))?;
let mut rows = statement
.query(rusqlite::params![
sqlite_integer(from_sequence, "scan sequence")?,
i64::try_from(max_entries.get()).unwrap_or(i64::MAX),
])
.map_err(|error| registry_error("scan queue entries", &error))?;
let mut progress = NotificationQueueScan {
visited: 0,
next_sequence: from_sequence,
exhausted: false,
};
while progress.visited < max_entries.get() {
control.check()?;
let Some(row) = rows
.next()
.map_err(|error| registry_error("read queue entry", &error))?
else {
control.check()?;
progress.exhausted = true;
return Ok(progress);
};
let entry = read_entry(row)?;
control.check()?;
progress.visited += 1;
let accepted = visit(entry)?;
control.check()?;
if accepted.is_break() {
return Ok(progress);
}
progress.next_sequence = entry.sequence + 1;
}
Ok(progress)
}
}
fn read_entry<'row>(
row: &'row rusqlite::Row<'_>,
) -> StorageBackendResult<NotificationQueueEntryRef<'row>> {
let sequence = row
.get::<_, i64>(0)
.map_err(|error| registry_error("read queue entry sequence", &error))?;
let process_id = row
.get(1)
.map_err(|error| registry_error("read queue entry process identifier", &error))?;
let channel = row
.get_ref(2)
.and_then(|value| value.as_str().map_err(rusqlite::Error::from))
.map_err(|error| registry_error("read queue entry channel", &error))?;
let payload = row
.get_ref(3)
.and_then(|value| value.as_str().map_err(rusqlite::Error::from))
.map_err(|error| registry_error("read queue entry payload", &error))?;
Ok(NotificationQueueEntryRef {
sequence: nonnegative_u64(sequence, "entry sequence")?,
process_id,
channel,
payload,
})
}