Class Sequel::Model::Associations::EagerGraphLoader
In: lib/sequel/model/associations.rb
Parent: Object

This class is the internal implementation of eager_graph. It is responsible for taking an array of plain hashes and returning an array of model objects with all eager_graphed associations already set in the association cache.

Methods

load   new  

Attributes

after_load_map  [R]  Hash with table alias symbol keys and after_load hook values
alias_map  [R]  Hash with table alias symbol keys and association name values
column_maps  [R]  Hash with table alias symbol keys and subhash values mapping column_alias symbols to the symbol of the real name of the column
dependency_map  [R]  Recursive hash with table alias symbol keys mapping to hashes with dependent table alias symbol keys.
limit_map  [R]  Hash with table alias symbol keys and [limit, offset] values
master  [R]  Hash with table alias symbol keys and callable values used to create model instances The table alias symbol for the primary model
primary_keys  [R]  Hash with table alias symbol keys and primary key symbol values (or arrays of primary key symbols for composite key tables)
reciprocal_map  [R]  Hash with table alias symbol keys and reciprocal association symbol values, used for setting reciprocals for one_to_many associations.
records_map  [R]  Hash with table alias symbol keys and subhash values mapping primary key symbols (or array of symbols) to model instances. Used so that only a single model instance is created for each object.
reflection_map  [R]  Hash with table alias symbol keys and AssociationReflection values
row_procs  [R]  Hash with table alias symbol keys and callable values used to create model instances
type_map  [R]  Hash with table alias symbol keys and true/false values, where true means the association represented by the table alias uses an array of values instead of a single value (i.e. true => *_many, false => *_to_one).

Public Class methods

Initialize all of the data structures used during loading.

[Source]

      # File lib/sequel/model/associations.rb, line 2077
2077:         def initialize(dataset)
2078:           opts = dataset.opts
2079:           eager_graph = opts[:eager_graph]
2080:           @master =  eager_graph[:master]
2081:           requirements = eager_graph[:requirements]
2082:           reflection_map = @reflection_map = eager_graph[:reflections]
2083:           reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
2084:           @unique = eager_graph[:cartesian_product_number] > 1
2085:       
2086:           alias_map = @alias_map = {}
2087:           type_map = @type_map = {}
2088:           after_load_map = @after_load_map = {}
2089:           limit_map = @limit_map = {}
2090:           reflection_map.each do |k, v|
2091:             alias_map[k] = v[:name]
2092:             type_map[k] = v.returns_array?
2093:             after_load_map[k] = v[:after_load] unless v[:after_load].empty?
2094:             limit_map[k] = v.limit_and_offset if v[:limit]
2095:           end
2096: 
2097:           # Make dependency map hash out of requirements array for each association.
2098:           # This builds a tree of dependencies that will be used for recursion
2099:           # to ensure that all parts of the object graph are loaded into the
2100:           # appropriate subordinate association.
2101:           @dependency_map = {}
2102:           # Sort the associations by requirements length, so that
2103:           # requirements are added to the dependency hash before their
2104:           # dependencies.
2105:           requirements.sort_by{|a| a[1].length}.each do |ta, deps|
2106:             if deps.empty?
2107:               dependency_map[ta] = {}
2108:             else
2109:               deps = deps.dup
2110:               hash = dependency_map[deps.shift]
2111:               deps.each do |dep|
2112:                 hash = hash[dep]
2113:               end
2114:               hash[ta] = {}
2115:             end
2116:           end
2117:       
2118:           # This mapping is used to make sure that duplicate entries in the
2119:           # result set are mapped to a single record.  For example, using a
2120:           # single one_to_many association with 10 associated records,
2121:           # the main object column values appear in the object graph 10 times.
2122:           # We map by primary key, if available, or by the object's entire values,
2123:           # if not. The mapping must be per table, so create sub maps for each table
2124:           # alias.
2125:           records_map = {@master=>{}}
2126:           alias_map.keys.each{|ta| records_map[ta] = {}}
2127:           @records_map = records_map
2128: 
2129:           datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
2130:           column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases]
2131:           primary_keys = {}
2132:           column_maps = {}
2133:           models = {}
2134:           row_procs = {}
2135:           datasets.each do |ta, ds|
2136:             models[ta] = ds.model
2137:             primary_keys[ta] = []
2138:             column_maps[ta] = {}
2139:             row_procs[ta] = ds.row_proc
2140:           end
2141:           column_aliases.each do |col_alias, tc|
2142:             ta, column = tc
2143:             column_maps[ta][col_alias] = column
2144:           end
2145:           column_maps.each do |ta, h|
2146:             pk = models[ta].primary_key
2147:             if pk.is_a?(Array)
2148:               primary_keys[ta] = []
2149:               h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
2150:             else
2151:               h.select{|ca, c| primary_keys[ta] = ca if pk == c}
2152:             end
2153:           end
2154:           @column_maps = column_maps
2155:           @primary_keys = primary_keys
2156:           @row_procs = row_procs
2157: 
2158:           # For performance, create two special maps for the master table,
2159:           # so you can skip a hash lookup.
2160:           @master_column_map = column_maps[master]
2161:           @master_primary_keys = primary_keys[master]
2162: 
2163:           # Add a special hash mapping table alias symbols to 5 element arrays that just
2164:           # contain the data in other data structures for that table alias.  This is
2165:           # used for performance, to get all values in one hash lookup instead of
2166:           # separate hash lookups for each data structure.
2167:           ta_map = {}
2168:           alias_map.keys.each do |ta|
2169:             ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]]
2170:           end
2171:           @ta_map = ta_map
2172:         end

Public Instance methods

Return an array of primary model instances with the associations cache prepopulated for all model objects (both primary and associated).

[Source]

      # File lib/sequel/model/associations.rb, line 2176
2176:         def load(hashes)
2177:           master = master()
2178:       
2179:           # Assign to local variables for speed increase
2180:           rp = row_procs[master]
2181:           rm = records_map[master]
2182:           dm = dependency_map
2183: 
2184:           # This will hold the final record set that we will be replacing the object graph with.
2185:           records = []
2186: 
2187:           hashes.each do |h|
2188:             unless key = master_pk(h)
2189:               key = hkey(master_hfor(h))
2190:             end
2191:             unless primary_record = rm[key]
2192:               primary_record = rm[key] = rp.call(master_hfor(h))
2193:               # Only add it to the list of records to return if it is a new record
2194:               records.push(primary_record)
2195:             end
2196:             # Build all associations for the current object and it's dependencies
2197:             _load(dm, primary_record, h)
2198:           end
2199:       
2200:           # Remove duplicate records from all associations if this graph could possibly be a cartesian product
2201:           # Run after_load procs if there are any
2202:           post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
2203: 
2204:           records
2205:         end

[Validate]