Class: Familia::UnsortedSet

Inherits:
DataType show all
Defined in:
lib/familia/data_type/types/unsorted_set.rb

Overview

Familia::UnsortedSet

Instance Attribute Summary collapse

Attributes included from Settings

#current_key_version, #default_expiration, #delim, #encryption_keys, #encryption_personalization, #logical_database, #prefix, #suffix, #transaction_mode

Instance Method Summary collapse

Methods included from Features::Autoloader

autoload_files, included, normalize_to_config_name

Methods included from DataType::Serialization

#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value

Methods included from DataType::DatabaseCommands

#current_expiration, #delete!, #echo, #exists?, #expire, #expireat, #persist, #rename, #renamenx, #type

Methods included from DataType::Connection

#dbclient, #dbkey, #direct_access, #uri

Methods included from Connection::Behavior

#connect, #create_dbclient, #multi, #normalize_uri, #pipeline, #pipelined, #transaction, #uri=, #url, #url=

Methods included from Settings

#configure, #default_suffix, #pipelined_mode, #pipelined_mode=

Methods included from Base

add_feature, #as_json, #expired?, #expires?, find_feature, #generate_id, #to_json, #to_s, #ttl, #update_expiration, #uuid

Constructor Details

This class inherits a constructor from Familia::DataType

Instance Attribute Details

#features_enabledObject (readonly) Originally defined in module Features

Returns the value of attribute features_enabled.

#logical_database(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

#parentObject Originally defined in module DataType::ClassMethods

Returns the value of attribute parent.

#prefixObject Originally defined in module DataType::ClassMethods

Returns the value of attribute prefix.

#suffixObject Originally defined in module DataType::ClassMethods

Returns the value of attribute suffix.

#uri(val = nil) ⇒ Object Originally defined in module DataType::ClassMethods

Returns the value of attribute uri.

Instance Method Details

#<<(v) ⇒ Object



29
30
31
# File 'lib/familia/data_type/types/unsorted_set.rb', line 29

def <<(v)
  add v
end

#add(*values) ⇒ Object Also known as: add_element



22
23
24
25
26
# File 'lib/familia/data_type/types/unsorted_set.rb', line 22

def add *values
  values.flatten.compact.each { |v| dbclient.sadd? dbkey, serialize_value(v) }
  update_expiration
  self
end

#collectObject



53
54
55
# File 'lib/familia/data_type/types/unsorted_set.rb', line 53

def collect(&)
  members.collect(&)
end

#collectrawObject



69
70
71
# File 'lib/familia/data_type/types/unsorted_set.rb', line 69

def collectraw(&)
  membersraw.collect(&)
end

#difference(*other_sets) ⇒ Array Also known as: diff

Returns the difference of this set minus one or more other sets.

Parameters:

  • other_sets (Array<UnsortedSet, String>)

    Other sets (as UnsortedSet instances or raw keys)

Returns:

  • (Array)

    Deserialized members present in this set but not in any other sets



112
113
114
115
116
# File 'lib/familia/data_type/types/unsorted_set.rb', line 112

def difference(*other_sets)
  keys = extract_keys(other_sets)
  elements = dbclient.sdiff(dbkey, *keys)
  deserialize_values(*elements)
end

#diffstore(destination, *other_sets) ⇒ Integer Also known as: difference_store

Stores the difference of this set minus other sets into a destination key.

Parameters:

  • destination (UnsortedSet, String)

    Destination set (as UnsortedSet instance or raw key)

  • other_sets (Array<UnsortedSet, String>)

    Other sets to subtract

Returns:

  • (Integer)

    Number of elements in the resulting set



189
190
191
192
193
194
195
# File 'lib/familia/data_type/types/unsorted_set.rb', line 189

def diffstore(destination, *other_sets)
  dest_key = extract_key(destination)
  keys = extract_keys(other_sets)
  result = dbclient.sdiffstore(dest_key, dbkey, *keys)
  update_expiration
  result
end

#eachObject



45
46
47
# File 'lib/familia/data_type/types/unsorted_set.rb', line 45

def each(&)
  members.each(&)
end

#each_with_indexObject



