Class: Amp::Repositories::Stores::FilenameCache::FilenameCacheOpener

Inherits:
Opener
  • Object
show all
Defined in:
lib/amp/repository/store.rb

Overview

FilenameCacheOpener

This opener handles a cache of filenames that we are currently tracking. This way we don’t need to recursively walk though the folders every single time. To use this class, you pass in the real Opener object (that responds to #open and returns a file pointer). then just treat it like any other opener. It will handle the behind-the-scenes work itself.

Instance Attribute Summary

Attributes inherited from Opener

#create_mode, #default

Instance Method Summary collapse

Methods inherited from Opener

#join, #open_file, #open_hg, #open_up_file, #read

Constructor Details

#initialize(opener) ⇒ FilenameCacheOpener

Initializes a new FNCacheOpener. Requires a normal object capable of opening files.

Parameters:

  • opener (Amp::Opener)

    an opener object initialized to the appropriate root directory.



219
220
221
222
# File 'lib/amp/repository/store.rb', line 219

def initialize(opener)
  @opener = opener
  @entries = nil
end

Instance Method Details

#load_filename_cacheObject

Parses the filename cache and loads it into an ivar.



229
230
231
232
233
234
# File 'lib/amp/repository/store.rb', line 229

def load_filename_cache
  @entries = {}
  FilenameCache.parse @opener do |f|
    @entries[f] = true
  end
end

#open(path, mode = 'r', &block) ⇒ Object

Opens a file while being sure to write the filename if we haven’t seen it before. Just like the normal Opener’s open() method.

Parameters:

  • path (String)

    the path to the file

  • mode (Fixnum) (defaults to: 'r')

    the read/write/append mode

  • block

    the block to pass to it (optional)



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/amp/repository/store.rb', line 243

def open(path, mode='r', &block)
  
  if mode !~ /r/ && path =~ /data\//
    load_filename_cache if @entries.nil?
    if @entries[path].nil?
      @opener.open('fncache','ab') {|f| f.puts path }
      @entries[path] = true
    end
  end
  
  begin
    @opener.open(Stores.hybrid_encode(path), mode, &block)
  rescue Errno::ENOENT
    raise
  rescue
    raise unless mode == 'r'
  end
rescue
  raise
end

#pathObject Also known as: root



224
# File 'lib/amp/repository/store.rb', line 224

def path; @opener.path; end