Class: WebVac::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/webvac.rb

Overview

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).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg) ⇒ Table

Takes an instance of Config.



119
120
121
# File 'lib/webvac.rb', line 119

def initialize cfg
	@config = cfg
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



116
117
118
# File 'lib/webvac.rb', line 116

def config
  @config
end

Instance Method Details

#fn2md(f) ⇒ Object

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



124
125
126
127
128
129
130
131
# 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) ⇒ Object



159
160
161
# File 'lib/webvac.rb', line 159

def guess_mime contents
	Magic.guess_string_mime_type(contents)
end

#meta_save!(fn, sc) ⇒ Object



133
134
135
136
137
# 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) ⇒ Object



139
140
141
142
143
144
# File 'lib/webvac.rb', line 139

def  score
	# Overall, doesn't really matter if this fails.
	JSON.parse(
		redis.call('HGET', 'score2md', score.sub(/^vac:/, ''))
	) rescue nil
end

#path2score(p) ⇒ Object



154
155
156
157
# 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) ⇒ Object



146
147
148
# File 'lib/webvac.rb', line 146

def rec_score! fn, sc
	redis.call 'HSET', 'path2score', fn, sc
end

#redisObject



150
151
152
# File 'lib/webvac.rb', line 150

def redis
	Thread.current[:webvac_redis] ||= Redic.new(config.redis_url)
end