pub struct Sets { /* private fields */ }Expand description
Every Redis set command, with the key as the first argument.
Keys and members are byte strings the way Redis’s are, so anything that is bytes will do.
let db = yo::open(yo::MEMORY)?;
let sets = db.sets();
sets.add_many("online", &["alice", "bob"])?;
assert!(sets.contains("online", "alice")?);
assert_eq!(sets.len_of("online")?, 2);Implementations§
Source§impl Sets
impl Sets
Sourcepub fn add(
&self,
key: impl AsRef<[u8]>,
member: impl AsRef<[u8]>,
) -> Result<bool>
pub fn add( &self, key: impl AsRef<[u8]>, member: impl AsRef<[u8]>, ) -> Result<bool>
Add one member, and say whether it was new. SADD.
The key is created by the first member that goes into it, so there is no step before this one.
§Errors
Code::WrongType when the key holds something that is not a set,
Code::Full for a member past the size limit, and Code::Invalid if
called from inside a callback that is already holding this database.
Sourcepub fn add_many<M: AsRef<[u8]>>(
&self,
key: impl AsRef<[u8]>,
members: &[M],
) -> Result<usize>
pub fn add_many<M: AsRef<[u8]>>( &self, key: impl AsRef<[u8]>, members: &[M], ) -> Result<usize>
Add several members, and say how many were new. SADD with a list.
One key lookup for the whole call rather than one per member, which is
the only reason to prefer it over calling Sets::add in a loop.
§Errors
As Sets::add. Nothing is added if any member is too long, because the
lengths are all checked before the first one goes in.
Sourcepub fn remove_many<M: AsRef<[u8]>>(
&self,
key: impl AsRef<[u8]>,
members: &[M],
) -> Result<usize>
pub fn remove_many<M: AsRef<[u8]>>( &self, key: impl AsRef<[u8]>, members: &[M], ) -> Result<usize>
Sourcepub fn contains_many<M: AsRef<[u8]>>(
&self,
key: impl AsRef<[u8]>,
members: &[M],
) -> Result<Vec<bool>>
pub fn contains_many<M: AsRef<[u8]>>( &self, key: impl AsRef<[u8]>, members: &[M], ) -> Result<Vec<bool>>
Sourcepub fn members(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<Vec<u8>>>>
pub fn members(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<Vec<u8>>>>
Every member, owned. SMEMBERS.
None for a key that is not there, which a caller who wants to tell that
apart from an empty answer can use. Sets::for_each is the same walk
without the allocations.
§Errors
As Sets::add.
Sourcepub fn for_each(
&self,
key: impl AsRef<[u8]>,
f: impl FnMut(Member<'_>),
) -> Result<bool>
pub fn for_each( &self, key: impl AsRef<[u8]>, f: impl FnMut(Member<'_>), ) -> Result<bool>
Hand every member to f where it lies, and say whether the key was
there.
Nothing is allocated and nothing is formatted. A set stored as integers
hands over Member::Int and the digits are only written if the closure
writes them.
let db = yo::open(yo::MEMORY)?;
let sets = db.sets();
sets.add_many("ids", &["1", "2", "3"])?;
let mut total = 0i64;
sets.for_each("ids", |m| {
if let yo::Member::Int(n) = m {
total += n;
}
})?;
assert_eq!(total, 6);§Errors
As Sets::add.
Sourcepub fn pick_n(&self, key: impl AsRef<[u8]>, count: i64) -> Result<Vec<Vec<u8>>>
pub fn pick_n(&self, key: impl AsRef<[u8]>, count: i64) -> Result<Vec<Vec<u8>>>
Draw count members and leave them in the set. SRANDMEMBER key count.
A positive count is distinct members, at most as many as the set holds.
A negative one is the with repeats form, which answers exactly that many
and can answer more members than the set has. That is one command with
two meanings in Redis and it stays one method here, because splitting it
would mean a caller holding a count from somewhere else has to branch on
its sign before choosing which method to call.
§Errors
As Sets::add.
Sourcepub fn move_member(
&self,
from: impl AsRef<[u8]>,
to: impl AsRef<[u8]>,
member: impl AsRef<[u8]>,
) -> Result<bool>
pub fn move_member( &self, from: impl AsRef<[u8]>, to: impl AsRef<[u8]>, member: impl AsRef<[u8]>, ) -> Result<bool>
Sourcepub fn intersect_len<K: AsRef<[u8]>>(
&self,
keys: &[K],
limit: usize,
) -> Result<usize>
pub fn intersect_len<K: AsRef<[u8]>>( &self, keys: &[K], limit: usize, ) -> Result<usize>
How big the intersection is, without building it. SINTERCARD.
A limit of zero means no limit. Any other limit stops the walk once it
has counted that many, which is what makes “do these two sets share at
least one member” cost one member and not the whole intersection.
§Errors
As Sets::add, for any of the keys.