2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-28 10:39:15 -05:00
|
|
|
require "cache_store"
|
|
|
|
|
|
|
|
#
|
2020-11-05 17:17:03 -05:00
|
|
|
# {LinkageCacheStore} provides methods to fetch and mutate linkage-specific data used
|
2018-10-18 21:42:43 -04:00
|
|
|
# by the `brew linkage` command.
|
2018-02-28 10:39:15 -05:00
|
|
|
#
|
2018-05-22 14:46:14 +01:00
|
|
|
class LinkageCacheStore < CacheStore
|
2018-10-18 21:42:43 -04:00
|
|
|
# @param keg_path [String]
|
|
|
|
# @param database [CacheStoreDatabase]
|
2018-02-28 10:39:15 -05:00
|
|
|
# @return [nil]
|
2018-06-06 13:27:59 +01:00
|
|
|
def initialize(keg_path, database)
|
|
|
|
@keg_path = keg_path
|
2018-05-22 14:46:14 +01:00
|
|
|
super(database)
|
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Returns `true` if the database has any value for the current `keg_path`.
|
2018-05-22 14:46:14 +01:00
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2018-05-22 12:54:54 -04:00
|
|
|
def keg_exists?
|
2018-06-06 13:27:59 +01:00
|
|
|
!database.get(@keg_path).nil?
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Inserts dylib-related information into the cache if it does not exist or
|
2020-11-05 17:17:03 -05:00
|
|
|
# updates data into the linkage cache if it does exist.
|
2018-02-28 10:39:15 -05:00
|
|
|
#
|
2018-10-18 21:42:43 -04:00
|
|
|
# @param hash_values [Hash] hash containing KVPs of { :type => Hash }
|
2018-02-28 10:39:15 -05:00
|
|
|
# @return [nil]
|
2018-06-01 13:26:45 +01:00
|
|
|
def update!(hash_values)
|
|
|
|
hash_values.each_key do |type|
|
|
|
|
next if HASH_LINKAGE_TYPES.include?(type)
|
|
|
|
|
|
|
|
raise TypeError, <<~EOS
|
|
|
|
Can't update types that are not defined for the linkage store
|
|
|
|
EOS
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
|
2018-09-25 20:19:30 +01:00
|
|
|
database.set @keg_path, hash_values
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# @param type [Symbol] the type to fetch from the {LinkageCacheStore}
|
2018-06-01 13:26:45 +01:00
|
|
|
# @raise [TypeError] error if the type is not in `HASH_LINKAGE_TYPES`
|
|
|
|
# @return [Hash]
|
2018-10-13 08:22:51 -07:00
|
|
|
def fetch(type)
|
2018-06-01 13:26:45 +01:00
|
|
|
unless HASH_LINKAGE_TYPES.include?(type)
|
2018-03-06 13:39:34 -05:00
|
|
|
raise TypeError, <<~EOS
|
|
|
|
Can't fetch types that are not defined for the linkage store
|
|
|
|
EOS
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
2018-06-01 13:26:45 +01:00
|
|
|
|
|
|
|
return {} unless keg_exists?
|
|
|
|
|
|
|
|
fetch_hash_values(type)
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
|
2020-11-05 17:17:03 -05:00
|
|
|
# Delete the keg from the {LinkageCacheStore}
|
2018-10-13 08:22:51 -07:00
|
|
|
#
|
2018-02-28 10:39:15 -05:00
|
|
|
# @return [nil]
|
2018-10-13 08:22:51 -07:00
|
|
|
def delete!
|
2018-06-06 13:27:59 +01:00
|
|
|
database.delete(@keg_path)
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-01 13:26:45 +01:00
|
|
|
HASH_LINKAGE_TYPES = [:keg_files_dylibs].freeze
|
2018-02-28 10:39:15 -05:00
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# @param type [Symbol]
|
2018-02-28 10:39:15 -05:00
|
|
|
# @return [Hash]
|
|
|
|
def fetch_hash_values(type)
|
2018-06-06 13:27:59 +01:00
|
|
|
keg_cache = database.get(@keg_path)
|
2018-05-22 14:46:14 +01:00
|
|
|
return {} unless keg_cache
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2018-09-25 20:19:30 +01:00
|
|
|
keg_cache[type.to_s]
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
end
|