Class: Relix::UniqueIndex

Inherits:
Index
  • Object
show all
Includes:
Ordering
Defined in:
lib/relix/indexes/unique.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, #initialize, kind, #name, #normalize, #query, #read, #read_normalized, #watch

Constructor Details

This class inherits a constructor from Relix::Index

Instance Method Details

#all(r, options = {}) ⇒ Object



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

def all(r, options={})
  r.zrange(sorted_set_name, *range_from_options(r, options))
end

#deindex(r, pk, old_value) ⇒ Object



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

def deindex(r, pk, old_value)
  r.hdel(hash_name, old_value)
  r.zrem(sorted_set_name, pk)
end

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



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

def eq(r, value, options={})
  [r.hget(hash_name, value)].compact
end

#filter(r, object, value) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/relix/indexes/unique.rb', line 17

def filter(r, object, value)
  return true if read(object).values.any?{|e| e.nil?}
  if r.hexists(hash_name, value)
    raise NotUniqueError.new("'#{value}' is not unique in index #{name}")
  end
  true
end

#hash_nameObject



9
10
11
# File 'lib/relix/indexes/unique.rb', line 9

def hash_name
  @set.keyer.component(name, 'lookup')
end

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/relix/indexes/unique.rb', line 25

def index(r, pk, object, value, old_value)
  if read(object).values.all?{|e| !e.nil?}
    r.hset(hash_name, value, pk)
    r.zadd(sorted_set_name, score(object, value), pk)
  else
    r.hdel(hash_name, value)
    r.zrem(sorted_set_name, pk)
  end
  r.hdel(hash_name, old_value)
end

#sorted_set_nameObject



5
6
7
# File 'lib/relix/indexes/unique.rb', line 5

def sorted_set_name
  @set.keyer.component(name, 'ordering')
end

#watch_keys(*values) ⇒ Object



13
14
15
# File 'lib/relix/indexes/unique.rb', line 13

def watch_keys(*values)
  hash_name
end