| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | require "dbm" | 
					
						
							|  |  |  | require "json" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  | # `CacheStoreDatabase` acts as an interface to a persistent storage mechanism | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | # residing in the `HOMEBREW_CACHE` | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  |     database = CacheStoreDatabase.new(type) | 
					
						
							|  |  |  |     return_value = yield(database) | 
					
						
							|  |  |  |     database.close_if_open! | 
					
						
							|  |  |  |     return_value | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Sets a value in the underlying database (and creates it if necessary). | 
					
						
							|  |  |  |   def set(key, value) | 
					
						
							|  |  |  |     db[key] = value | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Gets a value from the underlying database (if it already exists). | 
					
						
							|  |  |  |   def get(key) | 
					
						
							|  |  |  |     return unless created? | 
					
						
							|  |  |  |     db[key] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Gets a value from the underlying database (if it already exists). | 
					
						
							|  |  |  |   def delete(key) | 
					
						
							|  |  |  |     return unless created? | 
					
						
							|  |  |  |     db.delete(key) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Closes the underlying database (if it created and open). | 
					
						
							|  |  |  |   def close_if_open! | 
					
						
							|  |  |  |     @db&.close | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Returns `true` if the cache file has been created for the given `@type` | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @return [Boolean] | 
					
						
							|  |  |  |   def created? | 
					
						
							|  |  |  |     File.exist?(cache_path) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   # 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 16:37:01 -04:00
										 |  |  |   # 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 | 
					
						
							| 
									
										
										
										
											2018-06-21 13:59:07 +01:00
										 |  |  |     @db ||= begin | 
					
						
							|  |  |  |       HOMEBREW_CACHE.mkpath | 
					
						
							|  |  |  |       DBM.open(dbm_file_path, DATABASE_MODE, DBM::WRCREAT) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   # Creates a CacheStoreDatabase | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   # @param  [Symbol] type | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   # @return [nil] | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   def initialize(type) | 
					
						
							|  |  |  |     @type = type | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-21 12:13:03 -04:00
										 |  |  |   # `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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 16:37:01 -04:00
										 |  |  |   # The path where the database resides in the `HOMEBREW_CACHE` for the given | 
					
						
							|  |  |  |   # `@type` | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @return [String] | 
					
						
							|  |  |  |   def cache_path | 
					
						
							| 
									
										
										
										
											2018-05-21 12:13:03 -04:00
										 |  |  |     "#{dbm_file_path}.db" | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # `CacheStore` provides methods to mutate and fetch data from a persistent | 
					
						
							|  |  |  | # storage mechanism | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 14:46:14 +01:00
										 |  |  |   # @return [CacheStoreDatabase] | 
					
						
							|  |  |  |   attr_reader :database | 
					
						
							| 
									
										
										
										
											2018-02-28 10:39:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   # 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 |