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, #index?, #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



59
60
61
62
63
64
65
66
# File 'lib/relix/indexes/multi.rb', line 59

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

#destroy(r, pk, old_value) ⇒ Object



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

def destroy(r, pk, old_value)
  r.del(key_for(old_value))
  r.destroy_values(r) if index_values?
end

#destroy_value(r) ⇒ Object



68
69
70
# File 'lib/relix/indexes/multi.rb', line 68

def destroy_value(r)
  r.del(values_key)
end

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



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

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



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

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

#index_values?Boolean

Returns:

  • (Boolean)


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

def index_values?
  @options[:index_values]
end

#key_for(value) ⇒ Object



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

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

#position(r, pk, value) ⇒ Object



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

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



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

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

#values_keyObject



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

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