Sha256: 0863b339f08d25008758c4b7346dfba559f22b8292aab0fce78b981d670d136f

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module Snapsync
    class Cleanup
        # The underlying timeline policy object that we use to compute which
        # snapshots to delete and which to keep
        attr_reader :policy

        def initialize(policy)
            @policy = policy
        end

        def cleanup(target, dry_run: false)
            snapshots = target.each_snapshot.to_a
            filtered_snapshots = policy.filter_snapshots(snapshots).to_set

            if filtered_snapshots.any? { |s| s.synchronization_point? }
                raise InvalidPolicy, "#{policy} returned a snapsync synchronization point in its results"
            end

            if filtered_snapshots.empty?
                raise InvalidPolicy, "#{policy} returned no snapshots"
            end

            last_sync_point = snapshots.
                sort_by(&:num).reverse.
                find { |s| s.synchronization_point_for?(target) }
            filtered_snapshots << last_sync_point
            filtered_snapshots = filtered_snapshots.to_set

            deleted_snapshots = snapshots.sort_by(&:num).find_all do |s|
                if !filtered_snapshots.include?(s)
                    target.delete(s, dry_run: dry_run)
                    true
                end
            end
        end
    end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snapsync-0.3.8 lib/snapsync/cleanup.rb
snapsync-0.3.7 lib/snapsync/cleanup.rb
snapsync-0.3.6 lib/snapsync/cleanup.rb
snapsync-0.3.5 lib/snapsync/cleanup.rb
snapsync-0.3.4 lib/snapsync/cleanup.rb
snapsync-0.3.3 lib/snapsync/cleanup.rb