Class: Queris::Query::Op::Operand

Inherits:
Object
  • Object
show all
Defined in:
lib/queris/query/operations.rb

Overview

boilerplate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op_index, val) ⇒ Operand

Returns a new instance of Operand.



9
10
11
12
13
# File 'lib/queris/query/operations.rb', line 9

def initialize(op_index, val)
  @temp_keys = []
  @index = op_index
  @value = val
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



8
9
10
# File 'lib/queris/query/operations.rb', line 8

def index
  @index
end

#optimization_keyObject (readonly)

Returns the value of attribute optimization_key.



80
81
82
# File 'lib/queris/query/operations.rb', line 80

def optimization_key
  @optimization_key
end

#temp_keysObject

Returns the value of attribute temp_keys.



8
9
10
# File 'lib/queris/query/operations.rb', line 8

def temp_keys
  @temp_keys
end

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/queris/query/operations.rb', line 8

def value
  @value
end

Instance Method Details

#cleanup_optimization(redis) ⇒ Object



94
95
96
97
98
# File 'lib/queris/query/operations.rb', line 94

def cleanup_optimization(redis)
   if is_query? && @optimize_subquery_key
     Queris.run_script(:subquery_intersect_optimization_cleanup, redis, [ key, index.key ])
   end
end

#delayed_optimize_query(smallkey, multiplier) ⇒ Object



72
73
74
75
# File 'lib/queris/query/operations.rb', line 72

def delayed_optimize_query(smallkey, multiplier)
  @optimize_subquery_key = smallkey
  @optimize_threshold_multiplier = multiplier
end

#gather_key_sizes(redis) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/queris/query/operations.rb', line 48

def gather_key_sizes(redis)
  @size={}
  k=key #we assume the key already exists on redis (it should have been created by the responsible index earlier)
  if Enumerable === k
    k.each { |k| @size[k]=index.key_size(k, redis) }
  else
    @size[k]=index.key_size(k, redis)
  end
end

#is_query?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/queris/query/operations.rb', line 45

def is_query?
  Query === @index
end

#json_redis_dump(op_name = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/queris/query/operations.rb', line 99

def json_redis_dump(op_name = nil)
  ret = []
  miniop = {}
  if is_query?
    miniop = {query: index.json_redis_dump}
  else
    index.json_redis_dump miniop
    if Range === @value && @index.handle_range?
      miniop[:min] = @index.val(@value.begin)
      miniop[@value.exclude_end? ? :max : :max_or_equal] = @index.val(@value.end)
      miniop[:key] = @index.key
    elsif Enumerable === @value
      @value.each do |val|
        miniop = { equal: @index.val(val), key: @index.key(val) }
        miniop[:op] = op_name if op_name
        ret << miniop
      end
      return ret
    else
      miniop[:equal] = @index.val(@value) unless miniop[:nocompare]
    end
  end
  miniop[:key] = @index.key(@value)
  miniop[:op]=op_name if op_name
  ret << miniop
  ret
end

#keyObject



17
18
19
20
21
22
23
# File 'lib/queris/query/operations.rb', line 17

def key
  if is_query? && @optimize_subquery_key
    @temp_key ||= index.results_key(:as_optimized_subquery)
  else
    index.key_for_query value
  end
end

#key_size(redis_key) ⇒ Object



57
58
59
60
61
# File 'lib/queris/query/operations.rb', line 57

def key_size(redis_key)
  raise ClientError "Attepted to get query operand key size, but it's not ready yet." unless Hash === @size
  s = @size[redis_key]
  return Redis::Future === s ? s.value : s
end

#marshal_dumpObject



14
15
16
# File 'lib/queris/query/operations.rb', line 14

def marshal_dump
  [(Query === index ? index.id : index.name).to_sym, value]
end

#optimized?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/queris/query/operations.rb', line 77

def optimized?
  @optimized && !@optimized.empty?
end

#optimized_key(whichkey = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/queris/query/operations.rb', line 24

def optimized_key(whichkey=nil)
  k=whichkey || key
  @optimized||={}
  if Enumerable===k
    k.map! { |ky| @optimized[ky] || ky }
  else
    k = @optimized[k] || k
  end
  k
end

#preintersect(smallkey, mykey) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/queris/query/operations.rb', line 62

def preintersect(smallkey, mykey)
  #puts "preintersect #{self} with #{smallkey}"
  @preintersect||={}
  @optimized||={}
  @preintersect[mykey]=smallkey
  preintersect_key = "#{mykey}:optimized:#{Queris.digest smallkey}"
  @optimized[mykey]=preintersect_key
  temp_keys << preintersect_key
  preintersect_key
end

#run_optimization(redis) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/queris/query/operations.rb', line 82

def run_optimization(redis)
  if @preintersect
    @preintersect.each do |k, smallkey|
      #puts "running optimization - preintersecting #{k} and #{smallkey}"
      Queris.run_script(:query_intersect_optimization, redis, [@optimized[k], k, smallkey])
    end
    #puts "preintersected some stuff"
  elsif is_query? && @optimize_subquery_key
    Queris.run_script(:subquery_intersect_optimization, redis, [ key, index.key, @optimize_subquery_key], [ @optimize_threshold_multiplier ])
    #puts "no optimizations to run"
  end
end

#splitObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/queris/query/operations.rb', line 35

def split
  if Array === key && Enumerable === value
    raise ClientError, "Sanity check failed - different number of keys and values, bailing." if key.length != value.length
    value.map do |val|
      self.class.new(index, val)
    end
  else
    [ self ]
  end
end