class top_level_module::DeepCover::Persistence

Constants

BASENAME
TRACKER_TEMPLATE

Attributes

dir_path[R]

Public Class Methods

merge_tracker_hits_per_paths(*tracker_hits_per_path_hashes) click to toggle source
# File lib/deep_cover/persistence.rb, line 60
def self.merge_tracker_hits_per_paths(*tracker_hits_per_path_hashes)
  return {} if tracker_hits_per_path_hashes.empty?

  result = tracker_hits_per_path_hashes[0].transform_values(&:dup)

  tracker_hits_per_path_hashes[1..-1].each do |tracker_hits_per_path|
    tracker_hits_per_path.each do |path, tracker_hits|
      matching_result = result[path]
      if matching_result.nil?
        result[path] = tracker_hits.dup
        next
      end

      if matching_result.size != tracker_hits.size
        raise "Attempting to merge trackers of different sizes: #{matching_result.size} vs #{tracker_hits.size}, for path #{path}"
      end

      tracker_hits.each_with_index do |nb_hits, i|
        matching_result[i] += nb_hits
      end
    end
  end

  result
end
new(cache_directory) click to toggle source
# File lib/deep_cover/persistence.rb, line 17
def initialize(cache_directory)
  @dir_path = Pathname(cache_directory).expand_path
end

Public Instance Methods

clear_directory() click to toggle source
# File lib/deep_cover/persistence.rb, line 52
def clear_directory
  delete_trackers
  begin
    dir_path.rmdir
  rescue SystemCallError # rubocop:disable Lint/SuppressedException
  end
end
delete_trackers() click to toggle source
# File lib/deep_cover/persistence.rb, line 48
def delete_trackers
  tracker_files.each(&:delete)
end
load_trackers() click to toggle source

returns a TrackerHitsPerPath

# File lib/deep_cover/persistence.rb, line 32
def load_trackers
  tracker_hits_per_path_hashes = tracker_files.map do |full_path|
    check_tracker_file(**JSON.parse(full_path.binread).transform_keys(&:to_sym))
  end

  self.class.merge_tracker_hits_per_paths(*tracker_hits_per_path_hashes)
end
merge_persisted_trackers() click to toggle source
# File lib/deep_cover/persistence.rb, line 40
def merge_persisted_trackers
  tracker_hits_per_path = load_trackers
  return if tracker_hits_per_path.empty?
  tracker_files_before = tracker_files
  save_trackers(tracker_hits_per_path)
  tracker_files_before.each(&:delete)
end
save_trackers(tracker_hits_per_path) click to toggle source
# File lib/deep_cover/persistence.rb, line 21
def save_trackers(tracker_hits_per_path)
  create_directory_if_needed
  basename = format(TRACKER_TEMPLATE, unique: SecureRandom.urlsafe_base64)

  dir_path.join(basename).binwrite(JSON.dump(
                                       version: VERSION,
                                       tracker_hits_per_path: tracker_hits_per_path,
  ))
end

Private Instance Methods

check_tracker_file(version:, tracker_hits_per_path:) click to toggle source
# File lib/deep_cover/persistence.rb, line 88
def check_tracker_file(version:, tracker_hits_per_path:)
  raise "dump version mismatch: #{version}, currently #{VERSION}" unless version == VERSION
  tracker_hits_per_path
end
create_directory_if_needed() click to toggle source
# File lib/deep_cover/persistence.rb, line 93
def create_directory_if_needed
  dir_path.mkpath
end
tracker_files() click to toggle source
# File lib/deep_cover/persistence.rb, line 97
def tracker_files
  basename = format(TRACKER_TEMPLATE, unique: '*')
  Pathname.glob(dir_path.join(basename))
end