Class: Relix::MultiIndex

Inherits:
Index
  • Object
show all
Includes:
Ordering
Defined in:
lib/relix/indexes/multi.rb

Instance Attribute Summary

Attributes inherited from Index

#model_name

Instance Method Summary collapse

Methods inherited from Index

#attribute_immutable?, compact_kind, #create_query_clause, #filter, #initialize, kind, #name, #normalize, #query, #read, #read_normalized, #watch

Constructor Details

This class inherits a constructor from Relix::Index

Instance Method Details

#deindex(r, pk, old_value) ⇒ Object



18
19
20
21
# File 'lib/relix/indexes/multi.rb', line 18

def deindex(r, pk, old_value)
  r.zrem(key_for(old_value), pk)
  deindex_value(r, old_value) if index_values?
end

#deindex_value(r, old_value) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/relix/indexes/multi.rb', line 54

def deindex_value(r, old_value)
  r.eval %(
    if(redis.call("ZCARD", KEYS[2]) == 0) then
      return redis.call("SREM", KEYS[1], ARGV[1])
    end
    return "OK"
  ), [values_key, key_for(old_value)], [old_value]
end

#eq(r, value, options = {}) ⇒ Object



23
24
25
# File 'lib/relix/indexes/multi.rb', line 23

def eq(r, value, options={})
  r.zrange(key_for(value), *range_from_options(r, options, value))
end

#index(r, pk, object, value, old_value) ⇒ Object



11
12
13
14
15
16
# File 'lib/relix/indexes/multi.rb', line 11

def index(r, pk, object, value, old_value)
  r.zadd(key_for(value), score(object, value), pk)
  index_value(r, value) if index_values?

  deindex(r, pk, old_value)
end

#index_value(r, value) ⇒ Object



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

def index_value(r, value)
  r.sadd(values_key, value)
end

#index_values?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/relix/indexes/multi.rb', line 37

def index_values?
  @options[:index_values]
end

#key_for(value) ⇒ Object



33
34
35
# File 'lib/relix/indexes/multi.rb', line 33

def key_for(value)
  @set.keyer.component(name, value)
end

#position(r, pk, value) ⇒ Object



27
28
29
30
31
# File 'lib/relix/indexes/multi.rb', line 27

def position(r, pk, value)
  position = r.zrank(key_for(value), pk)
  raise MissingIndexValueError, "Cannot find key #{pk} in index for #{value}" unless position
  position
end

#values(r) ⇒ Object



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

def values(r)
  raise ValuesNotIndexedError.new("Value indexing not enabled for #{name}.") unless index_values?
  r.smembers(values_key)
end

#values_keyObject



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

def values_key
  @set.keyer.component(name, "_values")
end

#watch_keys(*values) ⇒ Object



5
6
7
8
9
# File 'lib/relix/indexes/multi.rb', line 5

def watch_keys(*values)
  keys = values.compact.map { |v| key_for(v) }
  keys << values_key if index_values?
  keys
end