Skip to main content

Sets

Struct Sets 

Source
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

Source

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.

Source

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.

Source

pub fn remove( &self, key: impl AsRef<[u8]>, member: impl AsRef<[u8]>, ) -> Result<bool>

Remove one member, and say whether it was there. SREM.

A set that loses its last member loses its key too, which is Redis’s rule and is why there is no such thing as an empty set in the keyspace.

§Errors

As Sets::add.

Source

pub fn remove_many<M: AsRef<[u8]>>( &self, key: impl AsRef<[u8]>, members: &[M], ) -> Result<usize>

Remove several members, and say how many were there. SREM with a list.

§Errors

As Sets::add.

Source

pub fn contains( &self, key: impl AsRef<[u8]>, member: impl AsRef<[u8]>, ) -> Result<bool>

Whether a member is in the set. SISMEMBER.

False for a key that is not there, which is the same answer as an empty set and is deliberate: a set nobody has added to and a set somebody emptied are the same set.

§Errors

As Sets::add.

Source

pub fn contains_many<M: AsRef<[u8]>>( &self, key: impl AsRef<[u8]>, members: &[M], ) -> Result<Vec<bool>>

Whether each of several members is in the set, in the order asked. SMISMEMBER.

§Errors

As Sets::add.

Source

pub fn len_of(&self, key: impl AsRef<[u8]>) -> Result<usize>

How many members the set holds, which is zero for a key that is not there. SCARD.

§Errors

As Sets::add.

Source

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.

Source

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.

Source

pub fn pop(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<u8>>>

Take one member out at random and hand it back. SPOP.

None for a key that is not there. The key goes when the last member does.

§Errors

As Sets::add.

Source

pub fn pop_n(&self, key: impl AsRef<[u8]>, count: usize) -> Result<Vec<Vec<u8>>>

Take up to count members out at random. SPOP key count.

The members are distinct, and fewer than count come back when the set holds fewer than that.

§Errors

As Sets::add.

Source

pub fn pick(&self, key: impl AsRef<[u8]>) -> Result<Option<Vec<u8>>>

Draw one member at random and leave it in the set. SRANDMEMBER.

§Errors

As Sets::add.

Source

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.

Source

pub fn move_member( &self, from: impl AsRef<[u8]>, to: impl AsRef<[u8]>, member: impl AsRef<[u8]>, ) -> Result<bool>

Move one member from one set to another, and say whether it moved. SMOVE.

False when the member was not in from, in which case to is untouched.

§Errors

As Sets::add, for either key.

Source

pub fn intersect<K: AsRef<[u8]>>(&self, keys: &[K]) -> Result<Vec<Vec<u8>>>

Everything in all of the sets. SINTER.

A key that is not there is an empty set, and an empty set anywhere empties the intersection.

§Errors

As Sets::add, for any of the keys.

Source

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.

Source

pub fn union<K: AsRef<[u8]>>(&self, keys: &[K]) -> Result<Vec<Vec<u8>>>

Everything in any of the sets. SUNION.

A key that is not there contributes nothing and is dropped, which is the opposite of what it does to an intersection and is right for the same reason: an empty set adds no members and removes none.

§Errors

As Sets::add, for any of the keys.

Source

pub fn difference<K: AsRef<[u8]>>(&self, keys: &[K]) -> Result<Vec<Vec<u8>>>

Everything in the first set and in none of the others. SDIFF.

§Errors

As Sets::add, for any of the keys.

Source

pub fn intersect_into<K: AsRef<[u8]>>( &self, destination: impl AsRef<[u8]>, keys: &[K], ) -> Result<usize>

Store the intersection under destination and say how big it is. SINTERSTORE.

An empty result removes destination rather than leaving an empty set there, because there is no such thing as an empty set in the keyspace.

§Errors

As Sets::add, for any of the keys.

Source

pub fn union_into<K: AsRef<[u8]>>( &self, destination: impl AsRef<[u8]>, keys: &[K], ) -> Result<usize>

Store the union under destination and say how big it is. SUNIONSTORE.

§Errors

As Sets::intersect_into.

Source

pub fn difference_into<K: AsRef<[u8]>>( &self, destination: impl AsRef<[u8]>, keys: &[K], ) -> Result<usize>

Store the difference under destination and say how big it is. SDIFFSTORE.

§Errors

As Sets::intersect_into.

Trait Implementations§

Source§

impl Clone for Sets

Source§

fn clone(&self) -> Sets

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sets

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Sets

§

impl !Send for Sets

§

impl !Sync for Sets

§

impl !UnwindSafe for Sets

§

impl Freeze for Sets

§

impl Unpin for Sets

§

impl UnsafeUnpin for Sets

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.