Class: PartitionGardener::GapDetection

Inherits:
Object
  • Object
show all
Defined in:
lib/partition_gardener/gap_detection.rb

Defined Under Namespace

Classes: Gap

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, config: nil) ⇒ GapDetection

Returns a new instance of GapDetection.



9
10
11
12
# File 'lib/partition_gardener/gap_detection.rb', line 9

def initialize(table_name, config: nil)
  @table_name = table_name
  @config = config || Registry.find_by_table_name(table_name)
end

Class Method Details

.call(table_name, config: Registry.find_by_table_name(table_name)) ⇒ Object



5
6
7
# File 'lib/partition_gardener/gap_detection.rb', line 5

def self.call(table_name, config: Registry.find_by_table_name(table_name))
  new(table_name, config: config).call
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/partition_gardener/gap_detection.rb', line 14

def call
  return [] unless @config

  strategy = Strategy.for(@config)
  return [] unless range_layout?(strategy)

  segments = strategy.attached_tail_segments
  return [] if segments.empty?

  sorted = segments.sort_by { |segment| [segment.range_start.to_s, segment.name] }
  gaps = []

  sorted.each_cons(2) do |left, right|
    left_end = normalize_range_end(left.range_end)
    right_start = right.range_start
    next if left_end == right_start

    gaps << Gap.new(
      range_start: left_end,
      range_end: right_start,
      message: "uncovered range between #{left.name} and #{right.name} (#{left_end}..#{right_start})"
    )
  end

  unless sorted.any? { |segment| segment.range_end == :max }
    gaps << Gap.new(
      range_start: sorted.last.range_end,
      range_end: :max,
      message: "no attached tail partition extends to MAXVALUE"
    )
  end

  gaps
end