Class: Redis::Bitops::Queries::UnaryOperator

Inherits:
Object
  • Object
show all
Includes:
LazyEvaluation, MaterializationHelpers, TreeBuildingHelpers
Defined in:
lib/redis/bitops/queries/unary_operator.rb

Overview

A unary bitwise operator. Currently only NOT is supported by redis.

Instance Method Summary collapse

Methods included from LazyEvaluation

#dest, #evaluate

Methods included from TreeBuildingHelpers

#&, #^, #|, #~

Methods included from MaterializationHelpers

#resolve_operand, #temp_bitmap, #unique_key

Constructor Details

#initialize(op, arg) ⇒ UnaryOperator

Create a new bitwise operator ‘op’ with one argument ‘arg’.



14
15
16
17
# File 'lib/redis/bitops/queries/unary_operator.rb', line 14

def initialize(op, arg)
  @op = op
  @arg = arg
end

Instance Method Details

#bitmap_factoryObject

Finds the first bitmap factory in the expression tree. Required by LazyEvaluation and MaterializationHelpers.



42
43
44
# File 'lib/redis/bitops/queries/unary_operator.rb', line 42

def bitmap_factory
  @arg.bitmap_factory or raise "Internal error. Cannot get redis connection."
end

#materialize(dest) ⇒ Object

Runs the expression tree against the redis database, saving the results in bitmap ‘dest’.



22
23
24
25
26
27
28
# File 'lib/redis/bitops/queries/unary_operator.rb', line 22

def materialize(dest)
  temp_intermediates = []
  result, = resolve_operand(@arg, dest, temp_intermediates)
  result.bitop(@op, dest)
ensure
  temp_intermediates.each(&:delete!)
end

#optimize!(parent_op = nil) ⇒ Object

Optimizes the expression tree by combining operands for neighboring identical operators. Because NOT operator in Redis can only accept one operand, no optimization is made for the operand but the children are optimized recursively.



34
35
36
37
# File 'lib/redis/bitops/queries/unary_operator.rb', line 34

def optimize!(parent_op = nil)
  @arg.optimize!(@op) if @arg.respond_to?(:optimize!)
  self
end