Class: Redlics::Query::Operation

Inherits:
Object
  • Object
show all
Includes:
Operators
Defined in:
lib/redlics/query/operation.rb

Overview

Operation class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operators

#&, #-, #-@, #^, #|

Constructor Details

#initialize(operator, queries) ⇒ Redlics::Query::Operation

Initialization of a query operation object.

Parameters:

  • operator (String)

    operator to calculate

  • queries (Array)

    queries to calculate with the given operator



18
19
20
21
22
23
24
# 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

Instance Attribute Details

#namespacesObject (readonly)

Gives read access to the listed instance variables.



11
12
13
# File 'lib/redlics/query/operation.rb', line 11

def namespaces
  @namespaces
end

Class Method Details

.finalize(namespaces) ⇒ Integer, NilClass

Finalize query operation called from garbage collector.

Parameters:

  • namespaces (Array)

    list of created operation keys in Redis

Returns:

  • (Integer)

    result of Redis delete keys

  • (NilClass)

    nil if namespaces are empty



92
93
94
# File 'lib/redlics/query/operation.rb', line 92

def finalize(namespaces)
  proc { reset_redis_namespaces(namespaces) }
end

.reset_redis_namespaces(namespaces) ⇒ Integer, NilClass

Reset Redis created namespace keys.

Parameters:

  • namespaces (Array)

    list of created operation keys in Redis

Returns:

  • (Integer)

    result of Redis delete keys

  • (NilClass)

    nil if namespaces are empty



101
102
103
# File 'lib/redlics/query/operation.rb', line 101

def reset_redis_namespaces(namespaces)
  Redlics.redis { |r| r.del(namespaces) } if namespaces.any?
end

Instance Method Details

#exists?(id) ⇒ Boolean

Check if object id exists in track bits.

Parameters:

  • the (Integer)

    object id to check

Returns:

  • (Boolean)

    true if exists, false if not



56
57
58
# 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?Boolean

Check if query operation is a leaf in the binary tree.

Returns:

  • (Boolean)

    true if a leaf, false if not



81
82
83
# File 'lib/redlics/query/operation.rb', line 81

def is_leaf?
  is_a?(Redlics::Query::Operation) && @track_bits.nil?
end

#reset!(space = nil) ⇒ Boolean

Reset processed data (also operation keys on Redis).

Parameters:

  • space (Symbol) (defaults to: nil)

    define space to reset

  • space (String) (defaults to: nil)

    define space to reset

Returns:

  • (Boolean)

    true



65
66
67
68
69
70
71
72
73
74
75
76
77
# 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_bitsString

Get or process track bits on Redis.

Returns:

  • (String)

    key of track bits result



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

#tracksInteger

Get or process tracks on Redis.

Returns:

  • (Integer)

    tracks result of given query operation



28
29
30
31
32
# File 'lib/redlics/query/operation.rb', line 28

def tracks
  @tracks ||= (
    Redlics.redis { |r| r.bitcount(@track_bits || traverse) }
  )
end