Module Sequel::Model::Associations::EagerLoading
In: lib/sequel_model/eager_loading.rb

Eager loading makes it so that you can load all associated records for a set of objects in a single query, instead of a separate query for each object.

Two separate implementations are provided. eager should be used most of the time, as it loads associated records using one query per association. However, it does not allow you the ability to filter based on columns in associated tables. eager_graph loads all records in one query. Using eager_graph you can filter based on columns in associated tables. However, eager_graph can be much slower than eager, especially if multiple *_to_many associations are joined.

You can cascade the eager loading (loading associations’ associations) with no limit to the depth of the cascades. You do this by passing a hash to eager or eager_graph with the keys being associations of the current model and values being associations of the model associated with the current model via the key.

The arguments can be symbols or hashes with symbol keys (for cascaded eager loading). Examples:

  Album.eager(:artist).all
  Album.eager_graph(:artist).all
  Album.eager(:artist, :genre).all
  Album.eager_graph(:artist, :genre).all
  Album.eager(:artist).eager(:genre).all
  Album.eager_graph(:artist).eager(:genre).all
  Artist.eager(:albums=>:tracks).all
  Artist.eager_graph(:albums=>:tracks).all
  Artist.eager(:albums=>{:tracks=>:genre}).all
  Artist.eager_graph(:albums=>{:tracks=>:genre}).all

Methods

Public Class methods

Add the eager! and eager_graph! mutation methods to the dataset.

[Source]

    # File lib/sequel_model/eager_loading.rb, line 31
31:   def self.extended(obj)
32:     obj.def_mutation_method(:eager, :eager_graph)
33:   end

Public Instance methods

The preferred eager loading method. Loads all associated records using one query for each association.

The basic idea for how it works is that the dataset is first loaded normally. Then it goes through all associations that have been specified via eager. It loads each of those associations separately, then associates them back to the original dataset via primary/foreign keys. Due to the necessity of all objects being present, you need to use .all to use eager loading, as it can‘t work with .each.

This implementation avoids the complexity of extracting an object graph out of a single dataset, by building the object graph out of multiple datasets, one for each association. By using a separate dataset for each association, it avoids problems such as aliasing conflicts and creating cartesian product result sets if multiple *_to_many eager associations are requested.

One limitation of using this method is that you cannot filter the dataset based on values of columns in an associated table, since the associations are loaded in separate queries. To do that you need to load all associations in the same query, and extract an object graph from the results of that query. If you need to filter based on columns in associated tables, look at eager_graph or join the tables you need to filter on manually.

Each association‘s order, if defined, is respected. Eager also works on a limited dataset, but does not use any :limit options for associations. If the association uses a block or has an :eager_block argument, it is used.

[Source]

    # File lib/sequel_model/eager_loading.rb, line 61
61:   def eager(*associations)
62:     model = check_model
63:     opt = @opts[:eager]
64:     opt = opt ? opt.dup : {}
65:     associations.flatten.each do |association|
66:       case association
67:         when Symbol
68:           check_association(model, association)
69:           opt[association] = nil
70:         when Hash
71:           association.keys.each{|assoc| check_association(model, assoc)}
72:           opt.merge!(association)
73:         else raise(Sequel::Error, 'Associations must be in the form of a symbol or hash')
74:       end
75:     end
76:     clone(:eager=>opt)
77:   end

The secondary eager loading method. Loads all associations in a single query. This method should only be used if you need to filter based on columns in associated tables.

This method builds an object graph using Dataset#graph. Then it uses the graph to build the associations, and finally replaces the graph with a simple array of model objects.

Be very careful when using this with multiple *_to_many associations, as you can create large cartesian products. If you must graph multiple *_to_many associations, make sure your filters are specific if you have a large database.

Each association‘s order, if definied, is respected. eager_graph probably won‘t work correctly on a limited dataset, unless you are only graphing many_to_one associations.

Does not use the block defined for the association, since it does a single query for all objects. You can use the :graph_join_type, :graph_conditions, and :graph_join_table_conditions association options to modify the SQL query.

[Source]

     # File lib/sequel_model/eager_loading.rb, line 97
 97:   def eager_graph(*associations)
 98:     model = check_model
 99:     table_name = model.table_name
100:     ds = if @opts[:eager_graph]
101:       self
102:     else
103:       # Each of the following have a symbol key for the table alias, with the following values: 
104:       # :reciprocals - the reciprocal instance variable to use for this association
105:       # :requirements - array of requirements for this association
106:       # :alias_association_type_map - the type of association for this association
107:       # :alias_association_name_map - the name of the association for this association
108:       clone(:eager_graph=>{:requirements=>{}, :master=>model.table_name, :alias_association_type_map=>{}, :alias_association_name_map=>{}, :reciprocals=>{}})
109:     end
110:     ds.eager_graph_associations(ds, model, table_name, [], *associations)
111:   end

Protected Instance methods

Call graph on the association with the correct arguments, update the eager_graph data structure, and recurse into eager_graph_associations if there are any passed in associations (which would be dependencies of the current association)

Arguments:

  • ds - Current dataset
  • model - Current Model
  • ta - table_alias used for the parent association
  • requirements - an array, used as a stack for requirements
  • r - association reflection for the current association
  • *associations - any associations dependent on this one

[Source]

     # File lib/sequel_model/eager_loading.rb, line 127
