Class: PartitionGardener::PlanDiff

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

Defined Under Namespace

Classes: Operation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attached_segments, target_segments) ⇒ PlanDiff

Returns a new instance of PlanDiff.



21
22
23
24
# File 'lib/partition_gardener/plan_diff.rb', line 21

def initialize(attached_segments, target_segments)
  @attached_segments = attached_segments
  @target_segments = target_segments
end

Class Method Details

.changed?(attached_segments, target_segments) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/partition_gardener/plan_diff.rb', line 17

def self.changed?(attached_segments, target_segments)
  attached_segments.map(&:signature).sort != target_segments.map(&:signature).sort
end

.operations(attached_segments, target_segments) ⇒ Object



8
9
10
# File 'lib/partition_gardener/plan_diff.rb', line 8

def self.operations(attached_segments, target_segments)
  new(attached_segments, target_segments).operations
end

.plan_signature(segments) ⇒ Object



12
13
14
15
# File 'lib/partition_gardener/plan_diff.rb', line 12

def self.plan_signature(segments)
  payload = segments.map(&:signature).sort_by(&:to_s).to_json
  Digest::SHA256.hexdigest(payload)[0, 16]
end

Instance Method Details

#operationsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/partition_gardener/plan_diff.rb', line 26

def operations
  attached_by_name = @attached_segments.each_with_object({}) { |segment, index| index[segment.name] = segment }
  target_by_name = @target_segments.each_with_object({}) { |segment, index| index[segment.name] = segment }
  result = []

  target_by_name.each_value do |segment|
    attached = attached_by_name[segment.name]
    result << if attached.nil?
      Operation.new(:create, segment, nil)
    elsif attached.signature == segment.signature
      Operation.new(:keep, segment, attached)
    else
      Operation.new(:reshape, segment, attached)
    end
  end

  attached_by_name.each_value do |attached|
    next if target_by_name[attached.name]

    result << Operation.new(:drop, nil, attached)
  end

  result
end