49
50
51
# File 'lib/familia/data_type/types/unsorted_set.rb', line 49

def each_with_index(&)
  members.each_with_index(&)
end

#eachrawObject



61
62
63
# File 'lib/familia/data_type/types/unsorted_set.rb', line 61

def eachraw(&)
  membersraw.each(&)
end

#eachraw_with_indexObject



65
66
67
# File 'lib/familia/data_type/types/unsorted_set.rb', line 65

def eachraw_with_index(&)
  membersraw.each_with_index(&)
end

#element_countInteger Also known as: size, length, count

Returns the number of elements in the unsorted set

Returns:

  • (Integer)

    number of elements



11
12
13
# File 'lib/familia/data_type/types/unsorted_set.rb', line 11

def element_count
  dbclient.scard dbkey
end

#empty?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/familia/data_type/types/unsorted_set.rb', line 18

def empty?
  element_count.zero?
end

#intercard(*other_sets, limit: 0) ⇒ Integer Also known as: intersection_cardinality

Returns the cardinality of the intersection without retrieving members. More memory-efficient than intersection when only the count is needed.

Parameters:

  • other_sets (Array<UnsortedSet, String>)

    Other sets (as UnsortedSet instances or raw keys)

  • limit (Integer) (defaults to: 0)

    Stop counting after reaching this limit (0 = no limit)

Returns:

  • (Integer)

    Number of elements in the intersection



148
149
150
151
152
153
154
155
156
# File 'lib/familia/data_type/types/unsorted_set.rb', line 148

def intercard(*other_sets, limit: 0)
  keys = extract_keys(other_sets)
  all_keys = [dbkey, *keys]
  if limit.positive?
    dbclient.sintercard(all_keys.size, *all_keys, limit: limit)
  else
    dbclient.sintercard(all_keys.size, *all_keys)
  end
end

#intersection(*other_sets) ⇒ Array Also known as: inter

Returns the intersection of this set with one or more other sets.

Parameters:

  • other_sets (Array<UnsortedSet, String>)

    Other sets (as UnsortedSet instances or raw keys)

Returns:

  • (Array)

    Deserialized members present in all sets



93
94
95
96
97
# File 'lib/familia/data_type/types/unsorted_set.rb', line 93

def intersection(*other_sets)
  keys = extract_keys(other_sets)
  elements = dbclient.sinter(dbkey, *keys)
  deserialize_values(*elements)
end

#interstore(destination, *other_sets) ⇒ Integer Also known as: intersection_store

Stores the intersection of this set with other sets into a destination key.

Parameters:

  • destination (UnsortedSet, String)

    Destination set (as UnsortedSet instance or raw key)

  • other_sets (Array<UnsortedSet, String>)

    Other sets to intersect with

Returns:

  • (Integer)

    Number of elements in the resulting set



163
164
165
166
167
168
169
# File 'lib/familia/data_type/types/unsorted_set.rb', line 163

def interstore(destination, *other_sets)
  dest_key = extract_key(destination)
  keys = extract_keys(other_sets)
  result = dbclient.sinterstore(dest_key, dbkey, *keys)
  update_expiration
  result
end

#member?(val) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


77
78
79
# File 'lib/familia/data_type/types/unsorted_set.rb', line 77

def member?(val)
  dbclient.sismember dbkey, serialize_value(val)
end

#member_any?(*values) ⇒ Array<Boolean> Also known as: members?

Checks membership for multiple values at once.

Parameters:

  • values (Array)

    Values to check for membership

Returns:

  • (Array<Boolean>)

    Array of booleans indicating membership for each value



122
123
124
125
126
# File 'lib/familia/data_type/types/unsorted_set.rb', line 122

def member_any?(*values)
  values = values.flatten
  serialized = values.map { |v| serialize_value(v) }
  dbclient.smismember(dbkey, *serialized)
end

#membersObject Also known as: all, to_a



33
34
35
36
37
# File 'lib/familia/data_type/types/unsorted_set.rb', line 33

def members
  echo :members, Familia.pretty_stack(limit: 1) if Familia.debug
  elements = membersraw
  deserialize_values(*elements)
end

#membersrawObject



41
42
43
# File 'lib/familia/data_type/types/unsorted_set.rb', line 41

