class Redlics::Query::Operation

Operation class

Attributes

namespaces[R]

Gives read access to the listed instance variables.

Public Class Methods

finalize(namespaces) click to toggle source

Finalize query operation called from garbage collector.

@param namespaces [Array] list of created operation keys in Redis @return [Integer] result of Redis delete keys @return [NilClass] nil if namespaces are empty

# File lib/redlics/query/operation.rb, line 92
def finalize(namespaces)
  proc { reset_redis_namespaces(namespaces) }
end
new(operator, queries) click to toggle source

Initialization of a query operation object.

@param operator [String] operator to calculate @param queries [Array] queries to calculate with the given operator @return [Redlics::Query::Operation] query operation object

# File lib/redlics/query/operation.rb, line 18
def initialize(operator, queries)
  @operator = operator.upcase.freeze
  @queries = queries.freeze
  @track_bits = nil
  @namespaces = []
  ObjectSpace.define_finalizer(self, self.class.finalize(namespaces)) if Redlics.config.auto_clean
end
reset_redis_namespaces(namespaces) click to toggle source

Reset Redis created namespace keys.

@param namespaces [Array] list of created operation keys in Redis @return [Integer] result of Redis delete keys @return [NilClass] nil if namespaces are empty

# File lib/redlics/query/operation.rb, line 101
def reset_redis_namespaces(namespaces)
  Redlics.redis { |r| r.del(namespaces) } if namespaces.any?
end

Public Instance Methods

exists?(id) click to toggle source

Check if object id exists in track bits.

@param [Integer] the object id to check @return [Boolean] true if exists, false if not

# File lib/redlics/query/operation.rb, line 56
def exists?(id)
  Redlics.redis { |r| r.getbit(@track_bits || traverse, id.to_i) } == 1
end
is_leaf?() click to toggle source

Check if query operation is a leaf in the binary tree. @return [Boolean] true if a leaf, false if not

# File lib/redlics/query/operation.rb, line 81
def is_leaf?
  is_a?(Redlics::Query::Operation) && @track_bits.nil?
end
reset!(space = nil) click to toggle source

Reset processed data (also operation keys on Redis).

@param space [Symbol] define space to reset @param space [String] define space to reset @return [Boolean] true

# File lib/redlics/query/operation.rb, line 65
def reset!(space = nil)
  space = space.to_sym if space
  case space
  when :tree
    @queries.each { |q| q.reset!(:tree) }
    reset!
  else
    @tracks, @track_bits = nil, nil
    self.class.reset_redis_namespaces(@namespaces)
    @namespaces = []
  end
  return true
end
track_bits() click to toggle source

Get or process track bits on Redis. @return [String] key of track bits result

# File lib/redlics/query/operation.rb, line 36
def track_bits
  @track_bits ||= (
    keys = []
    track_bits_namespace = Key.unique_namespace
    @namespaces << track_bits_namespace
    if @operator == 'NOT'
      keys << Key.with_namespace(@queries[0].track_bits)
    else
      @queries.each { |q| keys << Key.with_namespace(q.track_bits) }
    end
    Redlics.script(Redlics::LUA_SCRIPT, [], ['operation'.to_msgpack, keys.to_msgpack,
                   { operator: @operator, dest: Key.with_namespace(track_bits_namespace) }.to_msgpack])
    track_bits_namespace
  )
end
tracks() click to toggle source

Get or process tracks on Redis. @return [Integer] tracks result of given query operation

# File lib/redlics/query/operation.rb, line 28
def tracks
  @tracks ||= (
    Redlics.redis { |r| r.bitcount(@track_bits || traverse) }
  )
end

Private Instance Methods

traverse() click to toggle source

Traverse query operation binary tree and calculate operation leafs. @return [String] result operation key in Redis

# File lib/redlics/query/operation.rb, line 110
def traverse
  if @operator == 'NOT'
    @queries[0].traverse unless @queries[0].is_leaf?
    track_bits
  else
    @queries.each { |q| q.traverse unless q.is_leaf? }
    track_bits
  end
end