Class: Relix::IndexSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, redis_source) ⇒ IndexSet

Returns a new instance of IndexSet.



5
6
7
8
9
10
# File 'lib/relix/index_set.rb', line 5

def initialize(klass, redis_source)
  @klass = klass
  @redis_source = redis_source
  @indexes = Hash.new
  @keyer = Keyer.default_for(@klass) unless parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/relix/index_set.rb', line 40

def method_missing(m, *args)
  if Relix.index_types.keys.include?(m.to_sym)
    add_index(m, *args)
  else
    super
  end
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/relix/index_set.rb', line 4

def klass
  @klass
end

#redisObject

Returns the value of attribute redis.



3
4
5
# File 'lib/relix/index_set.rb', line 3

def redis
  @redis
end

Instance Method Details

#[](name) ⇒ Object



161
162
163
# File 'lib/relix/index_set.rb', line 161

def [](name)
  full_index_list[name.to_s]
end

#add_index(index_type, name, options = {}) ⇒ Object



48
49
50
51
# File 'lib/relix/index_set.rb', line 48

def add_index(index_type, name, options={})
  accessor = (options.delete(:on) || name)
  @indexes[name.to_s] = Relix.index_types[index_type].new(self, name, accessor, options)
end

#current_values_name(pk) ⇒ Object



157
158
159
# File 'lib/relix/index_set.rb', line 157

def current_values_name(pk)
  keyer.values(pk, @klass)
end

#deindex!(object) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/relix/index_set.rb', line 109

def deindex!(object)
  pk = primary_key_for(object)

  handle_concurrent_modifications(pk) do
    current_values_name = current_values_name(pk)
    redis.watch current_values_name
    current_values = redis.hgetall(current_values_name)

    full_index_list.map do |name, index|
      old_value = if index.attribute_immutable?
        index.read_normalized(object)
      else
        current_values[name]
      end

      ((watch = index.watch(old_value)) && !watch.empty? && redis.watch(*watch))
      proc { index.deindex(redis, pk, old_value) }
    end.tap { |ops| ops << proc { redis.del current_values_name } }
  end
end

#deindex_by_primary_key!(pk) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/relix/index_set.rb', line 130

def deindex_by_primary_key!(pk)
  handle_concurrent_modifications(pk) do
    current_values_name = current_values_name(pk)
    redis.watch current_values_name
    current_values = redis.hgetall(current_values_name)

    full_index_list.map do |name, index|
      old_value = current_values[name]

      ((watch = index.watch(old_value)) && !watch.empty? && redis.watch(*watch))
      proc { index.deindex(redis, pk, old_value) }
    end.tap { |ops| ops << proc { redis.del current_values_name } }
  end
end

#index!(object) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/relix/index_set.rb', line 101

def index!(object)
  pk = primary_key_for(object)

  handle_concurrent_modifications(pk) do
    index_ops(object, pk)
  end
end

#index_ops(object, pk) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/relix/index_set.rb', line 72

def index_ops(object, pk)
  current_values_name = current_values_name(pk)
  redis.watch current_values_name
  current_values = redis.hgetall(current_values_name)

  ops = full_index_list.collect do |name,index|
    value = index.read_normalized(object)
    old_value = current_values[name]

    ((watch = index.watch(value, old_value)) && redis.watch(*watch))

    next if value == old_value
    current_values[name] = value unless index.attribute_immutable?

    next unless index.filter(redis, object, value)

    query_value = index.query(redis, value)
    proc do
      index.index(redis, pk, object, value, old_value, *query_value)
    end
  end.compact

  ops << proc do
    redis.hmset(current_values_name, *current_values.flatten)
  end if current_values.any?

  ops
end

#indexesObject



53
54
55
56
# File 'lib/relix/index_set.rb', line 53

def indexes
  Relix.deprecate("Calling #indexes is deprecated; use #[] instead.", "2")
  self
end

#key_prefix(name) ⇒ Object



145
146
147
# File 'lib/relix/index_set.rb', line 145

def key_prefix(name)
  "#{@klass.name}:#{name}"
end

#keyer(value = nil, options = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/relix/index_set.rb', line 32

def keyer(value=nil, options={})
  if value
    @keyer = value.new(@klass, options)
  else
    (@keyer || parent.keyer)
  end
end

#lookup(&block) ⇒ Object



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

def lookup(&block)
  if block
    query = Query.new(self)
    yield(query)
    query.run
  else
    primary_key_index.all(redis)
  end
end

#lookup_values(index) ⇒ Object



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

def lookup_values(index)
  self[index].values(redis)
end

#parentObject



149
150
151
152
153
154
155
# File 'lib/relix/index_set.rb', line 149

def parent
  unless @parent || @parent == false
    parent = @klass.superclass
    @parent = (parent.respond_to?(:relix) ? parent.relix : false)
  end
  @parent
end

#primary_key(accessor) ⇒ Object Also known as: pk



16
17
18
# File 'lib/relix/index_set.rb', line 16

def primary_key(accessor)
  @primary_key_index = add_index(:primary_key, accessor)
end

#primary_key_indexObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/relix/index_set.rb', line 21

def primary_key_index
  unless @primary_key_index
    if parent
      @primary_key_index = parent.primary_key_index
    else
      raise MissingPrimaryKeyError.new("You must declare a primary key for #{@klass.name}")
    end
  end
  @primary_key_index
end