diff options
Diffstat (limited to 'lib/metropolis/tc/hdb/ro.rb')
-rw-r--r-- | lib/metropolis/tc/hdb/ro.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/metropolis/tc/hdb/ro.rb b/lib/metropolis/tc/hdb/ro.rb new file mode 100644 index 0000000..f348ee4 --- /dev/null +++ b/lib/metropolis/tc/hdb/ro.rb @@ -0,0 +1,34 @@ +# -*- encoding: binary -*- + +module Metropolis::TC::HDB::RO + def self.extended(obj) + obj.instance_eval do + @wr_flags = nil + @rd_flags |= TokyoCabinet::HDB::ONOLCK + @dbv.each { |(hdb, path)| + hdb.open(path, @rd_flags) or ex!(:open, path) + } + @ro_dbv = @dbv.map { |(hdb,_)| hdb } + end + end + + def call(env) + if %r{\A/(.*)\z} =~ env["PATH_INFO"] + key = unescape($1) + case env["REQUEST_METHOD"] + when "GET" + get(key) + when "HEAD" + head(key) + else + [ 405, {}, [] ] + end + else # OPTIONS + [ 405, {}, [] ] + end + end + + def reader(key) + yield @ro_dbv[key.hash % @nr_slots] + end +end |