Class: NoSE::Plans::SortPlanStep

Inherits:
PlanStep show all
Defined in:
lib/nose/plans/sort.rb

Overview

A query plan step performing external sort

Instance Attribute Summary collapse

Attributes inherited from PlanStep

#children, #cost, #fields, #parent, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PlanStep

#add_fields_from_index, #calculate_cost, inherited, #parent_index, #parent_steps

Methods included from Supertype

included

Constructor Details

#initialize(sort_fields, state = nil) ⇒ SortPlanStep

Returns a new instance of SortPlanStep.



9
10
11
12
13
14
# File 'lib/nose/plans/sort.rb', line 9

def initialize(sort_fields, state = nil)
  super()

  @sort_fields = sort_fields
  @state = state
end

Instance Attribute Details

#sort_fieldsObject (readonly)

Returns the value of attribute sort_fields.



7
8
9
# File 'lib/nose/plans/sort.rb', line 7

def sort_fields
  @sort_fields
end

Class Method Details

.apply(parent, state) ⇒ SortPlanStep

Check if an external sort can used (if a sort is the last step)

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/nose/plans/sort.rb', line 34

def self.apply(parent, state)
  fetched_all_ids = state.fields.none? { |f| f.is_a? Fields::IDField }
  resolved_predicates = state.eq.empty? && state.range.nil?
  can_order = !(state.order_by.to_set & parent.fields).empty?
  return nil unless fetched_all_ids && resolved_predicates && can_order

  new_state = state.dup
  new_state.order_by = []
  new_step = SortPlanStep.new(state.order_by, new_state)
  new_step.state.freeze

  new_step
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Two sorting steps are equal if they sort on the same fields



23
24
25
# File 'lib/nose/plans/sort.rb', line 23

def ==(other)
  other.instance_of?(self.class) && @sort_fields == other.sort_fields
end

#hashObject



28
29
30
# File 'lib/nose/plans/sort.rb', line 28

def hash
  @sort_fields.map(&:id).hash
end

#to_colorObject

:nocov:



17
18
19
# File 'lib/nose/plans/sort.rb', line 17

def to_color
  super + ' [' + @sort_fields.map(&:to_color).join(', ') + ']'
end