class Redstruct::Set
Mapping between Redis and Ruby sets. There is no caching mechanism in play, so most methods actually do access the underlying redis connection. Also, keep in mind Redis converts all values strings on the DB side
Public Instance Methods
Adds the given items to the set @param [Array<#to_s>] items the items to add to the set @return [Boolean, Integer] when only one item, returns true or false on insertion, otherwise the number of items added
# File lib/redstruct/set.rb, line 47 def add(*items) return self.connection.sadd(@key, items) end
Clears the set by simply removing the key from the DB @see Redstruct::Struct#delete
# File lib/redstruct/set.rb, line 15 def clear delete end
Checks if the set contains this particular item @param [#to_s] item the item to check for @return [Boolean] true if the set contains the item, false otherwise
# File lib/redstruct/set.rb, line 39 def contain?(item) return coerce_bool(self.connection.sismember(@key, item)) end
Computes the difference of the two sets and stores the result in `dest`. If no destination provided, computes the results in memory. @param [Redstruct::Set] other set the set to subtract @param [Redstruct::Set, String] dest if nil, results are computed in memory. if a string, a new Redstruct::Set
is constructed with the string as the key, and results are stored there. if already a Redstruct::Set
, results are stored there. @return [::Set, Integer] if dest was provided, returns the number of elements in the destination; otherwise a standard Ruby set containing the difference
# File lib/redstruct/set.rb, line 77 def difference(other, dest: nil) destination = coerce_destination(dest) results = if destination.nil? ::Set.new(self.connection.sdiff(@key, other.key)) else self.connection.sdiffstore(destination.key, @key, other.key) end return results end
Checks if the set is empty by checking if the key actually exists on the underlying redis db @see Redstruct::Struct#exists?
@return [Boolean] true if it is empty, false otherwise
# File lib/redstruct/set.rb, line 32 def empty? return !exists? end
Computes the interesection of the two sets and stores the result in `dest`. If no destination provided, computes the results in memory. @param [Redstruct::Set] other set the set to intersect @param [Redstruct::Set, String] dest if nil, results are computed in memory. if a string, a new Redstruct::Set
is constructed with the string as the key, and results are stored there. if already a Redstruct::Set
, results are stored there. @return [::Set, Integer] if dest was provided, returns the number of elements in the destination; otherwise a standard Ruby set containing the intersection
# File lib/redstruct/set.rb, line 95 def intersection(other, dest: nil) destination = coerce_destination(dest) results = if destination.nil? ::Set.new(self.connection.sinter(@key, other.key)) else self.connection.sinterstore(destination.key, @key, other.key) end return results end
Pops and returns an item from the set. NOTE: Since this is a redis set, keep in mind that popping the last element of the set effectively deletes the set @return [String] popped item
# File lib/redstruct/set.rb, line 55 def pop return self.connection.spop(@key) end
Returns random items from the set @param [Integer] count the number of items to return @return [String, Set] if count is one, then return the item; otherwise returns a set
# File lib/redstruct/set.rb, line 22 def random(count: 1) list = self.connection.srandmember(@key, count) return nil if list.nil? return count == 1 ? list[0] : ::Set.new(list) end
Removes the given items from the set. @param [Array<#to_s>] items the items to remove from the set @return [Boolean, Integer] when only one item, returns true or false on deletion, otherwise the number of items removed
# File lib/redstruct/set.rb, line 62 def remove(*items) return self.connection.srem(@key, items) end
@return [Integer] the number of items in the set
# File lib/redstruct/set.rb, line 67 def size return self.connection.scard(@key).to_i end
Returns an array representation of the set. Ordering is random and defined by redis NOTE: If the set is particularly large, consider using each
@return [Array<String>] an array of all items contained in the set
# File lib/redstruct/set.rb, line 134 def to_a return self.connection.smembers(@key) end
Use redis-rb sscan_each method to iterate over particular keys @return [Enumerator] base enumerator to iterate of the namespaced keys
# File lib/redstruct/set.rb, line 127 def to_enum(match: '*', count: 10) return self.connection.sscan_each(@key, match: match, count: count) end
Loads all members of the set and converts them to a Ruby set. NOTE: If the set is particularly large, consider using each
@return [::Set] ruby set of all items stored on redis for this set
# File lib/redstruct/set.rb, line 141 def to_set return ::Set.new(to_a) end
Computes the union of the two sets and stores the result in `dest`. If no destination provided, computes the results in memory. @param [Redstruct::Set] other set the set to add @param [Redstruct::Set, String] dest if nil, results are computed in memory. if a string, a new Redstruct::Set
is
constructed with the string as the key, and results are stored there. if already a Redstruct::Set, results are stored there.
@return [::Set, Integer] if dest was provided, returns the number of elements in the destination; otherwise a standard Ruby set containing the union
# File lib/redstruct/set.rb, line 113 def union(other, dest: nil) destination = coerce_destination(dest) results = if destination.nil? ::Set.new(self.connection.sunion(@key, other.key)) else self.connection.sunionstore(destination.key, @key, other.key) end return results end
Private Instance Methods
# File lib/redstruct/set.rb, line 145 def coerce_destination(dest) case dest when ::String @factory.set(dest) when self.class dest end end