class Aeternitas::StorageAdapter::File

A storage adapter that stores the entries on disk.

Public Class Methods

new(config) click to toggle source

Create a new File storage adapter. @param [Hash] config the adapters config @option config [String] :directory specifies where the entries are stored

Calls superclass method Aeternitas::StorageAdapter::new
# File lib/aeternitas/storage_adapter/file.rb, line 9
def initialize(config)
  super
end

Public Instance Methods

content_size(id) click to toggle source

Returns the raw_content's size in bytes @param [String] id the entries fingerprint @return [Integer] the entries size in byte

# File lib/aeternitas/storage_adapter/file.rb, line 42
def content_size(id)
  retrieve(id).bytesize
end
delete(id) click to toggle source
# File lib/aeternitas/storage_adapter/file.rb, line 27
def delete(id)
  begin
    !!::File.delete(file_path(id))
  rescue Errno::ENOENT => e
    return false
  end
end
exist?(id) click to toggle source
# File lib/aeternitas/storage_adapter/file.rb, line 35
def exist?(id)
  ::File.exist?(file_path(id))
end
file_size_disk(id) click to toggle source

Returns the raw_content compressed size in bytes @param [String] id the entries fingerprint @return [Integer] the entries size on disk in byte

# File lib/aeternitas/storage_adapter/file.rb, line 49
def file_size_disk(id)
  ::File.size(file_path(id))
end
retrieve(id) click to toggle source
# File lib/aeternitas/storage_adapter/file.rb, line 22
def retrieve(id)
  raise(Aeternitas::Errors::SourceDataNotFound, id) unless exist?(id)
  Zlib.inflate(::File.read(file_path(id), encoding: 'ascii-8bit'))
end
store(id, raw_content) click to toggle source
# File lib/aeternitas/storage_adapter/file.rb, line 13
def store(id, raw_content)
  path = file_path(id)
  ensure_folders_exist(path)
  raise(Aeternitas::Errors::SourceDataExists, id) if ::File.exist?(path)
  ::File.open(path, 'w+', encoding: 'ascii-8bit') do |f|
    f.write(Zlib.deflate(raw_content, Zlib::BEST_COMPRESSION))
  end
end

Private Instance Methods

ensure_folders_exist(path) click to toggle source

Makes sure that the storage location exists.

# File lib/aeternitas/storage_adapter/file.rb, line 67
def ensure_folders_exist(path)
  folders = ::File.dirname(path)
  FileUtils.mkdir_p(folders) unless Dir.exist?(folders)
end
file_path(id) click to toggle source

Calculates the location of the raw_content file given it's fingerprint. @param [String] id the entries fingerprint @return [String] the entries location

# File lib/aeternitas/storage_adapter/file.rb, line 58
def file_path(id)
  ::File.join(
      @config[:directory],
      id[0..1], id[2..3], id[4..5],
      id[6..-1]
  )
end