2018-02-28 10:39:15 -05:00
|
|
|
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
|
2018-03-06 13:39:34 -05:00
|
|
|
# by the owner and the group, and readable by everyone else). Files created
|
|
|
|
# will also be modified by the process' umask value at the time of creation:
|
|
|
|
# https://docs.oracle.com/cd/E17276_01/html/api_reference/C/envopen.html
|
2018-02-28 10:39:15 -05:00
|
|
|
DATABASE_MODE = 0664
|
|
|
|
|
2018-05-18 10:06:30 -04:00
|
|
|
def self.use(type)
|
|
|
|
return_value = nil
|
|
|
|
DatabaseCache.new(type) { |db| return_value = yield(db) }
|
|
|
|
return_value
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2018-05-15 13:15:30 -04:00
|
|
|
|
2018-02-28 10:39:15 -05:00
|
|
|
# Opens and yields a database in read/write mode. Closes the database after use
|
|
|
|
#
|
2018-03-06 11:42:45 -05:00
|
|
|
# @yield [DBM] db
|
2018-02-28 10:39:15 -05:00
|
|
|
# @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)
|
2018-05-18 10:06:30 -04:00
|
|
|
yield(@db)
|
2018-02-28 10:39:15 -05:00
|
|
|
@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
|
|
|
|
#
|
2018-03-06 13:39:34 -05:00
|
|
|
# @param [Hash] ruby `Hash` to be converted to `JSON` string
|
2018-02-28 10:39:15 -05:00
|
|
|
# @return [String]
|
|
|
|
def ruby_hash_to_json_string(hash)
|
|
|
|
hash.to_json
|
|
|
|
end
|
|
|
|
|
2018-03-06 13:39:34 -05:00
|
|
|
# @param [String] `JSON` string to be converted to ruby `Hash`
|
2018-02-28 10:39:15 -05:00
|
|
|
# @return [Hash]
|
|
|
|
def json_string_to_ruby_hash(string)
|
|
|
|
JSON.parse(string)
|
|
|
|
end
|
|
|
|
end
|