about summary refs log tree commit
path: root/lib/metropolis/tdb.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-06 15:43:46 -0800
committerEric Wong <normalperson@yhbt.net>2010-12-06 15:43:46 -0800
commit88fa90b7f039f62962cc8d11031446412b951be2 (patch)
tree04c2d961f6e3e41b069f074d7b93e1d91d8d716e /lib/metropolis/tdb.rb
parent74ca34be2d54809822447ff185d88d83fcd566ae (diff)
downloadmetropolis-88fa90b7f039f62962cc8d11031446412b951be2.tar.gz
allow easier, single-file options for TC and TDB
Most (other) users only need a single file, even though
my primary use of this is for multiple files.
Diffstat (limited to 'lib/metropolis/tdb.rb')
-rw-r--r--lib/metropolis/tdb.rb29
1 files changed, 8 insertions, 21 deletions
diff --git a/lib/metropolis/tdb.rb b/lib/metropolis/tdb.rb
index 14aa3aa..d734443 100644
--- a/lib/metropolis/tdb.rb
+++ b/lib/metropolis/tdb.rb
@@ -4,44 +4,35 @@ require 'tdb'
 
 module Metropolis::TDB
   include Metropolis::Common
+  autoload :Single, 'metropolis/tdb/single'
+  autoload :Multi, 'metropolis/tdb/multi'
 
   def setup(opts)
     super
-    path_pattern = opts[:path_pattern]
-    path_pattern.scan(/%\d*x/).size == 1 or
-      raise ArgumentError, "only one '/%\d*x/' may appear in #{path_pattern}"
     @tdb_opts = { :tdb_flags => 0 }
     if @readonly
       @tdb_opts[:open_flags] = IO::RDONLY
       extend Metropolis::Common::RO
     end
-    if query = opts[:query]
-      size = query['hash_size'] and @tdb_opts[:hash_size] = size.to_i
-      hash = query['hash'] and @tdb_opts[:hash] = hash.to_sym
+    if @query
+      size = @query['hash_size'] and @tdb_opts[:hash_size] = size.to_i
+      hash = @query['hash'] and @tdb_opts[:hash] = hash.to_sym
 
-      case query['volatile']
+      case @query['volatile']
       when 'true'; @tdb_opts[:tdb_flags] |= TDB::VOLATILE
       when 'false', nil
       else
         raise ArgumentError, "'volatile' must be 'true' or 'false'"
       end
 
-      case query['sync']
+      case @query['sync']
       when 'true', nil
       when 'false'; @tdb_opts[:tdb_flags] |= TDB::NOSYNC
       else
         raise ArgumentError, "'sync' must be 'true' or 'false'"
       end
     end
-
-    @dbv = (0...@nr_slots).to_a.map do |slot|
-      path = sprintf(path_pattern, slot)
-      ::TDB.new(path, @tdb_opts)
-    end
-  end
-
-  def db(key, &block)
-    yield @dbv[key.hash % @nr_slots]
+    extend(@path_pattern ? Metropolis::TDB::Multi : Metropolis::TDB::Single)
   end
 
   def put(key, env)
@@ -67,8 +58,4 @@ module Metropolis::TDB
     value = db(key) { |tdb| tdb.fetch(key) } or return r(404)
     [ 200, { 'Content-Length' => value.size.to_s }.merge!(@headers), [ value ] ]
   end
-
-  def close!
-    @dbv.each { |tdb| tdb.close }
-  end
 end