Separated os/mac/cache_store.rb into cache_store.rb and os/mac/linkage_cache_store.rb.

This commit is contained in:
AndrewMcBurney 2018-02-28 10:39:15 -05:00
parent 14256faa47
commit d7765dd223
6 changed files with 188 additions and 163 deletions

View File

@ -0,0 +1,80 @@
require "dbm"
require "json"
#
# `DatabaseCache` acts as an interface to a persistent storage mechanism
# residing in the `HOMEBREW_CACHE`
#
class DatabaseCache
# The mode of any created files will be 0664 (that is, readable and writable
# by the owner and the group, and readable by everyone else)
DATABASE_MODE = 0664
# Opens and yields a database in read/write mode. Closes the database after use
#
# @yield [DBM]
# @return [nil]
def initialize(name)
# DBM::WRCREAT: Creates the database if it does not already exist
@db = DBM.open("#{HOMEBREW_CACHE}/#{name}.db", DATABASE_MODE, DBM::WRCREAT)
yield(@db)
@db.close
end
end
#
# `CacheStore` provides methods to mutate and fetch data from a persistent
# storage mechanism
#
class CacheStore
# @param [DBM] database_cache
# @return [nil]
def initialize(database_cache)
@database_cache = database_cache
end
# Inserts new values or updates existing cached values to persistent storage
# mechanism
#
# @abstract
def update!(*)
raise NotImplementedError
end
# Fetches cached values in persistent storage according to the type of data
# stored
#
# @abstract
def fetch_type(*)
raise NotImplementedError
end
# Deletes data from the cache based on a condition defined in a concrete class
#
# @abstract
def flush_cache!
raise NotImplementedError
end
protected
# @return [DBM]
attr_reader :database_cache
# DBM stores ruby objects as a ruby `String`. Hence, when fetching the data,
# to convert the ruby string back into a ruby `Hash`, the string is converted
# into a JSON compatible string in `ruby_hash_to_json_string`, where it may
# later be parsed by `JSON.parse` in the `json_string_to_ruby_hash` method
#
# @param [Hash]
# @return [String]
def ruby_hash_to_json_string(hash)
hash.to_json
end
# @param [String]
# @return [Hash]
def json_string_to_ruby_hash(string)
JSON.parse(string)
end
end

View File

@ -13,6 +13,7 @@
#: If `--rebuild` is passed, flushes the `LinkageStore` cache for each #: If `--rebuild` is passed, flushes the `LinkageStore` cache for each
#: 'keg.name' and forces a check on the dylibs. #: 'keg.name' and forces a check on the dylibs.
require "cache_store"
require "os/mac/linkage_checker" require "os/mac/linkage_checker"
module Homebrew module Homebrew

View File

@ -1,3 +1,4 @@
require "cache_store"
require "os/mac/linkage_checker" require "os/mac/linkage_checker"
module FormulaCellarChecks module FormulaCellarChecks

View File

@ -1,150 +0,0 @@
require "dbm"
require "json"
#
# `DatabaseCache` acts as an interface to a persistent storage mechanism
# residing in the `HOMEBREW_CACHE`
#
class DatabaseCache
# The mode of any created files will be 0664 (that is, readable and writable
# by the owner and the group, and readable by everyone else)
DATABASE_MODE = 0664
# Opens and yields a database in read/write mode
def initialize(name)
# DBM::WRCREAT: Creates the database if it does not already exist
@db = DBM.open("#{HOMEBREW_CACHE}/#{name}.db", DATABASE_MODE, DBM::WRCREAT)
yield(@db)
@db.close
end
end
#
# `CacheStore` provides methods to mutate and fetch data from a persistent
# storage mechanism
#
class CacheStore
def initialize(database_cache)
@database_cache = database_cache
end
# Inserts new values or updates existing cached values to persistent storage
# mechanism
def update!(*)
raise NotImplementedError
end
# Fetches cached values in persistent storage according to the type of data
# stored
def fetch_type(*)
raise NotImplementedError
end
# Deletes data from the cache based on a condition defined in a concrete class
def flush_cache!
raise NotImplementedError
end
protected
# @return [DBM]
attr_reader :database_cache
# DBM stores ruby objects as a ruby `String`. Hence, when fetching the data,
# to convert the ruby string back into a ruby `Hash`, the string is converted
# into a JSON compatible string in `ruby_hash_to_json_string`, where it may
# later be parsed by `JSON.parse` in the `json_string_to_ruby_hash` method
#
# @param [Hash]
# @return [String]
def ruby_hash_to_json_string(hash)
hash.to_json
end
# @param [String]
# @return [Hash]
def json_string_to_ruby_hash(string)
JSON.parse(string)
end
end
#
# `LinkageStore` provides methods to fetch and mutate linkage-specific data used
# by the `brew linkage` command
#
class LinkageStore < CacheStore
HASH_LINKAGE_TYPES = [:brewed_dylibs, :reverse_links].freeze
def initialize(keg_name, database_cache)
@keg_name = keg_name
super(database_cache)
end
def update!(
array_values: {
system_dylibs: %w[],
variable_dylibs: %w[],
broken_dylibs: %w[],
indirect_deps: %w[],
undeclared_deps: %w[],
unnecessary_deps: %w[],
},
hash_values: {
brewed_dylibs: {},
reverse_links: {},
}
)
database_cache[keg_name] = ruby_hash_to_json_string(
array_values: format_array_values(array_values),
hash_values: format_hash_values(hash_values),
)
end
def fetch_type(type)
if HASH_LINKAGE_TYPES.include?(type)
fetch_hash_values(type: type)
else
fetch_array_values(type: type)
end
end
def flush_cache!
database_cache.delete(keg_name)
end
private
attr_reader :keg_name
def fetch_array_values(type:)
return [] unless database_cache.key?(keg_name)
json_string_to_ruby_hash(database_cache[keg_name])["array_values"][type.to_s]
end
def fetch_hash_values(type:)
return {} unless database_cache.key?(keg_name)
json_string_to_ruby_hash(database_cache[keg_name])["hash_values"][type.to_s]
end
# Formats the linkage data for `array_values` into a kind which can be parsed
# by the `json_string_to_ruby_hash` method. Internally converts ruby `Set`s to
# `Array`s
#
# @return [String]
def format_array_values(hash)
hash.each_with_object({}) { |(k, v), h| h[k] = v.to_a }
end
# Formats the linkage data for `hash_values` into a kind which can be parsed
# by the `json_string_to_ruby_hash` method. Converts ruby `Set`s to `Array`s, and
# converts ruby `Pathname`s to `String`s
#
# @return [String]
def format_hash_values(hash)
hash.each_with_object({}) do |(outer_key, outer_values), outer_hash|
outer_hash[outer_key] = outer_values.each_with_object({}) do |(k, v), h|
h[k] = v.to_a.map(&:to_s)
end
end
end
end

