Class: Mysql::Partitioner::Strategy::PartitionByPkDropByTime

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

Instance Method Summary collapse

Constructor Details

#initialize(operation, config) ⇒ PartitionByPkDropByTime

Returns a new instance of PartitionByPkDropByTime.



6
7
8
9
10
11
12
13
14
15
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 6

def initialize(operation, config)
  @operation = operation or raise "operation not specified"
  @key = config[:key] or raise "Key not specified"
  @time_key = config[:time_key] or raise "time column not specified"
  @ttl = config[:ttl] or raise "ttl not specified"
  @range = config[:range] or raise "range not specified"
  @min_empty_partitions = config[:min_empty_partitions] or raise "min_empty_partitions not specified"
  @desirable_empty_partitions = config[:desirable_empty_partitions] or raise "desirable_empty_partitions not specified"
  @operation.check!
end

Instance Method Details

#checkObject



30
31
32
33
34
35
36
37
38
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 30

def check()
  success = true
  begin
    check!
  rescue => e
    success = false
  end
  return success
end

#check!Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 17

def check!()

  raise "Not partitioned" unless @operation.partitionated?
  current = @operation.get_current_bounded_partitions
  
  empty = find_empty_partitions(current)
  if empty.size < @min_empty_partitions then
    raise "empty partitions is less than minimum was=#{empty.size} should_be=#{@min_empty_partitions}"
  else
    true
  end
end

#find_empty_partitions(current_partitions) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 75

def find_empty_partitions(current_partitions)
  
  max_val = @operation.get_max_val(@key)
  return current_partitions if max_val.nil?
  max_val = max_val.to_i
  max_active_partition = find_partition(current_partitions, max_val)

  current_partitions.select{|item|
    item.less_than > max_val && item != max_active_partition
  }
end

#find_old_partitions(current_partitions) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 87

def find_old_partitions(current_partitions)

  max_val = @operation.get_max_val(@key)
  return [] if max_val.nil?
  max_val = max_val.to_i

  max_active_partition = find_partition(current_partitions, max_val)

  old_index = current_partitions.rindex{|item|
    time = @operation.of_max_val(@key, @time_key, item.name)
    time && time.to_i + @ttl < Time.now.to_i && item != max_active_partition
  }

    
  old_index && old_index >= 0 ? current_partitions[0 .. old_index] : []
end

#find_partition(partitions, val) ⇒ Object



104
105
106
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 104

def find_partition(partitions, val)
  return partitions.find{|item| item.less_than > val} 
end

#initialize_partitionsObject



48
49
50
51
52
53
54
55
56
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 48

def initialize_partitions()
  max_less_than = 0
  new = []
  @desirable_empty_partitions.times do
    max_less_than = max_less_than + @range
    new.push( Mysql::Partitioner::Partition::Range.new(max_less_than) )
  end
  @operation.create_partitions(@key, new)
end

#migrateObject



40
41
42
43
44
45
46
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 40

def migrate()
  if @operation.partitionated? then
    update_partitions()
  else
    initialize_partitions()
  end
end

#update_partitionsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mysql/partitioner/strategy/partition_by_pk_drop_by_time.rb', line 58

def update_partitions()
  current = @operation.get_current_bounded_partitions
  empty = find_empty_partitions(current)

  new = []
  if empty.size < @desirable_empty_partitions then
    max_less_than = current.last.less_than
    ( @desirable_empty_partitions - empty.size ).times do
      max_less_than = max_less_than + @range
      new.push( Mysql::Partitioner::Partition::Range.new(max_less_than) )
    end
  end
  old = find_old_partitions(current)
  @operation.migrate_partitions(current, current - old + new)
end