diff --git a/Library/Homebrew/cache_store.rb b/Library/Homebrew/cache_store.rb index a9b28f704b..8a1c7c334d 100644 --- a/Library/Homebrew/cache_store.rb +++ b/Library/Homebrew/cache_store.rb @@ -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 diff --git a/Library/Homebrew/dev-cmd/linkage.rb b/Library/Homebrew/dev-cmd/linkage.rb index 01bdfda57f..d0370310f5 100644 --- a/Library/Homebrew/dev-cmd/linkage.rb +++ b/Library/Homebrew/dev-cmd/linkage.rb @@ -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 diff --git a/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb b/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb index bd78f0e30b..012716b10b 100644 --- a/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb +++ b/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb @@ -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 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 2f4fc73ae7..8ff186c8f4 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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 diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index 946a6fddb3..8901639fa6 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -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 diff --git a/Library/Homebrew/linkage_cache_store.rb b/Library/Homebrew/linkage_cache_store.rb index 2816b681df..d6ca753abe 100644 --- a/Library/Homebrew/linkage_cache_store.rb +++ b/Library/Homebrew/linkage_cache_store.rb @@ -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