Class: NoSE::IndexEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/nose/enumerator.rb

Overview

Produces potential indices to be used in schemas

Instance Method Summary collapse

Constructor Details

#initialize(workload) ⇒ IndexEnumerator

Returns a new instance of IndexEnumerator.



8
9
10
11
12
# File 'lib/nose/enumerator.rb', line 8

def initialize(workload)
  @logger = Logging.logger['nose::enumerator']

  @workload = workload
end

Instance Method Details

#indexes_for_query(query) ⇒ Array<Index>

Produce all possible indices for a given query

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nose/enumerator.rb', line 16

def indexes_for_query(query)
  @logger.debug "Enumerating indexes for query #{query.text}"

  range = if query.range_field.nil?
            query.order
          else
            [query.range_field] + query.order
          end

  eq = query.eq_fields.group_by(&:parent)
  eq.default_proc = ->(*) { [] }

  range = range.group_by(&:parent)
  range.default_proc = ->(*) { [] }

  query.graph.subgraphs.flat_map do |graph|
    indexes_for_graph graph, query.select, eq, range
  end.uniq << query.materialize_view
end

#indexes_for_workload(additional_indexes = [], by_id_graph = false) ⇒ Set<Index>

Produce all possible indices for a given workload

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nose/enumerator.rb', line 38

def indexes_for_workload(additional_indexes = [], by_id_graph = false)
  queries = @workload.queries
  indexes = Parallel.map(queries) do |query|
    indexes_for_query(query).to_a
  end.inject(additional_indexes, &:+)

  # Add indexes generated for support queries
  supporting = support_indexes indexes, by_id_graph
  supporting += support_indexes supporting, by_id_graph
  indexes += supporting

  # Deduplicate indexes, combine them and deduplicate again
  indexes.uniq!
  combine_indexes indexes
  indexes.uniq!

  @logger.debug do
    "Indexes for workload:\n" + indexes.map.with_index do |index, i|
      "#{i} #{index.inspect}"
    end.join("\n")
  end

  indexes
end