|
@@ 29-50 (lines=22) @@
|
| 26 |
|
|
| 27 |
|
include Comparable |
| 28 |
|
|
| 29 |
|
def initialize(statement, index, trees, update_steps, cost_model) |
| 30 |
|
@statement = statement |
| 31 |
|
@index = index |
| 32 |
|
@trees = trees |
| 33 |
|
@query_plans = nil # these will be set later when we pick indexes |
| 34 |
|
update_steps.each { |step| step.calculate_cost cost_model } |
| 35 |
|
@update_steps = update_steps |
| 36 |
|
@cost_model = cost_model |
| 37 |
|
|
| 38 |
|
# Update with fields specified in the settings and conditions |
| 39 |
|
# (rewrite from foreign keys to IDs if needed) |
| 40 |
|
@update_fields = if statement.is_a?(Connection) || |
| 41 |
|
statement.is_a?(Delete) |
| 42 |
|
[] |
| 43 |
|
else |
| 44 |
|
statement.settings.map(&:field) |
| 45 |
|
end |
| 46 |
|
@update_fields += statement.conditions.each_value.map(&:field) |
| 47 |
|
@update_fields.map! do |field| |
| 48 |
|
field.is_a?(Fields::ForeignKeyField) ? field.entity.id_field : field |
| 49 |
|
end |
| 50 |
|
end |
| 51 |
|
|
| 52 |
|
# The weight of this query for a given workload |
| 53 |
|
# @return [Fixnum] |
|
@@ 188-208 (lines=21) @@
|
| 185 |
|
|
| 186 |
|
# A planner for update statements in the workload |
| 187 |
|
class UpdatePlanner |
| 188 |
|
def initialize(model, trees, cost_model, by_id_graph = false) |
| 189 |
|
@logger = Logging.logger['nose::update_planner'] |
| 190 |
|
|
| 191 |
|
@model = model |
| 192 |
|
@cost_model = cost_model |
| 193 |
|
@by_id_graph = by_id_graph |
| 194 |
|
|
| 195 |
|
# Remove anything not a support query then group by statement and index |
| 196 |
|
@query_plans = trees.select do |tree| |
| 197 |
|
tree.query.is_a? SupportQuery |
| 198 |
|
end |
| 199 |
|
@query_plans = @query_plans.group_by { |tree| tree.query.statement } |
| 200 |
|
@query_plans.each do |plan_stmt, plan_trees| |
| 201 |
|
@query_plans[plan_stmt] = plan_trees.group_by do |tree| |
| 202 |
|
index = tree.query.index |
| 203 |
|
index = index.to_id_path if @by_id_path |
| 204 |
|
|
| 205 |
|
index |
| 206 |
|
end |
| 207 |
|
end |
| 208 |
|
end |
| 209 |
|
|
| 210 |
|
# Find the necessary update plans for a given set of indexes |
| 211 |
|
# @return [Array<UpdatePlan>] |