Module: EmptyEye::ShardWrangler::ClassMethods

Defined in:
lib/empty_eye/shard_wrangler.rb

Instance Method Summary collapse

Instance Method Details

#cascade_delete_all(conditions) ⇒ Object

batch deletion when there are conditions kill indiscriminately otherwise



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

def cascade_delete_all(conditions)
  mti_clear_identity_map
  affected = 0
  ids = []
  ids = conditions ? select(arel_table[primary_key.to_sym]).where(conditions).collect(&:id) : []
  transaction do
    begin
      batch = ids.pop(10000)
      shards.each do |shard|
        result = if conditions.nil?
          shard.klass.delete_all
        elsif shard.polymorphic_type
          shard.klass.delete_all(shard.foreign_key => batch, shard.polymorphic_type => shard.type_value)
        else
          shard.klass.delete_all(shard.foreign_key => batch)
        end
        affected = [affected, result].max
      end 
    end until ids.to_a.empty?
  end
  affected
end

#cascade_update_all(updates, conditions, options) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/empty_eye/shard_wrangler.rb', line 198

def cascade_update_all(updates, conditions, options)
  mti_clear_identity_map
  affected = 0
  stringified_updates = updates.stringify_keys
  ids = conditions ? select(arel_table[primary_key.to_sym]).where(conditions).apply_finder_options(options.slice(:limit, :order)).collect(&:id) : []
  transaction do
    begin
      batch = ids.pop(10000)
      shards.each do |shard|
        cols = shards.delegate_map(shard.name, stringified_updates)
        next if cols.empty?
        result = if conditions.nil?
          shard.klass.update_all(cols)
        elsif shard.polymorphic_type
          shard.klass.update_all(cols, shard.foreign_key => batch, shard.polymorphic_type => shard.type_value)
        else
          shard.klass.update_all(cols, shard.foreign_key => batch)
        end
        affected = [affected, result].max
      end
    end until ids.to_a.empty?
  end
  affected
end

#compute_view_nameObject

we need a name for the view need to have a way to set this



229
230
231
232
233
234
235
# File 'lib/empty_eye/shard_wrangler.rb', line 229

def compute_view_name
  if master_class.descends_from_active_record?
    master_class.send(:compute_table_name)
  else
    master_class.name.underscore.pluralize
  end
end

#create_reflection(macro, name, options, active_record) ⇒ Object

the wrangler uses special reflection; overriden here



112
113
114
115
116
117
118
119
# File 'lib/empty_eye/shard_wrangler.rb', line 112

def create_reflection(macro, name, options, active_record)
  raise(EmptyEye::NotYetSupported, "through associations are not yet spported") if options[:through]
  klass = options[:through] ? ShardThroughReflection : ShardAssociationReflection
  reflection = klass.new(macro, name, options, active_record)

  self.reflections = self.reflections.merge(name => reflection)
  reflection
end

#find_by_id(val) ⇒ Object

overriding find_by_id this is used to retrieve the wrangler instance for the master instance the type column is removed



131
132
133
134
135
# File 'lib/empty_eye/shard_wrangler.rb', line 131

def find_by_id(val)
  query = columns_except_type
  query = query.where(arel_table[:id].eq(val))
  find_by_sql(query.to_sql).first
end

#has_one(name, options = {}) ⇒ Object

the wrangler uses a special association builder



138
139
140
# File 'lib/empty_eye/shard_wrangler.rb', line 138

def has_one(name, options = {})
  Associations::Builder::ShardHasOne.build(self, name, options)
end

#master_classObject

reflection on master class; this should never change



143
144
145
# File 'lib/empty_eye/shard_wrangler.rb', line 143

def master_class
  @master_class
end

#master_class=(klass) ⇒ Object

the master_class value is set with this setter; should happen only once



148
149
150
# File 'lib/empty_eye/shard_wrangler.rb', line 148

def master_class=(klass)
  @master_class = klass
end

#primary_shardObject

the primary shard



159
160
161
# File 'lib/empty_eye/shard_wrangler.rb', line 159

def primary_shard
  shards.primary
end

#reset_column_informationObject

overriding to reset the special instance variable



153
154
155
156
# File 'lib/empty_eye/shard_wrangler.rb', line 153

def reset_column_information
  @columns_except_type = nil
  super
end

#shardsObject



223
224
225
# File 'lib/empty_eye/shard_wrangler.rb', line 223

def shards
  @shards ||= EmptyEye::ShardCollection.new(self)
end

#type_condition(table = arel_table) ⇒ Object

finder methods should use the master class’s type not the wrangler’s



122
123
124
125
126
# File 'lib/empty_eye/shard_wrangler.rb', line 122

def type_condition(table = arel_table)
  sti_column = table[inheritance_column.to_sym]

  sti_column.eq(master_class.name)
end

#wrangle_shards(mti_ancestors) ⇒ Object

we know the associations and we know what they can do we will make a mti class accordingly here



165
166
167
168
169
170
171
# File 'lib/empty_eye/shard_wrangler.rb', line 165

def wrangle_shards(mti_ancestors)
  mti_ancestors.each do |assoc|
    shards.create_with(assoc)
  end
  create_view if create_view?
  master_class.reset_column_information
end