Class: Relix::IndexSet

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

Defined Under Namespace

Classes: Obsolater

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
11
# File 'lib/relix/index_set.rb', line 5

def initialize(klass, redis_source)
  @klass = klass
  @redis_source = redis_source
  @indexes = Hash.new
  @obsolete_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



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

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



229
230
231
# File 'lib/relix/index_set.rb', line 229

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

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



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

def add_index(index_type, name, options={})
  raise Relix::InvalidIndexError.new("Index #{name} is already declared as obsolete.") if @obsolete_indexes[name.to_s]

  @indexes[name.to_s] = create_index(self, index_type, name, options)
end

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



75
76
77
78
79
80
# File 'lib/relix/index_set.rb', line 75

def add_obsolete_index(index_type, name, options={})
  raise Relix::InvalidIndexError.new("Primary key indexes cannot be obsoleted.") if(index_type == :primary_key)
  raise Relix::InvalidIndexError.new("Index #{name} is already declared as non-obsolete.") if @indexes[name.to_s]

  @obsolete_indexes[name.to_s] = create_index(self, index_type, name, options)
end

#current_values_name(pk) ⇒ Object



225
226
227
# File 'lib/relix/index_set.rb', line 225

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

#deindex!(object) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/relix/index_set.rb', line 177

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(:including_obsolete).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



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/relix/index_set.rb', line 198

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(:including_obsolete).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

#destroy_index(name) ⇒ Object

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/relix/index_set.rb', line 82

def destroy_index(name)
  name = name.to_s
  index = @obsolete_indexes[name]
  raise MissingIndexError.new("No obsolete index found for #{name}.") unless index
  raise InvalidIndexError.new("Indexes built on immutable attributes cannot be destroyed.") if index.attribute_immutable?

  lookup.each do |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)

      old_value = current_values[name]

      ((watch = index.watch(old_value)) && !watch.empty? && redis.watch(*watch))
      ops = []
      ops << proc{ index.destroy(redis, pk, old_value) } if index.respond_to?(:destroy)
      ops << proc{ redis.hdel current_values_name, name }
      ops
    end
  end

  if index.respond_to?(:destroy_all)
    index.destroy_all(redis)
  end
end

#index!(object) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/relix/index_set.rb', line 169

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

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

#index_ops(object, pk) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/relix/index_set.rb', line 128

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

  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))

    if index.index?(redis, object, value)
      new_current_values[name] = value unless index.attribute_immutable?
      next if value == old_value
      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
    else
      proc do
        index.deindex(redis, pk, old_value)
      end
    end
  end.compact

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

  ops
end

#indexesObject



109
110
111
112
# File 'lib/relix/index_set.rb', line 109

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

#key_prefix(name) ⇒ Object



213
214
215
# File 'lib/relix/index_set.rb', line 213

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

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



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

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

#lookup(&block) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/relix/index_set.rb', line 114

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



124
125
126
# File 'lib/relix/index_set.rb', line 124

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

#obsolete(&block) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
73
# File 'lib/relix/index_set.rb', line 69

def obsolete(&block)
  raise ArgumentError.new("No block passed.") unless block_given?

  Obsolater.new(self).instance_eval(&block)
end

#parentObject



217
218
219
220
221
222
223
# File 'lib/relix/index_set.rb', line 217

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



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

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

#primary_key_indexObject



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

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