about summary refs log tree commit
path: root/lib/metropolis/tc/hdb/ro.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-23 12:09:56 -0800
committerEric Wong <normalperson@yhbt.net>2010-11-23 12:09:56 -0800
commitdf91c57c312bee97a16bced1035bd704e518ac38 (patch)
tree0678c58830c09e9a8e0de92a33fa078f2445ee4e /lib/metropolis/tc/hdb/ro.rb
parent51d43461adca99482154ddbd337658a70d5ffc3d (diff)
downloadmetropolis-df91c57c312bee97a16bced1035bd704e518ac38.tar.gz
rename internal "TokyoCabinet" => "TC"
It's less typing and less likely to clash on both
the eyes and the interpreter.
Diffstat (limited to 'lib/metropolis/tc/hdb/ro.rb')
-rw-r--r--lib/metropolis/tc/hdb/ro.rb34
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