Class: CacheStoreDatabase Private

Inherits:
Object show all
Extended by:
T::Generic
Defined in:
cache_store.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

CacheStoreDatabase acts as an interface to a persistent storage mechanism residing in the HOMEBREW_CACHE.

Constant Summary collapse

Key =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

type_member
Value =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

type_member

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Creates a CacheStoreDatabase.

Parameters:



66
67
68
69
# File 'cache_store.rb', line 66

def initialize(type)
  @type = type
  @dirty = T.let(false, T.nilable(T::Boolean))
end

Instance Attribute Details

#db=(value) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



150
151
152
# File 'cache_store.rb', line 150

def db=(value)
  @db = value
end

Class Method Details

.use(type, &_blk) ⇒ T.type_parameter(:U)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Yields the cache store database. Closes the database after use if it has been loaded.

Parameters:

Returns:

  • (T.type_parameter(:U))


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'cache_store.rb', line 38

def self.use(type, &_blk)
  type_ref = @type_references_mutex.synchronize do
    @type_references[type] ||= TypeReference.new(mutex: Thread::Mutex.new)
  end

  db = type_ref.mutex.synchronize do
    type_ref.active_users += 1
    type_ref.database ||= CacheStoreDatabase.new(type)
  end

  begin
    return_value = yield(db)
    return_value
  ensure
    # `break` from the block used to skip the decrement and leak the refcount.
    type_ref.mutex.synchronize do
      type_ref.active_users -= 1
      if type_ref.active_users.zero?
        # Prevent a replacement database from opening until the final write completes.
        type_ref.database&.write_if_dirty!
        type_ref.database = nil
      end
    end
  end
end

Instance Method Details

#clear!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Deletes all content from the underlying database (if it already exists).



97
98
99
100
101
102
# File 'cache_store.rb', line 97

def clear!
  return unless created?

  dirty!
  db.clear
end

#created?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the cache file has been created for the given @type.

Returns:

  • (Boolean)


115
116
117
# File 'cache_store.rb', line 115

def created?
  cache_path.exist?
end

#delete(key) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Deletes a value from the underlying database (if it already exists).

Parameters:



88
89
90
91
92
93
# File 'cache_store.rb', line 88

def delete(key)
  return unless created?

  dirty!
  db.delete(key)
end

#each_key(&block) ⇒ Hash{Key => Value}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Performs a each_key on the underlying database.

Parameters:

  • block (T.proc.params(arg0: Key).returns(BasicObject))

Returns:



145
146
147
# File 'cache_store.rb', line 145

def each_key(&block)
  db.each_key(&block)
end

#empty?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns true if the cache is empty.

Returns:

  • (Boolean)


137
138
139
# File 'cache_store.rb', line 137

def empty?
  db.empty?
end

#get(key) ⇒ Value?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Gets a value from the underlying database (if it already exists).

Parameters:

Returns:



80
81
82
83
84
# File 'cache_store.rb', line 80

def get(key)
  return unless created?

  db[key]
end

#mtimeTime?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the modification time of the cache file (if it already exists).

Returns:



121
122
123
124
125
# File 'cache_store.rb', line 121

def mtime
  return unless created?

  cache_path.mtime
end

#select(&block) ⇒ Hash{Key => Value}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Performs a select on the underlying database.

Parameters:

  • block (T.proc.params(arg0: Key, arg1: Value).returns(BasicObject))

Returns:



131
132
133
# File 'cache_store.rb', line 131

def select(&block)
  db.select(&block)
end

#set(key, value) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Sets a value in the underlying database (and creates it if necessary).

Parameters:



73
74
75
76
# File 'cache_store.rb', line 73

def set(key, value)
  dirty!
  db[key] = value
end

#write_if_dirty!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Closes the underlying database (if it is created and open).



106
107
108
109
110
111
# File 'cache_store.rb', line 106

def write_if_dirty!
  return unless dirty?

  cache_path.dirname.mkpath
  cache_path.atomic_write(JSON.dump(@db))
end