<commit_before>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). 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
  DATABASE_MODE = 0664

  def self.use(type)
    return_value = nil

    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 the cache. Closes the database after use if it has been
  # loaded
  #
  # @param  [Symbol] type
  # @yield  [DatabaseCache] self
  # @return [nil]
  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

#
# `CacheStore` provides methods to mutate and fetch data from a persistent
# storage mechanism
#
class CacheStore
  # @param  [DBM] db
  # @return [nil]
  def initialize(db)
    @db = db
  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<commit_msg>Fix file path issue caused by dbm implicitly appending `.db` to end of file path provided.<commit_after>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). 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
  DATABASE_MODE = 0664

  def self.use(type)
    return_value = nil

    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(dbm_file_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 the cache. Closes the database after use if it has been
  # loaded
  #
  # @param  [Symbol] type
  # @yield  [DatabaseCache] self
  # @return [nil]
  def initialize(type)
    @type = type
    yield(self)
    @db&.close
  end

  # `DBM` appends `.db` file extension to the path provided, which is why it's
  # not included
  #
  # @return [String]
  def dbm_file_path
    File.join(HOMEBREW_CACHE, @type.to_s)
  end

  # The path where the database resides in the `HOMEBREW_CACHE` for the given
  # `@type`
  #
  # @return [String]
  def cache_path
    "#{dbm_file_path}.db"
  end
end

#
# `CacheStore` provides methods to mutate and fetch data from a persistent
# storage mechanism
#
class CacheStore
  # @param  [DBM] db
  # @return [nil]
  def initialize(db)
    @db = db
  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
{'fraction_non_alphanumeric': 0.0835030549898167, 'fraction_numerical': 0.006788866259334691, 'mean_word_length': 3.341846758349705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6488037', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1410, 'n_words': 531}