Class: Mysql::Partition::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql/partitioner/range.rb

Instance Method Summary collapse

Constructor Details

#initialize(cli, partition) ⇒ Range

Returns a new instance of Range.



5
6
7
8
9
# File 'lib/mysql/partitioner/range.rb', line 5

def initialize(cli, partition)
  @client = cli
  @partition = partition
  load_partitions
end

Instance Method Details

#build_new_partitionsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mysql/partitioner/range.rb', line 43

def build_new_partitions() 
  max_val = select_max_val()
  partitioned_upto = @partitions.last ? @partitions.last["less_than"] : 0
  open_partitions = @partitions.select { |item| item[:less_than] > max_val }.size
  buildable = @partition["prepared"] - open_partitions
  result = []
  for i in (1 .. buildable) do
    new_less_than = partitioned_upto + @partition["interval"]
    result.push({ less_than: new_less_than, name: "p" + new_less_than.to_s })
    partitioned_upto = new_less_than
  end
  result
end

#find_old_partitionsObject



37
38
39
40
41
# File 'lib/mysql/partitioner/range.rb', line 37

def find_old_partitions()
  @partitions.select {|item|
    find_most_recent_item(item) <= config[:target_date]
  }
end

#get_next_partitionsObject



31
32
33
34
35
# File 'lib/mysql/partitioner/range.rb', line 31

def get_next_partitions()
  next_partition = @partitions.dup
  next_partition = next_partition - find_old_partitions()
  next_partition = next_partition + build_new_partitions()
end

#load_partitionsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mysql/partitioner/range.rb', line 11

def load_partitions()
  @database = @client.query("SELECT DATABASE()").first.values[0]
  results = @client.query("      SELECT PARTITION_EXPRESSION, PARTITION_DESCRIPTION, PARTITION_ORDINAL_POSITION, PARTITION_METHOD, SUBPARTITION_EXPRESSION\n      FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME=\"\#{@partition[\"table\"]}\" AND TABLE_SCHEMA=\"\#{@database}\"\n")
  row = results.first
  if row.nil? then
    raise "Table schema not found"
  end

  if row["PARTITION_METHOD"] != "RANGE" and !row["PARTITION_METHOD"].nil? then
    raise "Not a range partition"
  end

  @partitions = results.select{|item| !item[:PARTITION_METHOD].nil? }.map {|item|
    { name: item[:partition_description], less_than: item[:PARTITION_ORDINAL_POSITION] }
  }
end

#select_max_valObject



57
58
59
# File 'lib/mysql/partitioner/range.rb', line 57

def select_max_val()
  @client.query("SELECT MAX(#{@partition["key"]}) FROM #{@partition["table"]}").first.values[0] or 0
end