2018-03-06 12:07:57 -05:00
|
|
|
require "set"
|
2018-02-28 10:39:15 -05:00
|
|
|
require "cache_store"
|
|
|
|
|
|
|
|
#
|
2018-05-22 14:46:14 +01:00
|
|
|
# `LinkageCacheStore` provides methods to fetch and mutate linkage-specific data used
|
2018-02-28 10:39:15 -05:00
|
|
|
# by the `brew linkage` command
|
|
|
|
#
|
2018-05-22 14:46:14 +01:00
|
|
|
class LinkageCacheStore < CacheStore
|
2018-02-28 10:39:15 -05:00
|
|
|
# @param [String] keg_name
|
2018-05-22 14:46:14 +01:00
|
|
|
# @param [CacheStoreDatabase] database
|
2018-02-28 10:39:15 -05:00
|
|
|
# @return [nil]
|
2018-05-22 14:46:14 +01:00
|
|
|
def initialize(keg_name, database)
|
2018-02-28 10:39:15 -05:00
|
|
|
@keg_name = keg_name
|
2018-05-22 14:46:14 +01:00
|
|
|
super(database)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns `true` if the database has any value for the current `keg_name`
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2018-05-22 12:54:54 -04:00
|
|
|
def keg_exists?
|
2018-06-01 13:26:45 +01:00
|
|
|
!database.get(@keg_name).nil?
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Inserts dylib-related information into the cache if it does not exist or
|
|
|
|
# updates data into the linkage cache if it does exist
|
|
|
|
#
|
2018-03-06 13:39:34 -05:00
|
|
|
# @param [Hash] hash_values: 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-06-01 13:26:45 +01:00
|
|
|
database.set @keg_name, ruby_hash_to_json_string(hash_values)
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
|
2018-05-22 14:46:14 +01:00
|
|
|
# @param [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-02-28 10:39:15 -05:00
|
|
|
def fetch_type(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
|
|
|
|
|
|
|
|
# @return [nil]
|
|
|
|
def flush_cache!
|
2018-06-01 13:26:45 +01:00
|
|
|
database.delete(@keg_name)
|
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
|
|
|
|
|
|
|
# @param [Symbol] type
|
|
|
|
# @return [Hash]
|
|
|
|
def fetch_hash_values(type)
|
2018-06-01 13:26:45 +01:00
|
|
|
keg_cache = database.get(@keg_name)
|
2018-05-22 14:46:14 +01:00
|
|
|
return {} unless keg_cache
|
2018-06-01 13:26:45 +01:00
|
|
|
json_string_to_ruby_hash(keg_cache)[type.to_s]
|
2018-02-28 10:39:15 -05:00
|
|
|
end
|
|
|
|
end
|