View File

@ -0,0 +1,97 @@
require "cache_store"
#
# `LinkageStore` provides methods to fetch and mutate linkage-specific data used
# by the `brew linkage` command
#
class LinkageStore < CacheStore
HASH_LINKAGE_TYPES = [:brewed_dylibs, :reverse_links].freeze
# @param [String] keg_name
# @param [DBM] database_cache
# @return [nil]
def initialize(keg_name, database_cache)
@keg_name = keg_name
super(database_cache)
end
# Inserts dylib-related information into the cache if it does not exist or
# updates data into the linkage cache if it does exist
#
# @param [Hash] array_values
# @param [Hash] hash_values
# @param [Array[Hash]] values
# @return [nil]
def update!(array_values: {}, hash_values: {}, **values)
values.each do |key, value|
if value.is_a? Hash
hash_values[key] = value
else
array_values[key] = value
end
end
database_cache[keg_name] = ruby_hash_to_json_string(
array_values: format_array_values(array_values),
hash_values: format_hash_values(hash_values),
)
end
# @param [Symbol] type
# @return [Hash | Array]
def fetch_type(type)
if HASH_LINKAGE_TYPES.include?(type)
fetch_hash_values(type)
else
fetch_array_values(type)
end
end
# @return [nil]
def flush_cache!
database_cache.delete(keg_name)
end
private
# @return [String]
attr_reader :keg_name
# @param [Symbol] type
# @return [Array]
def fetch_array_values(type)
return [] unless database_cache.key?(keg_name)
json_string_to_ruby_hash(database_cache[keg_name])["array_values"][type.to_s]
end
# @param [Symbol] type
# @return [Hash]
def fetch_hash_values(type)
return {} unless database_cache.key?(keg_name)
json_string_to_ruby_hash(database_cache[keg_name])["hash_values"][type.to_s]
end
# Formats the linkage data for `array_values` into a kind which can be parsed
# by the `json_string_to_ruby_hash` method. Internally converts ruby `Set`s to
# `Array`s
#
# @param [Hash]
# @return [String]
def format_array_values(hash)
hash.each_with_object({}) { |(k, v), h| h[k] = v.to_a }
end
# Formats the linkage data for `hash_values` into a kind which can be parsed
# by the `json_string_to_ruby_hash` method. Converts ruby `Set`s to `Array`s,
# and converts ruby `Pathname`s to `String`s
#
# @param [Hash]
# @return [String]
def format_hash_values(hash)
hash.each_with_object({}) do |(outer_key, outer_values), outer_hash|
outer_hash[outer_key] = outer_values.each_with_object({}) do |(k, v), h|
h[k] = v.to_a.map(&:to_s)
end
end
end
end

View File

@ -1,7 +1,7 @@
require "set" require "set"
require "keg" require "keg"
require "formula" require "formula"
require "os/mac/cache_store" require "os/mac/linkage_cache_store"
class LinkageChecker class LinkageChecker
attr_reader :keg, :formula, :store attr_reader :keg, :formula, :store
@ -223,18 +223,14 @@ class LinkageChecker
# Updates data store with package path values # Updates data store with package path values
def store_dylibs! def store_dylibs!
store.update!( store.update!(
array_values: { system_dylibs: @system_dylibs,
system_dylibs: @system_dylibs, variable_dylibs: @variable_dylibs,
variable_dylibs: @variable_dylibs, broken_dylibs: @broken_dylibs,
broken_dylibs: @broken_dylibs, indirect_deps: @indirect_deps,
indirect_deps: @indirect_deps, undeclared_deps: @undeclared_deps,
undeclared_deps: @undeclared_deps, unnecessary_deps: @unnecessary_deps,
unnecessary_deps: @unnecessary_deps, brewed_dylibs: @brewed_dylibs,
}, reverse_links: @reverse_links,
hash_values: {
brewed_dylibs: @brewed_dylibs,
reverse_links: @reverse_links,
},
) )
end end
end end