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 2196
2196:         def initialize(dataset)
2197:           opts = dataset.opts
2198:           eager_graph = opts[:eager_graph]
2199:           @master =  eager_graph[:master]
2200:           requirements = eager_graph[:requirements]
2201:           reflection_map = @reflection_map = eager_graph[:reflections]
2202:           reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
2203:           @unique = eager_graph[:cartesian_product_number] > 1
2204:       
2205:           alias_map = @alias_map = {}
2206:           type_map = @type_map = {}
2207:           after_load_map = @after_load_map = {}
2208:           limit_map = @limit_map = {}
2209:           reflection_map.each do |k, v|
2210:             alias_map[k] = v[:name]
2211:             type_map[k] = v.returns_array?
2212:             after_load_map[k] = v[:after_load] unless v[:after_load].empty?
2213:             limit_map[k] = v.limit_and_offset if v[:limit]
2214:           end
2215: 
2216:           # Make dependency map hash out of requirements array for each association.
2217:           # This builds a tree of dependencies that will be used for recursion
2218:           # to ensure that all parts of the object graph are loaded into the
2219:           # appropriate subordinate association.
2220:           @dependency_map = {}
2221:           # Sort the associations by requirements length, so that
2222:           # requirements are added to the dependency hash before their
2223:           # dependencies.
2224:           requirements.sort_by{|a| a[1].length}.each do |ta, deps|
2225:             if deps.empty?
2226:               dependency_map[ta] = {}
2227:             else
2228:               deps = deps.dup
2229:               hash = dependency_map[deps.shift]
2230:               deps.each do |dep|
2231:                 hash = hash[dep]
2232:               end
2233:               hash[ta] = {}
2234:             end
2235:           end
2236:       
2237:           # This mapping is used to make sure that duplicate entries in the
2238:           # result set are mapped to a single record.  For example, using a
2239:           # single one_to_many association with 10 associated records,
2240:           # the main object column values appear in the object graph 10 times.
2241:           # We map by primary key, if available, or by the object's entire values,
2242:           # if not. The mapping must be per table, so create sub maps for each table
2243:           # alias.
2244:           records_map = {@master=>{}}
2245:           alias_map.keys.each{|ta| records_map[ta] = {}}
2246:           @records_map = records_map
2247: 
2248:           datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
2249:           column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases]
2250:           primary_keys = {}
2251:           column_maps = {}
2252:           models = {}
2253:           row_procs = {}
2254:           datasets.each do |ta, ds|
2255:             models[ta] = ds.model
2256:             primary_keys[ta] = []
2257:             column_maps[ta] = {}
2258:             row_procs[ta] = ds.row_proc
2259:           end
2260:           column_aliases.each do |col_alias, tc|
2261:             ta, column = tc
2262:             column_maps[ta][col_alias] = column
2263:           end
2264:           column_maps.each do |ta, h|
2265:             pk = models[ta].primary_key
2266:             if pk.is_a?(Array)
2267:               primary_keys[ta] = []
2268:               h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
2269:             else
2270:               h.select{|ca, c| primary_keys[ta] = ca if pk == c}
2271:             end
2272:           end
2273:           @column_maps = column_maps
2274:           @primary_keys = primary_keys
2275:           @row_procs = row_procs
2276: 
2277:           # For performance, create two special maps for the master table,
2278:           # so you can skip a hash lookup.
2279:           @master_column_map = column_maps[master]
2280:           @master_primary_keys = primary_keys[master]
2281: 
2282:           # Add a special hash mapping table alias symbols to 5 element arrays that just
2283:           # contain the data in other data structures for that table alias.  This is
2284:           # used for performance, to get all values in one hash lookup instead of
2285:           # separate hash lookups for each data structure.
2286:           ta_map = {}
2287:           alias_map.keys.each do |ta|
2288:             ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]]
2289:           end
2290:           @ta_map = ta_map
2291:         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 2295
2295:         def load(hashes)
2296:           master = master()
2297:       
2298:           # Assign to local variables for speed increase
2299:           rp = row_procs[master]
2300:           rm = records_map[master]
2301:           dm = dependency_map
2302: 
2303:           # This will hold the final record set that we will be replacing the object graph with.
2304:           records = []
2305: 
2306:           hashes.each do |h|
2307:             unless key = master_pk(h)
2308:               key = hkey(master_hfor(h))
2309:             end
2310:             unless primary_record = rm[key]
2311:               primary_record = rm[key] = rp.call(master_hfor(h))
2312:               # Only add it to the list of records to return if it is a new record
2313:               records.push(primary_record)
2314:             end
2315:             # Build all associations for the current object and it's dependencies
2316:             _load(dm, primary_record, h)
2317:           end
2318:       
2319:           # Remove duplicate records from all associations if this graph could possibly be a cartesian product
2320:           # Run after_load procs if there are any
2321:           post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
2322: 
2323:           records
2324:         end

[Validate]