Class: RedBlocks::Set

Inherits:
Object
  • Object
show all
Includes:
SetOptimizer, SetUtils
Defined in:
lib/red_blocks/set.rb

Direct Known Subclasses

ComposedSet, EnumSet, InstantSet, SubtractionSet, UnitSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SetOptimizer

#unset

Methods included from SetUtils

included, #joined_key, #normalize_entries, #validate_array_entry, #validate_entries!

Instance Attribute Details

#weightNumeric

Returns A scale factor for score calculaton on intersection or union.

Returns:

  • (Numeric)

    A scale factor for score calculaton on intersection or union.



94
95
96
# File 'lib/red_blocks/set.rb', line 94

def weight
  @weight || 1
end

Class Method Details

.key_patternObject



56
57
58
# File 'lib/red_blocks/set.rb', line 56

def self.key_pattern
  joined_key([RedBlocks.config.key_namespace, key_prefix, '*'])
end

Instance Method Details

#cache_timeInteger

Returns Cache time in seconds.

Returns:

  • (Integer)

    Cache time in seconds.



89
90
91
# File 'lib/red_blocks/set.rb', line 89

def cache_time
  RedBlocks::CachePolicy.none
end

#disabled?(ttl = RedBlocks.client.ttl(key)) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/red_blocks/set.rb', line 21

def disabled?(ttl = RedBlocks.client.ttl(key))
  ttl < RedBlocks.config.intermediate_set_lifetime
end

#expiration_timeObject



25
26
27
# File 'lib/red_blocks/set.rb', line 25

def expiration_time
  RedBlocks.config.intermediate_set_lifetime + cache_time
end

#expression(id) ⇒ Object

Used in debug mode (with_expressions option)



105
106
107
108
# File 'lib/red_blocks/set.rb', line 105

def expression(id)
  score = self.disabled? ? nil : (RedBlocks.client.zscore(key, id) || 0)
  Expression.new(key, score: score, label: label, weight: weight)
end

#getArray+

Returns A list of ids, or a zippped list of ids and scores.

Examples:

[1, 3, 5]
[[1, 3.4], [5, 2.3]]

Returns:

  • (Array, Array<Array>)

    A list of ids, or a zippped list of ids and scores.

Raises:

  • (NotImplementedError)


84
85
86
# File 'lib/red_blocks/set.rb', line 84

def get
  raise NotImplementedError
end

#ids(paginator: Paginator.new, with_scores: false, with_expressions: false, update_if_disabled: true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/red_blocks/set.rb', line 29

def ids(paginator: Paginator.new, with_scores: false, with_expressions: false, update_if_disabled: true)
  update_if_disabled! if update_if_disabled
  res = RedBlocks.client.zrevrange(key, paginator.head, paginator.tail, with_scores: with_scores)
  res
    .map { |id, _score|
      if with_scores || with_expressions
        res = [id.to_i]
        res << _score if with_scores
        res << expression(id) if with_expressions
        res
      else
        id.to_i
      end
    }.select{ |id, _, _|
    id !=  RedBlocks.config.blank_id
  }
end

#keyObject



52
53
54
# File 'lib/red_blocks/set.rb', line 52

def key
  joined_key([RedBlocks.config.key_namespace, self.class.key_prefix, key_suffix])
end

#labelObject



100
101
102
# File 'lib/red_blocks/set.rb', line 100

def label
  nil
end

#size(update_if_disabled: true) ⇒ Object



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

def size(update_if_disabled: true)
  update_if_disabled! if update_if_disabled
  RedBlocks.client.zcard(key) - 1 # Discount the blank entry.
end

#update!(get_result = self.get) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/red_blocks/set.rb', line 6

def update!(get_result = self.get)
  entries = normalize_entries(validate_entries!(get_result))
  removed_ids = self.ids(paginator: Paginator.all, update_if_disabled: false) - entries.map(&:last)
  RedBlocks.client.pipelined do
    RedBlocks.client.zrem(key, removed_ids) if removed_ids.size > 0
    RedBlocks.client.zadd(key, entries)
    RedBlocks.client.expire(key, expiration_time)
  end
  nil
end

#update_if_disabled!Object



17
18
19
# File 'lib/red_blocks/set.rb', line 17

def update_if_disabled!
  update! if disabled?
end