Class: NoSE::Plans::ExecutionPlans

Inherits:
Object
  • Object
show all
Includes:
Loader
Defined in:
lib/nose/plans/execution_plan.rb

Overview

Simple DSL for constructing execution plans

Constant Summary collapse

LOAD_PATH =

The subdirectory execution plans are loaded from

'plans'

Instance Attribute Summary collapse

Attributes included from Loader

#source_code

Instance Method Summary collapse

Methods included from Loader

included

Constructor Details

#initialize(&block) ⇒ ExecutionPlans

Returns a new instance of ExecutionPlans.



13
14
15
16
17
18
19
20
21
22
# File 'lib/nose/plans/execution_plan.rb', line 13

def initialize(&block)
  @groups = Hash.new { |h, k| h[k] = [] }
  @weights = Hash.new { |h, k| h[k] = {} }
  @mix = :default

  instance_eval(&block) if block_given?

  # Reset the mix to force weight assignment
  self.mix = @mix
end

Instance Attribute Details

#groupsObject (readonly)

Returns the value of attribute groups.



11
12
13
# File 'lib/nose/plans/execution_plan.rb', line 11

def groups
  @groups
end

#mixObject

Returns the value of attribute mix.



11
12
13
# File 'lib/nose/plans/execution_plan.rb', line 11

def mix
  @mix
end

#schemaObject (readonly)

Returns the value of attribute schema.



11
12
13
# File 'lib/nose/plans/execution_plan.rb', line 11

def schema
  @schema
end

#weightsObject (readonly)

Returns the value of attribute weights.



11
12
13
# File 'lib/nose/plans/execution_plan.rb', line 11

def weights
  @weights
end

Instance Method Details

#calculate_cost(cost_model) ⇒ void

This method returns an undefined value.

Populate the cost of each plan



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nose/plans/execution_plan.rb', line 26

def calculate_cost(cost_model)
  @groups.each_value do |plans|
    plans.each do |plan|
      plan.steps.each do |step|
        cost = cost_model.index_lookup_cost step
        step.instance_variable_set(:@cost, cost)
      end

      plan.query_plans.each do |query_plan|
        query_plan.steps.each do |step|
          cost = cost_model.index_lookup_cost step
          step.instance_variable_set(:@cost, cost)
        end
      end

      # XXX Only bother with insert statements for now
      plan.update_steps.each do |step|
        cost = cost_model.insert_cost step
        step.instance_variable_set(:@cost, cost)
      end
    end
  end
end

#DefaultMix(mix) ⇒ void

This method returns an undefined value.

Set the default mix for these plans



77
78
79
# File 'lib/nose/plans/execution_plan.rb', line 77

def DefaultMix(mix)
  self.mix = mix
end

#Group(name, weight = 1.0, **mixes, &block) ⇒ void

This method returns an undefined value.

Define a group of query execution plans



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nose/plans/execution_plan.rb', line 83

def Group(name, weight = 1.0, **mixes, &block)
  @group = name

  # Save the weights
  if mixes.empty?
    @weights[name][:default] = weight
  else
    @weights[name] = mixes
  end

  instance_eval(&block) if block_given?
end

#Plan(name, &block) ⇒ void

This method returns an undefined value.

Define a single plan within a group



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nose/plans/execution_plan.rb', line 98

def Plan(name, &block)
  return unless block_given?

  plan = QueryExecutionPlan.new(@group, name, @schema, self)

  # Capture one level of nesting in plans
  if @parent_plan.nil?
    @parent_plan = plan if @parent_plan.nil?
    set_parent = true
  else
    set_parent = false
  end

  plan.instance_eval(&block)

  # Reset the parent plan if it was set
  @parent_plan = nil if set_parent

  @groups[@group] << plan
end

#Schema(name) ⇒ void

This method returns an undefined value.

Set the schema to be used by the execution plans



69
70
71
72
73
# File 'lib/nose/plans/execution_plan.rb', line 69

def Schema(name)
  @schema = Schema.load name
  NoSE::DSL.mixin_fields @schema.model.entities, QueryExecutionPlan
  NoSE::DSL.mixin_fields @schema.model.entities, ExecutionPlans
end

#Support(&block) ⇒ void

This method returns an undefined value.

Add support queries for updates in a plan



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/nose/plans/execution_plan.rb', line 121

def Support(&block)
  # XXX Hack to swap the group name and capture support plans
  old_group = @group
  @group = '__SUPPORT__'
  instance_eval(&block) if block_given?

  @parent_plan.query_plans = @groups[@group]
  @parent_plan.query_plans.each do |plan|
    plan.instance_variable_set(:@group, old_group)
  end

  @groups[@group] = []

  @group = old_group
end