Module: Redis::Bitops::Queries::MaterializationHelpers

Included in:
BinaryOperator, UnaryOperator
Defined in:
lib/redis/bitops/queries/materialization_helpers.rb

Overview

Helpers for materialization, i.e. running a BITOP command or, possibly, another command and saving its results to a Redis database in ‘intermediate’.

Instance Method Summary collapse

Instance Method Details

#bitmap_factoryObject



47
48
49
# File 'lib/redis/bitops/queries/materialization_helpers.rb', line 47

def bitmap_factory
  raise "Override in the class using the module to return the bitmap factory."
end

#resolve_operand(o, intermediate, temp_intermediates) ⇒ Object

Materializes the operand ‘o’ saving the result in ‘redis’. If the operand can be materialized, it does so storing the result in ‘intermediate’ unless the latter is nil. In that case, a temp intermediate bitmap is created to hold the result (and the bitmap is added to ‘temp_intermediates’).



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/redis/bitops/queries/materialization_helpers.rb', line 15

def resolve_operand(o, intermediate, temp_intermediates)
  if o.respond_to?(:materialize)
    if intermediate.nil?
      new_intermediate = temp_bitmap
      temp_intermediates << new_intermediate
    end
    intermediate ||= new_intermediate
    o.materialize(intermediate)
    [intermediate, nil]
  else
    [o, intermediate]
  end
end

#temp_bitmapObject

Creates a temp bitmap.



31
32
33
34
# File 'lib/redis/bitops/queries/materialization_helpers.rb', line 31

def temp_bitmap
  bitmap = bitmap_factory.call(unique_key)
  bitmap
end

#unique_keyObject

Generates a random unique key.

TODO: The key should be unique and not repeat in the database but this isn’t guaranteed. Considering the intended usage though (creation of temporary intermediate bitmaps while materializing queries), it should be sufficient.



43
44
45
# File 'lib/redis/bitops/queries/materialization_helpers.rb', line 43

def unique_key
  "redis:bitops:#{SecureRandom.hex(20)}"
end