| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | require "json" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  | # {CacheStoreDatabase} acts as an interface to a persistent storage mechanism | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  | # residing in the `HOMEBREW_CACHE`. | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  | class CacheStoreDatabase | 
					
						
							|  |  |  |   # Yields the cache store database. | 
					
						
							|  |  |  |   # Closes the database after use if it has been loaded. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @param  [Symbol] type | 
					
						
							|  |  |  |   # @yield  [CacheStoreDatabase] self | 
					
						
							|  |  |  |   def self.use(type) | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |     @db_type_reference_hash ||= {} | 
					
						
							|  |  |  |     @db_type_reference_hash[type] ||= {} | 
					
						
							|  |  |  |     type_ref = @db_type_reference_hash[type] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     type_ref[:count] ||= 0
 | 
					
						
							|  |  |  |     type_ref[:count]  += 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     type_ref[:db] ||= CacheStoreDatabase.new(type) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return_value = yield(type_ref[:db]) | 
					
						
							|  |  |  |     if type_ref[:count].positive? | 
					
						
							|  |  |  |       type_ref[:count] -= 1
 | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       type_ref[:count] = 0
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if type_ref[:count].zero? | 
					
						
							|  |  |  |       type_ref[:db].write_if_dirty! | 
					
						
							|  |  |  |       type_ref.delete(:db) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |     return_value | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-28 13:34:07 +00:00
										 |  |  |   # Creates a CacheStoreDatabase. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @param  [Symbol] type | 
					
						
							|  |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def initialize(type) | 
					
						
							|  |  |  |     @type = type | 
					
						
							|  |  |  |     @dirty = false | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   # Sets a value in the underlying database (and creates it if necessary). | 
					
						
							|  |  |  |   def set(key, value) | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |     dirty! | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |     db[key] = value | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Gets a value from the underlying database (if it already exists). | 
					
						
							|  |  |  |   def get(key) | 
					
						
							|  |  |  |     return unless created? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |     db[key] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Gets a value from the underlying database (if it already exists). | 
					
						
							|  |  |  |   def delete(key) | 
					
						
							|  |  |  |     return unless created? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |     dirty! | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |     db.delete(key) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 20:19:30 +01:00
										 |  |  |   # Closes the underlying database (if it is created and open). | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |   def write_if_dirty! | 
					
						
							|  |  |  |     return unless dirty? | 
					
						
							| 
									
										
										
										
											2019-02-19 13:12:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-14 00:29:02 +02:00
										 |  |  |     cache_path.dirname.mkpath | 
					
						
							| 
									
										
										
										
											2018-09-25 20:19:30 +01:00
										 |  |  |     cache_path.atomic_write(JSON.dump(@db)) | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Returns `true` if the cache file has been created for the given `@type`. | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @return [Boolean] | 
					
						
							|  |  |  |   def created? | 
					
						
							| 
									
										
										
										
											2018-09-20 10:57:27 +01:00
										 |  |  |     cache_path.exist? | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # Returns the modification time of the cache file (if it already exists). | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @return [Time] | 
					
						
							|  |  |  |   def mtime | 
					
						
							|  |  |  |     return unless created? | 
					
						
							| 
									
										
										
										
											2019-02-19 13:12:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     cache_path.mtime | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Performs a `select` on the underlying database. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @return [Array] | 
					
						
							|  |  |  |   def select(&block) | 
					
						
							|  |  |  |     db.select(&block) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Returns `true` if the cache is empty. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @return [Boolean] | 
					
						
							|  |  |  |   def empty? | 
					
						
							|  |  |  |     db.empty? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |   # Performs a `each_key` on the underlying database. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @return [Array] | 
					
						
							|  |  |  |   def each_key(&block) | 
					
						
							|  |  |  |     db.each_key(&block) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   # Lazily loaded database in read/write mode. If this method is called, a | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # database file will be created in the `HOMEBREW_CACHE` with a name | 
					
						
							|  |  |  |   # corresponding to the `@type` instance variable. | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-09-25 20:19:30 +01:00
										 |  |  |   # @return [Hash] db | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   def db | 
					
						
							| 
									
										
										
										
											2018-06-21 13:59:07 +01:00
										 |  |  |     @db ||= begin | 
					
						
							| 
									
										
										
										
											2018-09-25 20:19:30 +01:00
										 |  |  |       JSON.parse(cache_path.read) if created? | 
					
						
							|  |  |  |     rescue JSON::ParserError | 
					
						
							|  |  |  |       nil | 
					
						
							| 
									
										
										
										
											2018-06-21 13:59:07 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-09-25 20:19:30 +01:00
										 |  |  |     @db ||= {} | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # The path where the database resides in the `HOMEBREW_CACHE` for the given | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # `@type`. | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @return [String] | 
					
						
							|  |  |  |   def cache_path | 
					
						
							| 
									
										
										
										
											2018-09-25 20:19:30 +01:00
										 |  |  |     HOMEBREW_CACHE/"#{@type}.json" | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Sets that the cache needs to be written to disk. | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |   def dirty! | 
					
						
							|  |  |  |     @dirty = true | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Returns `true` if the cache needs to be written to disk. | 
					
						
							| 
									
										
										
										
											2020-09-11 12:56:15 +01:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @return [Boolean] | 
					
						
							|  |  |  |   def dirty? | 
					
						
							|  |  |  |     @dirty | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  | # {CacheStore} provides methods to mutate and fetch data from a persistent | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  | # storage mechanism. | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | # | 
					
						
							|  |  |  | class CacheStore | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   # @param  [CacheStoreDatabase] database | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   # @return [nil] | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   def initialize(database) | 
					
						
							|  |  |  |     @database = database | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Inserts new values or updates existing cached values to persistent storage. | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @abstract | 
					
						
							|  |  |  |   def update!(*) | 
					
						
							|  |  |  |     raise NotImplementedError | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Fetches cached values in persistent storage according to the type of data | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # stored. | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @abstract | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   def fetch(*) | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |     raise NotImplementedError | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Deletes data from the cache based on a condition defined in a concrete class. | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   # | 
					
						
							|  |  |  |   # @abstract | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   def delete!(*) | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |     raise NotImplementedError | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   protected | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   # @return [CacheStoreDatabase] | 
					
						
							|  |  |  |   attr_reader :database | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | end |