Class: SqlPartitioner::Partition

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_partitioner/partition.rb

Overview

Represents information for a single partition in the database

Constant Summary collapse

FUTURE_PARTITION_NAME =
'future'
FUTURE_PARTITION_VALUE =
'MAXVALUE'
TO_LOG_ATTRIBUTES_SORT_ORDER =
[
  :ordinal_position, :name, :timestamp, :table_rows, :data_length, :index_length
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(partition_data) ⇒ Partition

Likely only called by Partition.all



60
61
62
# File 'lib/sql_partitioner/partition.rb', line 60

def initialize(partition_data)
  @partition_data = partition_data
end

Class Method Details

.all(adapter, table_name) ⇒ PartitionCollection

Fetches info on all partitions for the given table_name, using the given adapter.



68
69
70
71
72
73
74
75
76
# File 'lib/sql_partitioner/partition.rb', line 68

def self.all(adapter, table_name)
  select_sql = SqlPartitioner::SQL.partition_info
  result = adapter.select(select_sql, adapter.schema_name, table_name).reject{|r| r.partition_description.nil? }

  partition_collection = PartitionCollection.new
  result.each{ |r| partition_collection << self.new(r) }

  partition_collection
end

.to_log(partitions) ⇒ String

logs the formatted partition info from information schema



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/sql_partitioner/partition.rb', line 132

def self.to_log(partitions)
  return "none" if partitions.empty?

  padding = TO_LOG_ATTRIBUTES_SORT_ORDER.map do |attribute|
              max_length = partitions.map do |partition|
                partition.send(attribute).to_s.length
              end.max
              [attribute.to_s.length, max_length].max + 3
            end

  header = TO_LOG_ATTRIBUTES_SORT_ORDER.each_with_index.map do |attribute, index|
              attribute.to_s.ljust(padding[index])
            end.join

  body = partitions.map do |partition|
           TO_LOG_ATTRIBUTES_SORT_ORDER.each_with_index.map do |attribute, index|
             partition.send(attribute).to_s.ljust(padding[index])
           end.join
         end.join("\n")

  separator = ''.ljust(padding.inject(&:+),'-')

  [separator, header, separator, body, separator].join("\n")
end

Instance Method Details

#attributesHash



118
119
120
121
122
123
124
125
126
127
# File 'lib/sql_partitioner/partition.rb', line 118

def attributes
  {
    :ordinal_position  => ordinal_position,
    :name              => name,
    :timestamp         => timestamp,
    :table_rows        => table_rows,
    :data_length       => data_length,
    :index_length      => index_length
  }
end

#data_lengthFixnum



103
104
105
# File 'lib/sql_partitioner/partition.rb', line 103

def data_length
  @partition_data.data_length
end

#future_partition?Boolean



113
114
115
# File 'lib/sql_partitioner/partition.rb', line 113

def future_partition?
  self.timestamp == FUTURE_PARTITION_VALUE
end

#index_lengthFixnum



108
109
110
# File 'lib/sql_partitioner/partition.rb', line 108

def index_length
  @partition_data.index_length
end

#nameString



84
85
86
# File 'lib/sql_partitioner/partition.rb', line 84

def name
  @partition_data.partition_name
end

#ordinal_positionFixnum



79
80
81
# File 'lib/sql_partitioner/partition.rb', line 79

def ordinal_position
  @partition_data.partition_ordinal_position
end

#table_rowsFixnum



98
99
100
# File 'lib/sql_partitioner/partition.rb', line 98

def table_rows
  @partition_data.table_rows
end

#timestampFixnum, String



89
90
91
92
93
94
95
# File 'lib/sql_partitioner/partition.rb', line 89

def timestamp
  if @partition_data.partition_description == FUTURE_PARTITION_VALUE
    FUTURE_PARTITION_VALUE
  else
    @partition_data.partition_description.to_i
  end
end