Class: Redstruct::Set

Inherits:
Struct show all
Includes:
Utils::Iterable
Defined in:
lib/redstruct/set.rb

Overview

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

Instance Attribute Summary

Attributes inherited from Struct

#key

Attributes inherited from Factory::Object

#factory

Instance Method Summary collapse

Methods included from Utils::Iterable

bound_enumerator, #each

Methods inherited from Struct

#delete, #dump, #exists?, #expire, #expire_at, #initialize, #inspectable_attributes, #persist, #restore, #ttl, #type

Methods included from Utils::Coercion

coerce_array, coerce_bool

Methods inherited from Factory::Object

#connection, #initialize, #inspectable_attributes

Methods included from Utils::Inspectable

#inspect, #inspectable_attributes

Constructor Details

This class inherits a constructor from Redstruct::Struct

Instance Method Details

#add(*items) ⇒ Boolean, Integer Also known as: <<

Adds the given items to the set

Parameters:

  • items (Array<#to_s>)

    the items to add to the set

Returns:

  • (Boolean, Integer)

    when only one item, returns true or false on insertion, otherwise the number of items added



47
48
49
# File 'lib/redstruct/set.rb', line 47

def add(*items)
  return self.connection.sadd(@key, items)
end

#clearObject

Clears the set by simply removing the key from the DB



15
16
17
# File 'lib/redstruct/set.rb', line 15

def clear
  delete
end

#contain?(item) ⇒ Boolean Also known as: include?

Checks if the set contains this particular item

Parameters:

  • item (#to_s)

    the item to check for

Returns:

  • (Boolean)

    true if the set contains the item, false otherwise



39
40
41
# File 'lib/redstruct/set.rb', line 39

def contain?(item)
  return coerce_bool(self.connection.sismember(@key, item))
end

#difference(other, dest: nil) ⇒ ::Set, Integer Also known as: -

Computes the difference of the two sets and stores the result in ‘dest`. If no destination provided, computes the results in memory. constructed with the string as the key, and results are stored there. if already a Redstruct::Set, results are stored there.

Parameters:

  • other (Redstruct::Set)

    set the set to subtract

  • dest (Redstruct::Set, String) (defaults to: nil)

    if nil, results are computed in memory. if a string, a new Redstruct::Set is

Returns:

  • (::Set, Integer)

    if dest was provided, returns the number of elements in the destination; otherwise a standard Ruby set containing the difference



77
78
79
80
81
82
83
84
85
86
# 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

#empty?Boolean

Checks if the set is empty by checking if the key actually exists on the underlying redis db

Returns:

  • (Boolean)

    true if it is empty, false otherwise

See Also:



32
33
34
# File 'lib/redstruct/set.rb', line 32

def empty?
  return !exists?
end

#intersection(other, dest: nil) ⇒ ::Set, Integer Also known as: |

Computes the interesection of the two sets and stores the result in ‘dest`. If no destination provided, computes the results in memory. constructed with the string as the key, and results are stored there. if already a Redstruct::Set, results are stored there.

Parameters:

  • other (Redstruct::Set)

    set the set to intersect

  • dest (Redstruct::Set, String) (defaults to: nil)

    if nil, results are computed in memory. if a string, a new Redstruct::Set is

Returns:

  • (::Set, Integer)

    if dest was provided, returns the number of elements in the destination; otherwise a standard Ruby set containing the intersection



95
96
97
98
99
100
101
102
103
104
# 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

#popString

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

Returns:



55
56
57
# File 'lib/redstruct/set.rb', line 55

def pop
  return self.connection.spop(@key)
end

#random(count: 1) ⇒ String, Set

Returns random items from the set

Parameters:

  • count (Integer) (defaults to: 1)

    the number of items to return

Returns:

  • (String, Set)

    if count is one, then return the item; otherwise returns a set



22
23
24
25
26
27
# 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

#remove(*items) ⇒ Boolean, Integer

Removes the given items from the set.

Parameters:

  • items (Array<#to_s>)

    the items to remove from the set

Returns:

  • (Boolean, Integer)

    when only one item, returns true or false on deletion, otherwise the number of items removed



62
63
64
# File 'lib/redstruct/set.rb', line 62

def remove(*items)
  return self.connection.srem(@key, items)
end

#sizeInteger

Returns the number of items in the set.

Returns:

  • (Integer)

    the number of items in the set



67
68
69
# File 'lib/redstruct/set.rb', line 67

def size
  return self.connection.scard(@key).to_i
end

#to_aArray<String>

Returns an array representation of the set. Ordering is random and defined by redis NOTE: If the set is particularly large, consider using #each

Returns:

  • (Array<String>)

    an array of all items contained in the set



134
135
136
# File 'lib/redstruct/set.rb', line 134

def to_a
  return self.connection.smembers(@key)
end

#to_enum(match: '*', count: 10) ⇒ Enumerator

Use redis-rb sscan_each method to iterate over particular keys

Returns:

  • (Enumerator)

    base enumerator to iterate of the namespaced keys



127
128
129
# File 'lib/redstruct/set.rb', line 127

def to_enum(match: '*', count: 10)
  return self.connection.sscan_each(@key, match: match, count: count)
end

#to_set::Set

Loads all members of the set and converts them to a Ruby set. NOTE: If the set is particularly large, consider using #each

Returns:

  • (::Set)

    ruby set of all items stored on redis for this set



141
142
143
# File 'lib/redstruct/set.rb', line 141

def to_set
  return ::Set.new(to_a)
end

#union(other, dest: nil) ⇒ ::Set, Integer Also known as: +

Computes the union of the two sets and stores the result in ‘dest`. If no destination provided, computes the results in memory.

Parameters:

  • other (Redstruct::Set)

    set the set to add

  • dest (Redstruct::Set, String) (defaults to: nil)

    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.

Returns:

  • (::Set, Integer)

    if dest was provided, returns the number of elements in the destination; otherwise a standard Ruby set containing the union



113
114
115
116
117
118
119
120
121
122
# 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