pub struct MemoryStreams { /* private fields */ }Expand description
The stream commands of Streams run in memory, with the semantics Redis 7 gives them, on a
Clock the test controls: entry ids, idle times and blocking reads follow its time, so lease
expiry, idle claims and delivery counts can be tested to the millisecond.
It implements exactly the commands the trait names, and keeps no trimming or persistence. A key
given an expiry with EXPIRE goes once the clock passes it, as Redis expires keys: every
command and inspection sees it gone. XAUTOCLAIM reports deleted entries the Redis 7 way. A
blocking read on a manual clock waits until an entry arrives or the test advances the clock
past its timeout.
The double is a cheap handle; clones share one set of streams.
use std::time::Duration;
use legit_streams::{Clock, EntryId, MemoryStreams, Streams};
let clock = Clock::manual(1_000);
let streams = MemoryStreams::with_clock(clock.clone());
streams.xgroup_create("hash_tasks", "hash_workers")?;
let id = streams.xadd("hash_tasks", &[(b"task_id".as_slice(), b"app_1".as_slice())])?;
streams.xreadgroup("hash_tasks", "hash_workers", "a", 1, None)?;
clock.advance(Duration::from_secs(300));
let claim = streams.xautoclaim("hash_tasks", "hash_workers", "b", Duration::from_secs(300), EntryId::ZERO, 1)?;
assert_eq!(claim.claimed[0].id, id);
assert_eq!(streams.xpending_entry("hash_tasks", "hash_workers", id)?.map(|p| p.deliveries), Some(2));Implementations§
Source§impl MemoryStreams
impl MemoryStreams
Sourcepub fn new() -> Self
pub fn new() -> Self
An empty double on the system clock.
use legit_streams::{MemoryStreams, Streams};
assert_eq!(MemoryStreams::new().xlen("hash_tasks")?, 0);Sourcepub fn with_clock(clock: Clock) -> Self
pub fn with_clock(clock: Clock) -> Self
An empty double on clock.
use legit_streams::{Clock, MemoryStreams};
let streams = MemoryStreams::with_clock(Clock::manual(0));
assert!(streams.clock().is_manual());Sourcepub fn clock(&self) -> &Clock
pub fn clock(&self) -> &Clock
The clock the double runs on.
use legit_streams::MemoryStreams;
assert!(!MemoryStreams::new().clock().is_manual());Sourcepub fn entries(&self, stream: &str) -> Vec<Entry>
pub fn entries(&self, stream: &str) -> Vec<Entry>
Every entry of a stream, oldest first, as XRANGE stream - + gives them.
use legit_streams::{MemoryStreams, Streams};
let streams = MemoryStreams::new();
streams.xadd("hash_tasks", &[(b"k".as_slice(), b"v".as_slice())])?;
assert_eq!(streams.entries("hash_tasks")[0].field("k"), Some(&b"v"[..]));Sourcepub fn pending(&self, stream: &str, group: &str) -> Vec<Pending>
pub fn pending(&self, stream: &str, group: &str) -> Vec<Pending>
A group’s whole pending list, oldest first, as XPENDING stream group - + <all> gives it;
empty when the group doesn’t exist.
use legit_streams::{MemoryStreams, Streams};
let streams = MemoryStreams::new();
streams.xgroup_create("hash_tasks", "hash_workers")?;
streams.xadd("hash_tasks", &[(b"k".as_slice(), b"v".as_slice())])?;
streams.xreadgroup("hash_tasks", "hash_workers", "c", 1, None)?;
assert_eq!(streams.pending("hash_tasks", "hash_workers")[0].consumer, "c");Sourcepub fn consumers(&self, stream: &str, group: &str) -> Vec<String>
pub fn consumers(&self, stream: &str, group: &str) -> Vec<String>
A group’s consumers, in name order; empty when the group doesn’t exist.
use legit_streams::{MemoryStreams, Streams};
let streams = MemoryStreams::new();
streams.xgroup_create("hash_tasks", "hash_workers")?;
streams.xreadgroup("hash_tasks", "hash_workers", "c", 1, None)?;
assert_eq!(streams.consumers("hash_tasks", "hash_workers"), ["c"]);Sourcepub fn ttl(&self, key: &str) -> Option<Duration>
pub fn ttl(&self, key: &str) -> Option<Duration>
How long until a key’s expiry, or None when the key doesn’t exist or has none, as PTTL
tells it.
use std::time::Duration;
use legit_streams::{Clock, MemoryStreams, Streams};
let clock = Clock::manual(0);
let streams = MemoryStreams::with_clock(clock.clone());
streams.xadd("results:app:hash:rpc:app_1", &[(b"k".as_slice(), b"v".as_slice())])?;
assert_eq!(streams.ttl("results:app:hash:rpc:app_1"), None);
streams.expire("results:app:hash:rpc:app_1", Duration::from_secs(60))?;
clock.advance(Duration::from_secs(15));
assert_eq!(streams.ttl("results:app:hash:rpc:app_1"), Some(Duration::from_secs(45)));Trait Implementations§
Source§impl Clone for MemoryStreams
impl Clone for MemoryStreams
Source§impl Debug for MemoryStreams
impl Debug for MemoryStreams
Source§impl Default for MemoryStreams
impl Default for MemoryStreams
Source§impl Streams for MemoryStreams
impl Streams for MemoryStreams
Source§fn group_backlog(
&self,
stream: &str,
group: &str,
) -> Result<Option<GroupBacklog>>
fn group_backlog( &self, stream: &str, group: &str, ) -> Result<Option<GroupBacklog>>
Exact, where a server may not be: the lag is always known, as the number of entries after the group’s last-delivered id. Redis 7 and newer give the same count until entries the group hasn’t read are deleted, and then answer nil.
Source§fn xadd(&self, stream: &str, fields: &[(&[u8], &[u8])]) -> Result<EntryId>
fn xadd(&self, stream: &str, fields: &[(&[u8], &[u8])]) -> Result<EntryId>
XADD stream * field value ...: append an entry and return its id. Read moreSource§fn xlen(&self, stream: &str) -> Result<u64>
fn xlen(&self, stream: &str) -> Result<u64>
XLEN stream: how many entries the stream holds, 0 when it doesn’t exist. Read moreSource§fn xgroup_create(&self, stream: &str, group: &str) -> Result<bool>
fn xgroup_create(&self, stream: &str, group: &str) -> Result<bool>
XGROUP CREATE stream group 0 MKSTREAM: create the group, reading from the start of the
stream, and the stream when it doesn’t exist. Returns false when the group already
existed (BUSYGROUP), which isn’t an error. Read moreSource§fn xreadgroup(
&self,
stream: &str,
group: &str,
consumer: &str,
count: usize,
block: Option<Duration>,
) -> Result<Vec<Entry>>
fn xreadgroup( &self, stream: &str, group: &str, consumer: &str, count: usize, block: Option<Duration>, ) -> Result<Vec<Entry>>
XREADGROUP GROUP group consumer COUNT count [BLOCK ms] STREAMS stream >: deliver up to
count entries no consumer of the group has seen, adding each to the group’s pending list
under consumer. With block, wait up to that long for one when there are none; None
returns at once. A block under a millisecond waits a millisecond: Redis reads BLOCK 0
as forever. Read moreSource§fn xautoclaim(
&self,
stream: &str,
group: &str,
consumer: &str,
min_idle: Duration,
start: EntryId,
count: usize,
) -> Result<AutoClaim>
fn xautoclaim( &self, stream: &str, group: &str, consumer: &str, min_idle: Duration, start: EntryId, count: usize, ) -> Result<AutoClaim>
XAUTOCLAIM stream group consumer min-idle start COUNT count: scan the pending list from
start and claim for consumer up to count entries idle at least min_idle, adding one
to each one’s delivery count. Read moreSource§fn xclaim_justid(
&self,
stream: &str,
group: &str,
consumer: &str,
ids: &[EntryId],
idle: Option<Duration>,
) -> Result<Vec<EntryId>>
fn xclaim_justid( &self, stream: &str, group: &str, consumer: &str, ids: &[EntryId], idle: Option<Duration>, ) -> Result<Vec<EntryId>>
XCLAIM stream group consumer 0 id ... [IDLE ms] JUSTID: give pending entries to
consumer without counting a delivery, and return the ids claimed. Without idle their
idle time restarts at 0, which renews a lease; with it, it’s set to idle, which can make
an entry claimable by another worker at once. Ids no longer pending are skipped. Read moreSource§fn xpending_entry(
&self,
stream: &str,
group: &str,
id: EntryId,
) -> Result<Option<Pending>>
fn xpending_entry( &self, stream: &str, group: &str, id: EntryId, ) -> Result<Option<Pending>>
XPENDING stream group id id 1: the pending list’s record of one entry, or None when
it isn’t pending. Read moreSource§fn xpending_consumer(
&self,
stream: &str,
group: &str,
consumer: &str,
count: usize,
) -> Result<Vec<Pending>>
fn xpending_consumer( &self, stream: &str, group: &str, consumer: &str, count: usize, ) -> Result<Vec<Pending>>
XPENDING stream group - + count consumer: up to count of the entries consumer owns,
oldest first. Read moreSource§fn xack(&self, stream: &str, group: &str, ids: &[EntryId]) -> Result<u64>
fn xack(&self, stream: &str, group: &str, ids: &[EntryId]) -> Result<u64>
XACK stream group id ...: drop entries from the group’s pending list, returning how many
were pending. Read moreSource§fn xdel(&self, stream: &str, ids: &[EntryId]) -> Result<u64>
fn xdel(&self, stream: &str, ids: &[EntryId]) -> Result<u64>
XDEL stream id ...: delete entries from the stream, returning how many existed. Read moreSource§fn xgroup_delconsumer(
&self,
stream: &str,
group: &str,
consumer: &str,
) -> Result<u64>
fn xgroup_delconsumer( &self, stream: &str, group: &str, consumer: &str, ) -> Result<u64>
XGROUP DELCONSUMER stream group consumer: remove a consumer, and with it any entries it
still owns; returns how many it owned. Read moreSource§fn renew_lease(
&self,
stream: &str,
group: &str,
consumer: &str,
id: EntryId,
) -> Result<Renewal>
fn renew_lease( &self, stream: &str, group: &str, consumer: &str, id: EntryId, ) -> Result<Renewal>
consumer’s lease on a pending entry, as one atomic step on the server: if, at the
instant it runs, the entry is pending in the group for consumer, its idle time becomes 0
and nothing else changes, neither its owner nor its delivery count; otherwise nothing
changes and the answer is Renewal::Lost. An entry still pending but deleted from the
stream is dropped from the pending list and answers LostLease::NotPending. Read moreSource§fn release_lease(
&self,
stream: &str,
group: &str,
consumer: &str,
id: EntryId,
idle: Duration,
) -> Result<bool>
fn release_lease( &self, stream: &str, group: &str, consumer: &str, id: EntryId, idle: Duration, ) -> Result<bool>
consumer’s lease on a pending entry, as one atomic step on the server: if the
entry is pending in the group for consumer, its idle time is set to idle, so a worker
that claims entries idle that long takes it at once; otherwise nothing changes. The owner
and the delivery count don’t change. Returns whether the entry was released. As with
Streams::renew_lease, a separate check and XCLAIM could take back an entry another
worker has just claimed. Read moreSource§fn xread(
&self,
stream: &str,
after: EntryId,
count: usize,
block: Option<Duration>,
) -> Result<Vec<Entry>>
fn xread( &self, stream: &str, after: EntryId, count: usize, block: Option<Duration>, ) -> Result<Vec<Entry>>
XREAD COUNT count [BLOCK ms] STREAMS stream after: up to count entries added after
after, without a consumer group, so nothing is delivered or pending. With block, wait
up to that long for one when there are none; None returns at once. A block under a
millisecond waits a millisecond, as for Streams::xreadgroup. A stream that doesn’t
exist reads as empty. count 0 means no limit. Read moreSource§fn expire(&self, key: &str, ttl: Duration) -> Result<bool>
fn expire(&self, key: &str, ttl: Duration) -> Result<bool>
EXPIRE key seconds: delete the key once ttl has passed, replacing any expiry it had.
The time is rounded up to whole seconds, so the key never goes sooner than asked; a ttl of
zero deletes it now, as Redis does. Returns whether the key existed. Read moreSource§fn del(&self, keys: &[&str]) -> Result<u64>
fn del(&self, keys: &[&str]) -> Result<u64>
DEL key ...: delete keys, returning how many existed. Read more