Class: Dexter::IndexCreator

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/dexter/index_creator.rb

Constant Summary

Constants included from Logging

Logging::COLOR_CODES

Instance Method Summary collapse

Methods included from Logging

#colorize, #log, #output

Constructor Details

#initialize(connection, indexer, new_indexes, tablespace) ⇒ IndexCreator

Returns a new instance of IndexCreator.



5
6
7
8
9
10
# File 'lib/dexter/index_creator.rb', line 5

def initialize(connection, indexer, new_indexes, tablespace)
  @connection = connection
  @indexer = indexer
  @new_indexes = new_indexes
  @tablespace = tablespace
end

Instance Method Details

#performObject

  1. create lock

  2. refresh existing index list

  3. create indexes that still don’t exist

  4. release lock



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

def perform
  with_advisory_lock do
    @new_indexes.each do |index|
      unless index_exists?(index)
        statement = String.new("CREATE INDEX CONCURRENTLY ON #{@connection.quote_ident(index[:table])} (#{index[:columns].map { |c| @connection.quote_ident(c) }.join(", ")})")
        statement << " TABLESPACE #{@connection.quote_ident(@tablespace)}" if @tablespace
        log "Creating index: #{statement}"
        started_at = monotonic_time
        begin
          @connection.execute(statement)
          log "Index created: #{((monotonic_time - started_at) * 1000).to_i} ms"
        rescue PG::LockNotAvailable
          log "Could not acquire lock: #{index[:table]}"
        end
      end
    end
  end
end