Sha256: 549c74e131d8490f74e54455a18c83c0f4f946a11ade4b1b25f3151abe0d6e30

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

# -*- encoding: binary -*-
module Regurgitator::FileInfo
  include Regurgitator::Domain
  include Regurgitator::Device

  # :stopdoc:
  FILE_INFO = 'SELECT fid,length FROM file WHERE dmid = ? AND dkey = ? LIMIT 1'
  # :startdoc:

  # returns a hash with file information and URIs for accessing the file:
  # {
  #   :fid => 3149113,
  #   :length => 17,
  #   :uris => {
  #     "east" => [ [read_write,read_only], [read_write,read_only] ],
  #     "west" => [ [read_write,read_only], [read_write,read_only] ],
  #     nil => [ [read_write,read_only], [read_write,read_only] ],
  #   }
  # }
  def file_info(env, domain, dkey, update = false)
    dmid = get_dmid(domain) or return
    devices = refresh_device(update)
    info = @db[FILE_INFO, dmid, dkey].first or return
    0 == info[:length] and return info
    fid = info[:fid]
    read_uris = Hash.new { |h,k| h[k] = [] }
    drain_uris = Hash.new { |h,k| h[k] = [] }

    zone = env['regurgitator.zone']

    @db[DEVICES_ON, fid].each do |x|
      devinfo = devices[x[:devid]] or next
      fid >= 10_000_000_000 and next # TODO: support larger FIDs

      uris = devinfo[:uris][alt_zone?(zone, devinfo) ? :alt : :pri]
      nfid = sprintf('%010u', fid)
      /\A(\d)(\d{3})(\d{3})(?:\d{3})\z/ =~ nfid
      fid_path = "/#$1/#$2/#$3/#{nfid}.fid"
      uris = uris.map do |u|
        u = u.dup
        u.path += fid_path # must be a copy
        u
      end
      (devinfo[:preferred] ? read_uris : drain_uris)[devinfo[:zone]] << uris
    end
    uris = info[:uris] = read_uris.empty? ? drain_uris : read_uris
    (uris.empty? && ! update) ? file_info(env, domain, dkey, true) : info
  end

  def alt_zone?(zone, devinfo)
    zone == :alt || devinfo[:ipaddr][:altmask].include?(zone)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
regurgitator-0.0.0 lib/regurgitator/file_info.rb