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



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



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.



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



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.



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



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



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.



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

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

#sizeInteger



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



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



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



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.



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