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.
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). |
Initialize all of the data structures used during loading.
# File lib/sequel/model/associations.rb, line 2941 2941: def initialize(dataset) 2942: opts = dataset.opts 2943: eager_graph = opts[:eager_graph] 2944: @master = eager_graph[:master] 2945: requirements = eager_graph[:requirements] 2946: reflection_map = @reflection_map = eager_graph[:reflections] 2947: reciprocal_map = @reciprocal_map = eager_graph[:reciprocals] 2948: limit_map = @limit_map = eager_graph[:limits] 2949: @unique = eager_graph[:cartesian_product_number] > 1 2950: 2951: alias_map = @alias_map = {} 2952: type_map = @type_map = {} 2953: after_load_map = @after_load_map = {} 2954: reflection_map.each do |k, v| 2955: alias_map[k] = v[:name] 2956: after_load_map[k] = v[:after_load] unless v[:after_load].empty? 2957: type_map[k] = if v.returns_array? 2958: true 2959: elsif (limit_and_offset = limit_map[k]) && !limit_and_offset.last.nil? 2960: :offset 2961: end 2962: end 2963: 2964: # Make dependency map hash out of requirements array for each association. 2965: # This builds a tree of dependencies that will be used for recursion 2966: # to ensure that all parts of the object graph are loaded into the 2967: # appropriate subordinate association. 2968: @dependency_map = {} 2969: # Sort the associations by requirements length, so that 2970: # requirements are added to the dependency hash before their 2971: # dependencies. 2972: requirements.sort_by{|a| a[1].length}.each do |ta, deps| 2973: if deps.empty? 2974: dependency_map[ta] = {} 2975: else 2976: deps = deps.dup 2977: hash = dependency_map[deps.shift] 2978: deps.each do |dep| 2979: hash = hash[dep] 2980: end 2981: hash[ta] = {} 2982: end 2983: end 2984: 2985: # This mapping is used to make sure that duplicate entries in the 2986: # result set are mapped to a single record. For example, using a 2987: # single one_to_many association with 10 associated records, 2988: # the main object column values appear in the object graph 10 times. 2989: # We map by primary key, if available, or by the object's entire values, 2990: # if not. The mapping must be per table, so create sub maps for each table 2991: # alias. 2992: records_map = {@master=>{}} 2993: alias_map.keys.each{|ta| records_map[ta] = {}} 2994: @records_map = records_map 2995: 2996: datasets = opts[:graph][:table_aliases].to_a.reject{|ta,ds| ds.nil?} 2997: column_aliases = opts[:graph_aliases] || opts[:graph][:column_aliases] 2998: primary_keys = {} 2999: column_maps = {} 3000: models = {} 3001: row_procs = {} 3002: datasets.each do |ta, ds| 3003: models[ta] = ds.model 3004: primary_keys[ta] = [] 3005: column_maps[ta] = {} 3006: row_procs[ta] = ds.row_proc 3007: end 3008: column_aliases.each do |col_alias, tc| 3009: ta, column = tc 3010: column_maps[ta][col_alias] = column 3011: end 3012: column_maps.each do |ta, h| 3013: pk = models[ta].primary_key 3014: if pk.is_a?(Array) 3015: primary_keys[ta] = [] 3016: h.select{|ca, c| primary_keys[ta] << ca if pk.include?(c)} 3017: else 3018: h.select{|ca, c| primary_keys[ta] = ca if pk == c} 3019: end 3020: end 3021: @column_maps = column_maps 3022: @primary_keys = primary_keys 3023: @row_procs = row_procs 3024: 3025: # For performance, create two special maps for the master table, 3026: # so you can skip a hash lookup. 3027: @master_column_map = column_maps[master] 3028: @master_primary_keys = primary_keys[master] 3029: 3030: # Add a special hash mapping table alias symbols to 5 element arrays that just 3031: # contain the data in other data structures for that table alias. This is 3032: # used for performance, to get all values in one hash lookup instead of 3033: # separate hash lookups for each data structure. 3034: ta_map = {} 3035: alias_map.keys.each do |ta| 3036: ta_map[ta] = [records_map[ta], row_procs[ta], alias_map[ta], type_map[ta], reciprocal_map[ta]] 3037: end 3038: @ta_map = ta_map 3039: end
Return an array of primary model instances with the associations cache prepopulated for all model objects (both primary and associated).
# File lib/sequel/model/associations.rb, line 3043 3043: def load(hashes) 3044: master = master() 3045: 3046: # Assign to local variables for speed increase 3047: rp = row_procs[master] 3048: rm = records_map[master] 3049: dm = dependency_map 3050: 3051: # This will hold the final record set that we will be replacing the object graph with. 3052: records = [] 3053: 3054: hashes.each do |h| 3055: unless key = master_pk(h) 3056: key = hkey(master_hfor(h)) 3057: end 3058: unless primary_record = rm[key] 3059: primary_record = rm[key] = rp.call(master_hfor(h)) 3060: # Only add it to the list of records to return if it is a new record 3061: records.push(primary_record) 3062: end 3063: # Build all associations for the current object and it's dependencies 3064: _load(dm, primary_record, h) 3065: end 3066: 3067: # Remove duplicate records from all associations if this graph could possibly be a cartesian product 3068: # Run after_load procs if there are any 3069: post_process(records, dm) if @unique || !after_load_map.empty? || !limit_map.empty? 3070: 3071: records 3072: end