lib/dexter/indexer.rb in pgdexter-0.3.10 vs lib/dexter/indexer.rb in pgdexter-0.4.0

- old
+ new

@@ -238,32 +238,37 @@ candidates end def find_columns(plan) + plan = JSON.parse(plan.to_json) find_by_key(plan, "ColumnRef") end def find_indexes(plan) find_by_key(plan, "Index Name") end def find_by_key(plan, key) - indexes = [] - case plan - when Hash - plan.each do |k, v| - if k == key - indexes << v - else - indexes.concat(find_by_key(v, key)) + result = [] + queue = [plan] + while queue.any? + node = queue.pop + case node + when Hash + node.each do |k, v| + if k == key + result << v + elsif !v.nil? + queue << v + end end + when Array + queue.concat(node) end - when Array - indexes.concat(plan.flat_map { |v| find_by_key(v, key) }) end - indexes + result end def hypo_indexes_from_plan(index_name_to_columns, plan, index_set) query_indexes = [] @@ -481,22 +486,26 @@ new_indexes.each do |index| unless index_exists?(index) statement = String.new("CREATE INDEX CONCURRENTLY ON #{quote_ident(index[:table])} (#{index[:columns].map { |c| quote_ident(c) }.join(", ")})") statement << " TABLESPACE #{quote_ident(@tablespace)}" if @tablespace log "Creating index: #{statement}" - started_at = Time.now + started_at = monotonic_time begin execute(statement) - log "Index created: #{((Time.now - started_at) * 1000).to_i} ms" + 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 new_indexes + end + + def monotonic_time + Process.clock_gettime(Process::CLOCK_MONOTONIC) end def conn @conn ||= begin # set connect timeout if none set