diff options
Diffstat (limited to 'lib/rubygems/source_info_cache.rb')
-rw-r--r-- | lib/rubygems/source_info_cache.rb | 74 |
1 files changed, 52 insertions, 22 deletions
diff --git a/lib/rubygems/source_info_cache.rb b/lib/rubygems/source_info_cache.rb index 9383f6362e..4d69d88c89 100644 --- a/lib/rubygems/source_info_cache.rb +++ b/lib/rubygems/source_info_cache.rb @@ -22,8 +22,8 @@ require 'rubygems/user_interaction' # Gem::SourceInfoCache # @cache_data = { # source_uri => Gem::SourceInfoCacheEntry -# @size => source index size -# @source_index => Gem::SourceIndex +# @size = source index size +# @source_index = Gem::SourceIndex # ... # } @@ -31,14 +31,14 @@ class Gem::SourceInfoCache include Gem::UserInteraction - @cache = nil - @system_cache_file = nil - @user_cache_file = nil + ## + # The singleton Gem::SourceInfoCache. If +all+ is true, a full refresh will + # be performed if the singleton instance is being initialized. - def self.cache + def self.cache(all = false) return @cache if @cache @cache = new - @cache.refresh false if Gem.configuration.update_sources + @cache.refresh all if Gem.configuration.update_sources @cache end @@ -63,6 +63,15 @@ class Gem::SourceInfoCache end ## + # Reset all singletons, discarding any changes. + + def self.reset + @cache = nil + @system_cache_file = nil + @user_cache_file = nil + end + + ## # Search all source indexes. See Gem::SourceInfoCache#search. def self.search(*args) @@ -122,13 +131,6 @@ class Gem::SourceInfoCache try_file(user_cache_file) or raise "unable to locate a writable cache file") end - - ## - # Force cache file to be reset, useful for integration testing of rubygems - - def reset_cache_file - @cache_file = nil - end ## # Write the cache to a local file (if it is dirty). @@ -175,13 +177,30 @@ class Gem::SourceInfoCache self.class.latest_user_cache_file end + ## + # Merges the complete cache file into this Gem::SourceInfoCache. + def read_all_cache_data if @only_latest then @only_latest = false - @cache_data = read_cache_data cache_file + all_data = read_cache_data cache_file + + cache_data.update all_data do |source_uri, latest_sice, all_sice| + all_sice.source_index.gems.update latest_sice.source_index.gems + + Gem::SourceInfoCacheEntry.new all_sice.source_index, latest_sice.size + end + + begin + refresh true + rescue Gem::RemoteFetcher::FetchError + end end end + ## + # Reads cached data from +file+. + def read_cache_data(file) # Marshal loads 30-40% faster from a String, and 2MB on 20061116 is small data = open file, 'rb' do |fp| fp.read end @@ -203,6 +222,8 @@ class Gem::SourceInfoCache end cache_data + rescue Errno::ENOENT + {} rescue => e if Gem.configuration.really_verbose then say "Exception during cache_data handling: #{e.class} - #{e}" @@ -243,6 +264,14 @@ class Gem::SourceInfoCache def reset_cache_data @cache_data = nil + @only_latest = true + end + + ## + # Force cache file to be reset, useful for integration testing of rubygems + + def reset_cache_file + @cache_file = nil end ## @@ -315,10 +344,7 @@ class Gem::SourceInfoCache end end - if File.writable? dir then - open path, "wb" do |io| io.write Marshal.dump({}) end - return path - end + return path if File.writable? dir nil end @@ -338,11 +364,13 @@ class Gem::SourceInfoCache end ## - # Write data to the proper cache. + # Write data to the proper cache files. def write_cache - open cache_file, 'wb' do |io| - io.write Marshal.dump(cache_data) + if not File.exist?(cache_file) or not @only_latest then + open cache_file, 'wb' do |io| + io.write Marshal.dump(cache_data) + end end open latest_cache_file, 'wb' do |io| @@ -350,5 +378,7 @@ class Gem::SourceInfoCache end end + reset + end |