Module: Switchman::ActiveRecord::Preloader::Association

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

Instance Method Summary collapse

Instance Method Details

#associated_records_by_owner(preloader = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
# File 'lib/switchman/active_record/association.rb', line 88

def associated_records_by_owner(preloader = nil)
  owners_map = owners_by_key

  if klass.nil? || owners_map.empty?
    records = []
  else
    # determine the shard to search for each owner
    if reflection.macro == :belongs_to
      # for belongs_to, it's the shard of the foreign_key
      partition_proc = ->(owner) do
        if owner.class.sharded_column?(owner_key_name)
          Shard.shard_for(owner[owner_key_name], owner.shard)
        else
          Shard.current
        end
      end
    elsif !reflection.options[:multishard]
      # for non-multishard associations, it's *just* the owner's shard
      partition_proc = ->(owner) { owner.shard }
    else
      # for multishard associations, it's the owner object itself
      # (all associated shards)

      # this is the default behavior of partition_by_shard, so just let it be nil
      # to avoid the proc call
      # partition_proc = ->(owner) { owner }
    end

    records = Shard.partition_by_shard(owners, partition_proc) do |partitioned_owners|
      # Some databases impose a limit on the number of ids in a list (in Oracle it's 1000)
      # Make several smaller queries if necessary or make one query if the adapter supports it
      sliced_owners = partitioned_owners.each_slice(model.connection.in_clause_length || partitioned_owners.size)
      sliced_owners.map do |slice|
        relative_owner_keys = slice.map do |owner|
          key = owner[owner_key_name]
          if key && owner.class.sharded_column?(owner_key_name)
            key = Shard.relative_id_for(key, owner.shard, Shard.current(owner.class.shard_category))
          end
          key && key.to_s
        end
        relative_owner_keys.compact!
        relative_owner_keys.uniq!
        records_for(relative_owner_keys)
      end
    end
    records.flatten!
  end

  @preloaded_records = records

  # Each record may have multiple owners, and vice-versa
  records_by_owner = owners.each_with_object({}) do |owner,h|
    h[owner] = []
  end
  records.each do |record|
    owner_key = record[association_key_name]
    owner_key = Shard.global_id_for(owner_key, record.shard) if owner_key && record.class.sharded_column?(association_key_name)

    owners_map[owner_key.to_s].each do |owner|
      owner.association(reflection.name).set_inverse_instance(record)
      records_by_owner[owner] << record
    end
  end
  records_by_owner
end

#owners_by_keyObject



154
155
156
157
158
159
160
# File 'lib/switchman/active_record/association.rb', line 154

def owners_by_key
  @owners_by_key ||= owners.group_by do |owner|
    key = owner[owner_key_name]
    key = Shard.global_id_for(key, owner.shard) if key && owner.class.sharded_column?(owner_key_name)
    key && key.to_s
  end
end

#run(preloader) ⇒ Object



80
81
82
83
84
85
# File 'lib/switchman/active_record/association.rb', line 80

def run(preloader)
  # TODO - can move associated_records_by_owner into this after 5.1 is gonzo
  associated_records_by_owner(preloader).each do |owner, records|
    associate_records_to_owner(owner, records)
  end
end

#scopeObject



162
163
164
# File 'lib/switchman/active_record/association.rb', line 162

def scope
  build_scope
end