127:   def eager_graph_association(ds, model, ta, requirements, r, *associations)
128:     klass = r.associated_class
129:     assoc_name = r[:name]
130:     assoc_table_alias = ds.eager_unique_table_alias(ds, assoc_name)
131:     ds = r[:eager_grapher].call(ds, assoc_table_alias, ta)
132:     ds = ds.order_more(*Array(r[:order]).map{|c| eager_graph_qualify_order(assoc_table_alias, c)}) if r[:order] and r[:order_eager_graph]
133:     eager_graph = ds.opts[:eager_graph]
134:     eager_graph[:requirements][assoc_table_alias] = requirements.dup
135:     eager_graph[:alias_association_name_map][assoc_table_alias] = assoc_name
136:     eager_graph[:alias_association_type_map][assoc_table_alias] = r.returns_array?
137:     ds = ds.eager_graph_associations(ds, r.associated_class, assoc_table_alias, requirements + [assoc_table_alias], *associations) unless associations.empty?
138:     ds
139:   end

Check the associations are valid for the given model. Call eager_graph_association on each association.

Arguments:

  • ds - Current dataset
  • model - Current Model
  • ta - table_alias used for the parent association
  • requirements - an array, used as a stack for requirements
  • *associations - the associations to add to the graph

[Source]

     # File lib/sequel_model/eager_loading.rb, line 150
150:   def eager_graph_associations(ds, model, ta, requirements, *associations)
151:     return ds if associations.empty?
152:     associations.flatten.each do |association|
153:       ds = case association
154:       when Symbol
155:         ds.eager_graph_association(ds, model, ta, requirements, check_association(model, association))
156:       when Hash
157:         association.each do |assoc, assoc_assocs|
158:           ds = ds.eager_graph_association(ds, model, ta, requirements, check_association(model, assoc), assoc_assocs)
159:         end
160:         ds
161:       else raise(Sequel::Error, 'Associations must be in the form of a symbol or hash')
162:       end
163:     end
164:     ds
165:   end

Build associations out of the array of returned object graphs.

[Source]

     # File lib/sequel_model/eager_loading.rb, line 168
168:   def eager_graph_build_associations(record_graphs)
169:     eager_graph = @opts[:eager_graph]
170:     master = eager_graph[:master]
171:     requirements = eager_graph[:requirements]
172:     alias_map = eager_graph[:alias_association_name_map]
173:     type_map = eager_graph[:alias_association_type_map]
174:     reciprocal_map = eager_graph[:reciprocals]
175: 
176:     # Make dependency map hash out of requirements array for each association.
177:     # This builds a tree of dependencies that will be used for recursion
178:     # to ensure that all parts of the object graph are loaded into the
179:     # appropriate subordinate association.
180:     dependency_map = {}
181:     # Sort the associations be requirements length, so that
182:     # requirements are added to the dependency hash before their
183:     # dependencies.
184:     requirements.sort_by{|a| a[1].length}.each do |ta, deps|
185:       if deps.empty?
186:         dependency_map[ta] = {}
187:       else
188:         deps = deps.dup
189:         hash = dependency_map[deps.shift]
190:         deps.each do |dep|
191:           hash = hash[dep]
192:         end
193:         hash[ta] = {}
194:       end
195:     end
196: 
197:     # This mapping is used to make sure that duplicate entries in the
198:     # result set are mapped to a single record.  For example, using a
199:     # single one_to_many association with 10 associated records,
200:     # the main object will appear in the object graph 10 times.
201:     # We map by primary key, if available, or by the object's entire values,
202:     # if not. The mapping must be per table, so create sub maps for each table
203:     # alias.
204:     records_map = {master=>{}}
205:     alias_map.keys.each{|ta| records_map[ta] = {}}
206: 
207:     # This will hold the final record set that we will be replacing the object graph with.
208:     records = []
209:     record_graphs.each do |record_graph|
210:       primary_record = record_graph[master]
211:       key = primary_record.pk || primary_record.values.sort_by{|x| x[0].to_s}
212:       if cached_pr = records_map[master][key]
213:         primary_record = cached_pr
214:       else
215:         records_map[master][key] = primary_record
216:         # Only add it to the list of records to return if it is a new record
217:         records.push(primary_record)
218:       end
219:       # Build all associations for the current object and it's dependencies
220:       eager_graph_build_associations_graph(dependency_map, alias_map, type_map, reciprocal_map, records_map, primary_record, record_graph)
221:     end
222: 
223:     # Remove duplicate records from all associations if this graph could possibly be a cartesian product
224:     eager_graph_make_associations_unique(records, dependency_map, alias_map, type_map) if type_map.values.select{|v| v}.length > 1
225:     
226:     # Replace the array of object graphs with an array of model objects
227:     record_graphs.replace(records)
228:   end

Creates a unique table alias that hasn‘t already been used in the query. Will either be the table_alias itself or table_alias_N for some integer N (starting at 0 and increasing until an unused one is found).

[Source]

     # File lib/sequel_model/eager_loading.rb, line 233
233:   def eager_unique_table_alias(ds, table_alias)
234:     used_aliases = ds.opts[:from]
235:     graph = ds.opts[:graph]
236:     used_aliases += graph[:table_aliases].keys if graph
237:     if used_aliases.include?(table_alias)
238:       i = 0
239:       loop do
240:         ta = "#{table_alias}_#{i}""#{table_alias}_#{i}"
241:         return ta unless used_aliases.include?(ta)
242:         i += 1
243:       end
244:     end
245:     table_alias
246:   end

[Validate]