Class: Relix::OrderedIndex::QueryClause

Inherits:
Object
  • Object
show all
Defined in:
lib/relix/indexes/ordered.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis, index) ⇒ QueryClause

Returns a new instance of QueryClause.



35
36
37
38
# File 'lib/relix/indexes/ordered.rb', line 35

def initialize(redis, index)
  @redis, @index = redis, index
  @lt, @gt, @limit, @offset, @order = '+inf', '-inf', nil, nil, :asc
end

Instance Method Details

#eq(value) ⇒ Object



60
61
62
63
# File 'lib/relix/indexes/ordered.rb', line 60

def eq(value)
  lte(value)
  gte(value)
end

#gt(value) ⇒ Object



50
51
52
53
# File 'lib/relix/indexes/ordered.rb', line 50

def gt(value)
  @gt = "(#{@index.score_for_value(value)}"
  self
end

#gte(value) ⇒ Object



55
56
57
58
# File 'lib/relix/indexes/ordered.rb', line 55

def gte(value)
  @gt = @index.score_for_value(value)
  self
end

#limit(value) ⇒ Object



74
75
76
77
# File 'lib/relix/indexes/ordered.rb', line 74

def limit(value)
  @limit = value
  self
end

#lookupObject



91
92
93
94
95
96
97
98
# File 'lib/relix/indexes/ordered.rb', line 91

def lookup
  command, score_1, score_2 = case @order
    when :desc then [:zrevrangebyscore, @lt, @gt]
    when :asc  then [:zrangebyscore,    @gt, @lt]
  end

  @redis.send(command, @index.sorted_set_name, score_1, score_2, limit: zrangebyscore_limit)
end

#lt(value) ⇒ Object



40
41
42
43
# File 'lib/relix/indexes/ordered.rb', line 40

def lt(value)
  @lt = "(#{@index.score_for_value(value)}"
  self
end

#lte(value) ⇒ Object



45
46
47
48
# File 'lib/relix/indexes/ordered.rb', line 45

def lte(value)
  @lt = @index.score_for_value(value)
  self
end

#offset(value) ⇒ Object



79
80
81
82
# File 'lib/relix/indexes/ordered.rb', line 79

def offset(value)
  @offset = value
  self
end

#order(value) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/relix/indexes/ordered.rb', line 65

def order(value)
  unless [:asc, :desc].include?(value)
    raise InvalidQueryOption.new("order must be :asc or :desc but was #{value.inspect}")
  end

  @order = value
  self
end

#zrangebyscore_limitObject



84
85
86
87
88
89
# File 'lib/relix/indexes/ordered.rb', line 84

def zrangebyscore_limit
  # zrangebyscore uses offset/count rather than start/stop like zrange
  offset, stop = @index.range_from_options(@redis, offset: @offset, limit: @limit)
  count = stop == -1 ? -1 : (stop - offset + 1)
  [offset, count]
end