Module: Switchman::ActiveRecord::Relation

Defined in:
lib/switchman/active_record/relation.rb

Defined Under Namespace

Modules: InsertUpsertAll

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(klass) ⇒ Object



6
7
8
# File 'lib/switchman/active_record/relation.rb', line 6

def self.prepended(klass)
  klass::SINGLE_VALUE_METHODS.push(:shard, :shard_source)
end

Instance Method Details

#activate(unordered: false, &block) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/switchman/active_record/relation.rb', line 166

def activate(unordered: false, &block)
  shards = all_shards
  if Array === shards && shards.length == 1
    if !loaded? && shard_value != shards.first
      shard(shards.first).activate(&block)
    elsif shards.first == DefaultShard || shards.first == Shard.current(klass.connection_class_for_self)
      yield(self, shards.first)
    else
      shards.first.activate(klass.connection_class_for_self) { yield(self, shards.first) }
    end
  else
    result_count = 0
    can_order = false
    result = Shard.with_each_shard(shards, [klass.connection_class_for_self]) do
      # don't even query other shards if we're already past the limit
      next if limit_value && result_count >= limit_value && order_values.empty?

      relation = shard(Shard.current(klass.connection_class_for_self))
      relation.remove_nonlocal_primary_keys!
      # do a minimal query if possible
      if limit_value && !result_count.zero? && order_values.empty?
        relation = relation.limit(limit_value - result_count)
      end

      shard_results = relation.activate(&block)

      if shard_results.present? && !unordered
        can_order ||= can_order_cross_shard_results? unless order_values.empty?
        raise OrderOnMultiShardQuery if !can_order && !order_values.empty? && result_count.positive?

        result_count += shard_results.is_a?(Array) ? shard_results.length : 1
      end
      shard_results
    end

    result = reorder_cross_shard_results(result) if can_order
    result.slice!(limit_value..-1) if limit_value
    result
  end
end

#can_order_cross_shard_results?Boolean

Returns:

  • (Boolean)


207
208
209
210
# File 'lib/switchman/active_record/relation.rb', line 207

def can_order_cross_shard_results?
  # we only presume to be able to post-sort the most basic of orderings
  order_values.all? { |ov| ov.is_a?(::Arel::Nodes::Ordering) && ov.expr.is_a?(::Arel::Attributes::Attribute) }
end

#cloneObject



16
17
18
19
20
# File 'lib/switchman/active_record/relation.rb', line 16

def clone
  result = super
  result.shard_value = Shard.current(klass ? klass.connection_class_for_self : :primary) unless shard_value
  result
end

#createObject



35
36
37
# File 'lib/switchman/active_record/relation.rb', line 35

def create(*, &)
  primary_shard.activate(klass.connection_class_for_self) { super }
end

#create!Object



39
40
41
# File 'lib/switchman/active_record/relation.rb', line 39

def create!(*, &)
  primary_shard.activate(klass.connection_class_for_self) { super }
end

#explain(*args) ⇒ Object



53
54
55
# File 'lib/switchman/active_record/relation.rb', line 53

def explain(*args)
  activate { |relation| relation.call_super(:explain, Relation, *args) }
end

#find_ids_in_ranges(options = {}) ⇒ Object



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
# File 'lib/switchman/active_record/relation.rb', line 129

def find_ids_in_ranges(options = {})
  is_integer = columns_hash[primary_key.to_s].type == :integer
  loose_mode = options[:loose] && is_integer
  # loose_mode: if we don't care about getting exactly batch_size ids in between
  # don't get the max - just get the min and add batch_size so we get that many _at most_
  values = loose_mode ? "MIN(id)" : "MIN(id), MAX(id)"

  batch_size = options[:batch_size].try(:to_i) || 1000
  quoted_primary_key =
    "#{klass.connection.quote_local_table_name(table_name)}.#{klass.connection.quote_column_name(primary_key)}"
  as_id = " AS id" unless primary_key == "id"
  subquery_scope = except(:select)
                   .select("#{quoted_primary_key}#{as_id}")
                   .reorder(primary_key.to_sym)
                   .limit(loose_mode ? 1 : batch_size)
  subquery_scope = subquery_scope.where("#{quoted_primary_key} <= ?", options[:end_at]) if options[:end_at]

  first_subquery_scope = if options[:start_at]
                           subquery_scope.where("#{quoted_primary_key} >= ?",
                                                options[:start_at])
                         else
                           subquery_scope
                         end

  ids = connection.select_rows("SELECT #{values} FROM (#{first_subquery_scope.to_sql}) AS subquery").first

  while ids.first.present?
    ids.map!(&:to_i) if is_integer
    ids << (ids.first + batch_size) if loose_mode

    yield(*ids)
    last_value = ids.last
    next_subquery_scope = subquery_scope.where(["#{quoted_primary_key}>?", last_value])
    ids = connection.select_rows("SELECT #{values} FROM (#{next_subquery_scope.to_sql}) AS subquery").first
  end
end

#initializeObject



10
11
12
13
14
# File 'lib/switchman/active_record/relation.rb', line 10

def initialize(*, **)
  super
  self.shard_value = Shard.current(klass ? klass.connection_class_for_self : :primary) unless shard_value
  self.shard_source_value = :implicit unless shard_source_value
end

#load(&block) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/switchman/active_record/relation.rb', line 57

def load(&block)
  if !loaded? || scheduled?
    @records = activate { |relation| relation.send(:exec_queries, &block) }
    @loaded = true
  end

  self
end

#mergeObject



22
23
24
25
26
27
28
29
# File 'lib/switchman/active_record/relation.rb', line 22

def merge(*)
  relation = super
  if relation.shard_value != shard_value && relation.shard_source_value == :implicit
    relation.shard_value = shard_value
    relation.shard_source_value = shard_source_value
  end
  relation
end

#newObject



31
32
33
# File 'lib/switchman/active_record/relation.rb', line 31

def new(*, &)
  primary_shard.activate(klass.connection_class_for_self) { super }
end

#reorder_cross_shard_results(results) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/switchman/active_record/relation.rb', line 212

def reorder_cross_shard_results(results)
  results.sort! do |l, r|
    result = 0
    order_values.each do |ov|
      if l.is_a?(::ActiveRecord::Base)
        a = l.attribute(ov.expr.name)
        b = r.attribute(ov.expr.name)
      else
        a, b = l, r
      end
      next if a == b

      if a.nil? || b.nil?
        result = 1 if a.nil?
        result *= -1 if ov.is_a?(::Arel::Nodes::Descending)
      else
        result = a <=> b
      end

      result *= -1 if ov.is_a?(::Arel::Nodes::Descending)
      break unless result.zero?
    end
    result
  end
end

#to_sqlObject



43
44
45
# File 'lib/switchman/active_record/relation.rb', line 43

def to_sql
  primary_shard.activate(klass.connection_class_for_self) { super }
end

#transactionObject



48
49
50
# File 'lib/switchman/active_record/relation.rb', line 48

def transaction(...)
  primary_shard.activate(klass.connection_class_for_self) { super }
end