Skip to content

Commit

Permalink
use the node as cache so we can avoid accessing the table
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Oct 15, 2013
1 parent c5fe508 commit 23f35b2
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions activerecord/lib/active_record/associations/join_dependency.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ class Aliases
def initialize(tables) def initialize(tables)
@tables = tables @tables = tables
@alias_cache = tables.each_with_object({}) { |table,h| @alias_cache = tables.each_with_object({}) { |table,h|
h[table.name] = table.columns.each_with_object({}) { |column,i| h[table.node] = table.columns.each_with_object({}) { |column,i|
i[column.name] = column.alias i[column.name] = column.alias
} }
} }
@name_and_alias_cache = tables.each_with_object({}) { |table,h| @name_and_alias_cache = tables.each_with_object({}) { |table,h|
h[table.name] = table.columns.map { |column| h[table.node] = table.columns.map { |column|
[column.name, column.alias] [column.name, column.alias]
} }
} }
Expand All @@ -108,17 +108,17 @@ def columns
end end


# An array of [column_name, alias] pairs for the table # An array of [column_name, alias] pairs for the table
def column_aliases(table) def column_aliases(node)
@name_and_alias_cache[table] @name_and_alias_cache[node]
end end


def column_alias(table, column) def column_alias(node, column)
@alias_cache[table][column] @alias_cache[node][column]
end end


class Table < Struct.new(:name, :alias, :columns) class Table < Struct.new(:node, :columns)
def table def table
Arel::Nodes::TableAlias.new name, self.alias Arel::Nodes::TableAlias.new node.table, node.aliased_table_name
end end


def column_aliases def column_aliases
Expand All @@ -134,12 +134,12 @@ def aliases
columns = join_part.column_names.each_with_index.map { |column_name,j| columns = join_part.column_names.each_with_index.map { |column_name,j|
Aliases::Column.new column_name, "t#{i}_r#{j}" Aliases::Column.new column_name, "t#{i}_r#{j}"
} }
Aliases::Table.new(join_part.table, join_part.aliased_table_name, columns) Aliases::Table.new(join_part, columns)
} }
end end


def instantiate(result_set, aliases) def instantiate(result_set, aliases)
primary_key = aliases.column_alias(join_root.table, join_root.primary_key) primary_key = aliases.column_alias(join_root, join_root.primary_key)
type_caster = result_set.column_type primary_key type_caster = result_set.column_type primary_key


seen = Hash.new { |h,parent_klass| seen = Hash.new { |h,parent_klass|
Expand All @@ -150,7 +150,7 @@ def instantiate(result_set, aliases)


model_cache = Hash.new { |h,klass| h[klass] = {} } model_cache = Hash.new { |h,klass| h[klass] = {} }
parents = model_cache[join_root] parents = model_cache[join_root]
column_aliases = aliases.column_aliases join_root.table column_aliases = aliases.column_aliases join_root


result_set.each { |row_hash| result_set.each { |row_hash|
primary_id = type_caster.type_cast row_hash[primary_key] primary_id = type_caster.type_cast row_hash[primary_key]
Expand Down Expand Up @@ -242,7 +242,7 @@ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)
end end
end end


key = aliases.column_alias(node.table, node.primary_key) key = aliases.column_alias(node, node.primary_key)
id = row[key] id = row[key]
next if id.nil? next if id.nil?


Expand All @@ -260,7 +260,7 @@ def construct(ar_parent, parent, row, rs, seen, model_cache, aliases)


def construct_model(record, node, row, model_cache, id, aliases) def construct_model(record, node, row, model_cache, id, aliases)
model = model_cache[node][id] ||= node.instantiate(row, model = model_cache[node][id] ||= node.instantiate(row,
aliases.column_aliases(node.table)) aliases.column_aliases(node))
other = record.association(node.reflection.name) other = record.association(node.reflection.name)


if node.reflection.collection? if node.reflection.collection?
Expand Down

0 comments on commit 23f35b2

Please sign in to comment.