class WebVac::Table

Sits in front of Redis (just Redis right now), and handles the mapping of vac hashes to pathnames, as well as the metadata (in JSON and in the form of HTTP headers, which allows HEAD requests to be cheap). Also does some of the bookkeeping necessary for that, like the interaction with libmagic.

Relatively threadsafe, but maintains one Redis connection per active thread (created on demand).

Attributes

config[R]

Public Class Methods

new(cfg) click to toggle source

Takes an instance of Config.

# File lib/webvac.rb, line 119
def initialize cfg
        @config = cfg
end

Public Instance Methods

fn2md(f) click to toggle source

Takes a filename, returns the filename's metadata. Stateless-ish.

# File lib/webvac.rb, line 124
def fn2md f
        s = File.stat(f)
        m = {
                'Content-Type' => Magic.guess_file_mime_type(f),
                'Content-Length' => s.size.to_s,
                'Last-Modified' => s.mtime.rfc822,
        } rescue nil
end
guess_mime(contents) click to toggle source
# File lib/webvac.rb, line 159
def guess_mime contents
        Magic.guess_string_mime_type(contents)
end
meta_save!(fn, sc) click to toggle source
# File lib/webvac.rb, line 133
def meta_save! fn, sc
        md = fn2md(fn)
        return unless md
        redis.call 'HSET', 'score2md', sc, md.to_json
end
metadata(score) click to toggle source
# File lib/webvac.rb, line 139
def metadata score
        # Overall, doesn't really matter if this fails.
        JSON.parse(
                redis.call('HGET', 'score2md', score.sub(/^vac:/, ''))
        ) rescue nil
end
path2score(p) click to toggle source
# File lib/webvac.rb, line 154
def path2score p
        r = redis.call 'HGET', 'path2score', p
        return "vac:#{r}" if r
end
rec_score!(fn, sc) click to toggle source
# File lib/webvac.rb, line 146
def rec_score! fn, sc
        redis.call 'HSET', 'path2score', fn, sc
end
redis() click to toggle source
# File lib/webvac.rb, line 150
def redis
        Thread.current[:webvac_redis] ||= Redic.new(config.redis_url)
end