def membersraw
  dbclient.smembers(dbkey)
end

#move(dstkey, val) ⇒ Object



202
203
204
# File 'lib/familia/data_type/types/unsorted_set.rb', line 202

def move(dstkey, val)
  dbclient.smove dbkey, dstkey, val
end

#popObject



198
199
200
# File 'lib/familia/data_type/types/unsorted_set.rb', line 198

def pop
  dbclient.spop dbkey
end

#remove_element(value) ⇒ Integer Also known as: remove

Removes a member from the set

Parameters:

  • value

    The value to remove from the set

Returns:

  • (Integer)

    The number of members that were removed (0 or 1)



85
86
87
# File 'lib/familia/data_type/types/unsorted_set.rb', line 85

def remove_element(value)
  dbclient.srem dbkey, serialize_value(value)
end

#sample(count = 1) ⇒ Array

Get one or more random members from the set

Parameters:

  • count (Integer) (defaults to: 1)

    Number of random members to return (default: 1)

Returns:

  • (Array)

    Array of deserialized random members



209
210
211
# File 'lib/familia/data_type/types/unsorted_set.rb', line 209

def sample(count = 1)
  deserialize_values(*sampleraw(count))
end

#sampleraw(count = 1) ⇒ Array Also known as: random

Get one or more random members from the set without deserialization

Parameters:

  • count (Integer) (defaults to: 1)

    Number of random members to return (default: 1)

Returns:

  • (Array)

    Array of raw random members



217
218
219
# File 'lib/familia/data_type/types/unsorted_set.rb', line 217

def sampleraw(count = 1)
  dbclient.srandmember(dbkey, count) || []
end

#scan(cursor = 0, match: nil, count: nil) ⇒ Array<Integer, Array>

Iterates over set members using cursor-based iteration.

Parameters:

  • cursor (Integer) (defaults to: 0)

    Starting cursor position (default: 0)

  • match (String, nil) (defaults to: nil)

    Optional pattern to filter members

  • count (Integer, nil) (defaults to: nil)

    Optional hint for number of elements to return per call

Returns:

  • (Array<Integer, Array>)

    Two-element array: [new_cursor, deserialized_members]



134
135
136
137
138
139
140
141
# File 'lib/familia/data_type/types/unsorted_set.rb', line 134

def scan(cursor = 0, match: nil, count: nil)
  opts = {}
  opts[:match] = match if match
  opts[:count] = count if count

  new_cursor, elements = dbclient.sscan(dbkey, cursor, **opts)
  [new_cursor.to_i, deserialize_values(*elements)]
end

#selectObject



57
58
59
# File 'lib/familia/data_type/types/unsorted_set.rb', line 57

def select(&)
  members.select(&)
end

#selectrawObject



73
74
75
# File 'lib/familia/data_type/types/unsorted_set.rb', line 73

def selectraw(&)
  membersraw.select(&)
end

#union(*other_sets) ⇒ Array

Returns the union of this set with one or more other sets.

Parameters:

  • other_sets (Array<UnsortedSet, String>)

    Other sets (as UnsortedSet instances or raw keys)

Returns:

  • (Array)

    Deserialized members present in any of the sets



103
104
105
106
107
# File 'lib/familia/data_type/types/unsorted_set.rb', line 103

def union(*other_sets)
  keys = extract_keys(other_sets)
  elements = dbclient.sunion(dbkey, *keys)
  deserialize_values(*elements)
end

#unionstore(destination, *other_sets) ⇒ Integer Also known as: union_store

Stores the union of this set with other sets into a destination key.

Parameters:

  • destination (UnsortedSet, String)

    Destination set (as UnsortedSet instance or raw key)

  • other_sets (Array<UnsortedSet, String>)

    Other sets to union with

Returns:

  • (Integer)

    Number of elements in the resulting set



176
177
178
179
180
181
182
# File 'lib/familia/data_type/types/unsorted_set.rb', line 176

def unionstore(destination, *other_sets)
  dest_key = extract_key(destination)
  keys = extract_keys(other_sets)
  result = dbclient.sunionstore(dest_key, dbkey, *keys)
  update_expiration
  result
end