Lazily load db of type DBM
instance variable for DatabaseCache
so the corresponding database file isn't created in the .use
block for a DatabaseCache
.
This commit is contained in:
parent
cd6f89ca76
commit
e93e8f3266
@ -14,21 +14,51 @@ class DatabaseCache
|
||||
|
||||
def self.use(type)
|
||||
return_value = nil
|
||||
DatabaseCache.new(type) { |db| return_value = yield(db) }
|
||||
|
||||
DatabaseCache.new(type) do |database_cache|
|
||||
return_value = yield(database_cache)
|
||||
end
|
||||
|
||||
return_value
|
||||
end
|
||||
|
||||
# Lazily loaded database in read/write mode. If this method is called, a
|
||||
# database file with be created in the `HOMEBREW_CACHE` with name
|
||||
# corresponding to the `@type` instance variable
|
||||
#
|
||||
# @return [DBM] db
|
||||
def db
|
||||
# DBM::WRCREAT: Creates the database if it does not already exist
|
||||
@db ||= DBM.open(cache_path, DATABASE_MODE, DBM::WRCREAT)
|
||||
end
|
||||
|
||||
# Returns `true` if the cache is empty for the given `@type`
|
||||
#
|
||||
# @return [Boolean]
|
||||
def empty?
|
||||
!File.exist?(cache_path)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Opens and yields a database in read/write mode. Closes the database after use
|
||||
# Opens and yields the cache. Closes the database after use if it has been
|
||||
# loaded
|
||||
#
|
||||
# @yield [DBM] db
|
||||
# @param [Symbol] type
|
||||
# @yield [DatabaseCache] self
|
||||
# @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
|
||||
def initialize(type)
|
||||
@type = type
|
||||
yield(self)
|
||||
@db&.close
|
||||
end
|
||||
|
||||
# The path where the database resides in the `HOMEBREW_CACHE` for the given
|
||||
# `@type`
|
||||
#
|
||||
# @return [String]
|
||||
def cache_path
|
||||
File.join(HOMEBREW_CACHE, "#{@type}.db")
|
||||
end
|
||||
end
|
||||
|
||||
@ -37,10 +67,10 @@ end
|
||||
# storage mechanism
|
||||
#
|
||||
class CacheStore
|
||||
# @param [DBM] database_cache
|
||||
# @param [DBM] db
|
||||
# @return [nil]
|
||||
def initialize(database_cache)
|
||||
@database_cache = database_cache
|
||||
def initialize(db)
|
||||
@db = db
|
||||
end
|
||||
|
||||
# Inserts new values or updates existing cached values to persistent storage
|
||||
@ -69,7 +99,7 @@ class CacheStore
|
||||
protected
|
||||
|
||||
# @return [DBM]
|
||||
attr_reader :database_cache
|
||||
attr_reader :db
|
||||
|
||||
# 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
|
||||
|
@ -25,7 +25,7 @@ module Homebrew
|
||||
ohai "Checking #{keg.name} linkage" if ARGV.kegs.size > 1
|
||||
|
||||
use_cache = ARGV.include?("--cached") || ENV["HOMEBREW_LINKAGE_CACHE"]
|
||||
result = LinkageChecker.new(keg, database_cache, use_cache)
|
||||
result = LinkageChecker.new(keg, database_cache.db, use_cache)
|
||||
|
||||
if ARGV.include?("--test")
|
||||
result.display_test_output
|
||||
|
@ -68,7 +68,7 @@ module FormulaCellarChecks
|
||||
|
||||
DatabaseCache.use(:linkage) do |database_cache|
|
||||
use_cache = !ENV["HOMEBREW_LINKAGE_CACHE"].nil?
|
||||
checker = LinkageChecker.new(keg, database_cache, use_cache, formula)
|
||||
checker = LinkageChecker.new(keg, database_cache.db, use_cache, formula)
|
||||
|
||||
next unless checker.broken_library_linkage?
|
||||
output = <<~EOS
|
||||
|
@ -1529,7 +1529,7 @@ class Formula
|
||||
|
||||
undeclared_deps = DatabaseCache.use(:linkage) do |database_cache|
|
||||
use_cache = !ENV["HOMEBREW_LINKAGE_CACHE"].nil?
|
||||
linkage_checker = LinkageChecker.new(keg, database_cache, use_cache, self)
|
||||
linkage_checker = LinkageChecker.new(keg, database_cache.db, use_cache, self)
|
||||
linkage_checker.undeclared_deps.map { |n| Dependency.new(n) }
|
||||
end
|
||||
|
||||
|
@ -613,7 +613,7 @@ class FormulaInstaller
|
||||
# Updates the cache for a particular formula after doing an install
|
||||
DatabaseCache.use(:linkage) do |database_cache|
|
||||
break if database_cache.empty?
|
||||
LinkageChecker.new(keg, database_cache, false, formula)
|
||||
LinkageChecker.new(keg, database_cache.db, false, formula)
|
||||
end
|
||||
|
||||
# let's reset Utils.git_available? if we just installed git
|
||||
|
@ -11,11 +11,11 @@ class LinkageStore < CacheStore
|
||||
HASH_LINKAGE_TYPES = [:brewed_dylibs, :reverse_links, :broken_deps].freeze
|
||||
|
||||
# @param [String] keg_name
|
||||
# @param [DBM] database_cache
|
||||
# @param [DBM] db
|
||||
# @return [nil]
|
||||
def initialize(keg_name, database_cache)
|
||||
def initialize(keg_name, db)
|
||||
@keg_name = keg_name
|
||||
super(database_cache)
|
||||
super(db)
|
||||
end
|
||||
|
||||
# Inserts dylib-related information into the cache if it does not exist or
|
||||
@ -40,7 +40,7 @@ class LinkageStore < CacheStore
|
||||
end
|
||||
end
|
||||
|
||||
database_cache[keg_name] = ruby_hash_to_json_string(
|
||||
db[keg_name] = ruby_hash_to_json_string(
|
||||
array_values: format_array_values(array_values),
|
||||
hash_values: format_hash_values(hash_values),
|
||||
)
|
||||
@ -64,7 +64,7 @@ class LinkageStore < CacheStore
|
||||
|
||||
# @return [nil]
|
||||
def flush_cache!
|
||||
database_cache.delete(keg_name)
|
||||
db.delete(keg_name)
|
||||
end
|
||||
|
||||
private
|
||||
@ -75,15 +75,15 @@ class LinkageStore < CacheStore
|
||||
# @param [Symbol] the type to fetch from the `LinkageStore`
|
||||
# @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]
|
||||
return [] unless db.key?(keg_name)
|
||||
json_string_to_ruby_hash(db[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]
|
||||
return {} unless db.key?(keg_name)
|
||||
json_string_to_ruby_hash(db[keg_name])["hash_values"][type.to_s]
|
||||
end
|
||||
|
||||
# Formats the linkage data for `array_values` into a kind which can be parsed
|
||||
|
Loading…
x
Reference in New Issue
Block a user