use std::fmt;
use std::time::Duration;
#[cfg(doc)]
use crate::entry::LostLease;
use crate::entry::{AutoClaim, Entry, EntryId, GroupBacklog, Pending, Renewal};
use crate::error::Result;
pub trait Streams: Send + Sync + fmt::Debug {
fn xadd(&self, stream: &str, fields: &[(&[u8], &[u8])]) -> Result<EntryId>;
fn xlen(&self, stream: &str) -> Result<u64>;
fn xgroup_create(&self, stream: &str, group: &str) -> Result<bool>;
fn xreadgroup(
&self,
stream: &str,
group: &str,
consumer: &str,
count: usize,
block: Option<Duration>,
) -> Result<Vec<Entry>>;
fn xautoclaim(
&self,
stream: &str,
group: &str,
consumer: &str,
min_idle: Duration,
start: EntryId,
count: usize,
) -> Result<AutoClaim>;
fn xclaim_justid(
&self,
stream: &str,
group: &str,
consumer: &str,
ids: &[EntryId],
idle: Option<Duration>,
) -> Result<Vec<EntryId>>;
fn xpending_entry(&self, stream: &str, group: &str, id: EntryId) -> Result<Option<Pending>>;
fn xpending_consumer(&self, stream: &str, group: &str, consumer: &str, count: usize) -> Result<Vec<Pending>>;
fn xack(&self, stream: &str, group: &str, ids: &[EntryId]) -> Result<u64>;
fn xdel(&self, stream: &str, ids: &[EntryId]) -> Result<u64>;
fn xack_xdel(&self, stream: &str, group: &str, ids: &[EntryId]) -> Result<()> {
self.xack(stream, group, ids)?;
self.xdel(stream, ids)?;
Ok(())
}
fn xgroup_delconsumer(&self, stream: &str, group: &str, consumer: &str) -> Result<u64>;
fn renew_lease(&self, stream: &str, group: &str, consumer: &str, id: EntryId) -> Result<Renewal>;
fn release_lease(&self, stream: &str, group: &str, consumer: &str, id: EntryId, idle: Duration) -> Result<bool>;
fn xread(&self, stream: &str, after: EntryId, count: usize, block: Option<Duration>) -> Result<Vec<Entry>> {
let _ = (stream, after, count, block);
Err(unsupported("XREAD"))
}
fn expire(&self, key: &str, ttl: Duration) -> Result<bool> {
let _ = (key, ttl);
Err(unsupported("EXPIRE"))
}
fn del(&self, keys: &[&str]) -> Result<u64> {
let _ = keys;
Err(unsupported("DEL"))
}
fn xadd_expire(&self, stream: &str, fields: &[(&[u8], &[u8])], ttl: Duration) -> Result<EntryId> {
let id = self.xadd(stream, fields)?;
self.expire(stream, ttl)?;
Ok(id)
}
fn group_backlog(&self, stream: &str, group: &str) -> Result<Option<GroupBacklog>> {
let _ = (stream, group);
Ok(None)
}
}
fn unsupported(command: &str) -> crate::Error {
crate::Error::Redis(format!("ERR this `Streams` implementation doesn't send {command}"))
}
impl<S: Streams + ?Sized> Streams for std::sync::Arc<S> {
fn xadd(&self, stream: &str, fields: &[(&[u8], &[u8])]) -> Result<EntryId> {
(**self).xadd(stream, fields)
}
fn xlen(&self, stream: &str) -> Result<u64> {
(**self).xlen(stream)
}
fn xgroup_create(&self, stream: &str, group: &str) -> Result<bool> {
(**self).xgroup_create(stream, group)
}
fn xreadgroup(
&self,
stream: &str,
group: &str,
consumer: &str,
count: usize,
block: Option<Duration>,
) -> Result<Vec<Entry>> {
(**self).xreadgroup(stream, group, consumer, count, block)
}
fn xautoclaim(
&self,
stream: &str,
group: &str,
consumer: &str,
min_idle: Duration,
start: EntryId,
count: usize,
) -> Result<AutoClaim> {
(**self).xautoclaim(stream, group, consumer, min_idle, start, count)
}
fn xclaim_justid(
&self,
stream: &str,
group: &str,
consumer: &str,
ids: &[EntryId],
idle: Option<Duration>,
) -> Result<Vec<EntryId>> {
(**self).xclaim_justid(stream, group, consumer, ids, idle)
}
fn xpending_entry(&self, stream: &str, group: &str, id: EntryId) -> Result<Option<Pending>> {
(**self).xpending_entry(stream, group, id)
}
fn xpending_consumer(&self, stream: &str, group: &str, consumer: &str, count: usize) -> Result<Vec<Pending>> {
(**self).xpending_consumer(stream, group, consumer, count)
}
fn xack(&self, stream: &str, group: &str, ids: &[EntryId]) -> Result<u64> {
(**self).xack(stream, group, ids)
}
fn xdel(&self, stream: &str, ids: &[EntryId]) -> Result<u64> {
(**self).xdel(stream, ids)
}
fn xack_xdel(&self, stream: &str, group: &str, ids: &[EntryId]) -> Result<()> {
(**self).xack_xdel(stream, group, ids)
}
fn xgroup_delconsumer(&self, stream: &str, group: &str, consumer: &str) -> Result<u64> {
(**self).xgroup_delconsumer(stream, group, consumer)
}
fn renew_lease(&self, stream: &str, group: &str, consumer: &str, id: EntryId) -> Result<Renewal> {
(**self).renew_lease(stream, group, consumer, id)
}
fn release_lease(&self, stream: &str, group: &str, consumer: &str, id: EntryId, idle: Duration) -> Result<bool> {
(**self).release_lease(stream, group, consumer, id, idle)
}
fn xread(&self, stream: &str, after: EntryId, count: usize, block: Option<Duration>) -> Result<Vec<Entry>> {
(**self).xread(stream, after, count, block)
}
fn expire(&self, key: &str, ttl: Duration) -> Result<bool> {
(**self).expire(key, ttl)
}
fn del(&self, keys: &[&str]) -> Result<u64> {
(**self).del(keys)
}
fn xadd_expire(&self, stream: &str, fields: &[(&[u8], &[u8])], ttl: Duration) -> Result<EntryId> {
(**self).xadd_expire(stream, fields, ttl)
}
fn group_backlog(&self, stream: &str, group: &str) -> Result<Option<GroupBacklog>> {
(**self).group_backlog(stream, group)
}
}
pub(crate) fn expire_seconds(ttl: Duration) -> u64 {
ttl.as_secs().saturating_add(u64::from(ttl.subsec_nanos() > 0))
}