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 3043
3043:         def initialize(dataset)
3044:           opts = dataset.opts
3045:           eager_graph = opts[:eager_graph]
3046:           @master =  eager_graph[:master]
3047:           requirements = eager_graph[:requirements]
3048:           reflection_map = @reflection_map = eager_graph[:reflections]
3049:           reciprocal_map = @reciprocal_map = eager_graph[:reciprocals]
3050:           limit_map = @limit_map = eager_graph[:limits]
3051:           @unique = eager_graph[:cartesian_product_number] > 1
3052:       
3053:           alias_map = @alias_map = {}
3054:           type_map = @type_map = {}
3055:           after_load_map = @after_load_map = {}
3056:           reflection_map.each do |k, v|
3057:             alias_map[k] = v[:name]
3058:             after_load_map[k] = v[:after_load] unless v[:after_load].empty?
3059:             type_map[k] = if v.returns_array?
3060:               true
3061:             elsif (limit_and_offset = limit_map[k]) && !limit_and_offset.last.nil?
3062:               :offset
3063:             end
3064:           end
3065: 
3066:           # Make dependency map hash out of requirements array for each association.
3067:           # This builds a tree of dependencies that will be used for recursion
3068:           # to ensure that all parts of the object graph are loaded into the
3069:           # appropriate subordinate association.
3070:           @dependency_map = {}
3071:           # Sort the associations by requirements length, so that
3072:           # requirements are added to the dependency hash before their
3073:           # dependencies.
3074:           requirements.sort_by{|a| a[1].length}.each do |ta, deps|
3075:             if deps.empty?
3076:               dependency_map[ta] = {}
3077:             else
3078:               deps = deps.dup
3079:               hash = dependency_map[deps.shift]
3080:               deps.each do |dep|
3081:                 hash = hash[dep]
3082:               end
3083:               hash[ta] = {}
3084:             end
3085:           end
3086:       
3087:           # This mapping is used to make sure that duplicate entries in the
3088:           # result set are mapped to a single record.  For example, using a
3089:           # single one_to_many association with 10 associated records,
3090:           # the main object column values appear in the object graph 10 times.
3091:           # We map by primary key, if available, or by the object's entire values,
3092:           # if not. The mapping must be per table, so create sub maps for each table
3093:           # alias.
3094:           records_map = {@master=>{}}
3095:           alias_map.keys.each{|ta| records_map[ta] = {}}
3096:           @records_map = records_map
3097: 
3098:           datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?}
3099:           column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases]
3100:           primary_keys = {}
3101:           column_maps = {}
3102:           models = {}
3103:           row_procs = {}
3104:           datasets.each do |ta, ds|
3105:             models[ta] = ds.model
3106:             primary_keys[ta] = []
3107:             column_maps[ta] = {}
3108:             row_procs[ta] = ds.row_proc
3109:           end
3110:           column_aliases.each do |col_alias, tc|
3111:             ta, column = tc
3112:             column_maps[ta][col_alias] = column
3113:           end
3114:           column_maps.each do |ta, h|
3115:             pk = models[ta].primary_key
3116:             if pk.is_a?(Array)
3117:               primary_keys[ta] = []
3118:               h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)}
3119:             else
3120:               h.select{|ca, c| primary_keys[ta] = ca if pk == c}
3121:             end
3122:           end
3123:           @column_maps = column_maps
3124:           @primary_keys = primary_keys
3125:           @row_procs = row_procs
3126: 
3127:           # For performance, create two special maps for the master table,
3128:           # so you can skip a hash lookup.
3129:           @master_column_map = column_maps[master]
3130:           @master_primary_keys = primary_keys[master]
3131: 
3132:           # Add a special hash mapping table alias symbols to 5 element arrays that just
3133:           # contain the data in other data structures for that table alias.  This is
3134:           # used for performance, to get all values in one hash lookup instead of
3135:           # separate hash lookups for each data structure.
3136:           ta_map = {}
3137:           alias_map.keys.each do |ta|
3138:             ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]]
3139:           end
3140:           @ta_map = ta_map
3141:         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 3145
3145:         def load(hashes)
3146:           master = master()
3147:       
3148:           # Assign to local variables for speed increase
3149:           rp = row_procs[master]
3150:           rm = records_map[master]
3151:           dm = dependency_map
3152: 
3153:           # This will hold the final record set that we will be replacing the object graph with.
3154:           records = []
3155: 
3156:           hashes.each do |h|
3157:             unless key = master_pk(h)
3158:               key = hkey(master_hfor(h))
3159:             end
3160:             unless primary_record = rm[key]
3161:               primary_record = rm[key] = rp.call(master_hfor(h))
3162:               # Only add it to the list of records to return if it is a new record
3163:               records.push(primary_record)
3164:             end
3165:             # Build all associations for the current object and it's dependencies
3166:             _load(dm, primary_record, h)
3167:           end
3168:       
3169:           # Remove duplicate records from all associations if this graph could possibly be a cartesian product
3170:           # Run after_load procs if there are any
3171:           post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty?
3172: 
3173:           records
3174:         end

[Validate]