about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-05 15:34:56 -0800
committerEric Wong <normalperson@yhbt.net>2010-12-05 15:41:37 -0800
commit74ca34be2d54809822447ff185d88d83fcd566ae (patch)
tree6d062e8b82b72996a5e53840f7e3517ddaaaae66
parenta84908349094bd8835d3261cd8b11dde863178fd (diff)
downloadmetropolis-74ca34be2d54809822447ff185d88d83fcd566ae.tar.gz
:path_pattern is independent of the URI
It can be confusing with printf-style patterns in URIs,
so just force path_pattern-users to specify "/" as their
path.
-rw-r--r--lib/metropolis.rb7
-rw-r--r--test/rack_read_write.rb10
-rw-r--r--test/test_hash.rb3
-rw-r--r--test/test_tc_hdb.rb21
-rw-r--r--test/test_tdb.rb3
5 files changed, 26 insertions, 18 deletions
diff --git a/lib/metropolis.rb b/lib/metropolis.rb
index e9810d7..425ce1c 100644
--- a/lib/metropolis.rb
+++ b/lib/metropolis.rb
@@ -14,18 +14,19 @@ module Metropolis
     opts = opts.dup
     rv = Object.new
     uri = opts[:uri] = URI.parse(opts[:uri])
+    if uri.path != '/' && opts[:path_pattern]
+      raise ArgumentError, ":path_pattern may only be used if path is '/'"
+    end
     case uri.scheme
     when 'hash'
       opts[:path] = uri.path if uri.path != '/'
       rv.extend Metropolis::Hash
     when 'tdb'
-      opts[:path_pattern] = uri.path
       opts[:query] = Rack::Utils.parse_query(uri.query) if uri.query
       rv.extend Metropolis::TDB
     when 'tc'
-      opts[:path_pattern] = uri.path
       opts[:query] = Rack::Utils.parse_query(uri.query) if uri.query
-      case ext = File.extname(uri.path)
+      case ext = File.extname(opts[:path_pattern] || uri.path)
       when '.tch'
         rv.extend Metropolis::TC::HDB
       else
diff --git a/test/rack_read_write.rb b/test/rack_read_write.rb
index 46c1764..9031b6d 100644
--- a/test/rack_read_write.rb
+++ b/test/rack_read_write.rb
@@ -8,7 +8,7 @@ module TestRackReadWrite
   attr_reader :app
 
   def test_rack_read_write_deflated
-    @app = Metropolis.new(:uri => uri, :encoding => :deflate)
+    @app = Metropolis.new(@app_opts.merge(:encoding => :deflate))
     basic_rest
 
     blob = "." * 1024 * 1024
@@ -31,7 +31,7 @@ module TestRackReadWrite
   end
 
   def test_rack_read_write_gzipped
-    @app = Metropolis.new(:uri => uri, :encoding => :gzip)
+    @app = Metropolis.new(@app_opts.merge(:encoding => :gzip))
     basic_rest
 
     blob = "." * 1024 * 1024
@@ -54,7 +54,7 @@ module TestRackReadWrite
   end
 
   def test_rack_read_write
-    @app = Metropolis.new(:uri => uri)
+    @app = Metropolis.new(@app_opts)
     basic_rest
   end
 
@@ -92,9 +92,9 @@ module TestRackReadWrite
   end
 
   def test_rack_readonly
-    tmp = Metropolis.new(:uri => uri)
+    tmp = Metropolis.new(@app_opts)
     tmp.close!
-    @app = Metropolis.new(:uri => uri, :readonly => true)
+    @app = Metropolis.new(@app_opts.merge(:readonly => true))
     basic_rest_readonly
   end
 
diff --git a/test/test_hash.rb b/test/test_hash.rb
index cf11e82..8c89375 100644
--- a/test/test_hash.rb
+++ b/test/test_hash.rb
@@ -11,6 +11,7 @@ class Test_Hash < Test::Unit::TestCase
     @tmp = Tempfile.new('hash')
     File.unlink(@tmp)
     @uri = "hash://#{@tmp.path}"
+    @app_opts = { :uri => @uri }
   end
 
   def teardown
@@ -19,7 +20,7 @@ class Test_Hash < Test::Unit::TestCase
 
   def test_marshalled
     File.open(@tmp, "wb") { |fp| fp.write(Marshal.dump({"x" => "y"})) }
-    app = Metropolis.new(:uri => @uri, :readonly => true)
+    app = Metropolis.new(@app_opts.merge(:readonly => true))
     o = { :lint => true, :fatal => true }
     req = Rack::MockRequest.new(app)
 
diff --git a/test/test_tc_hdb.rb b/test/test_tc_hdb.rb
index c272093..a5d27b8 100644
--- a/test/test_tc_hdb.rb
+++ b/test/test_tc_hdb.rb
@@ -12,7 +12,8 @@ class Test_TC_HDB < Test::Unit::TestCase
     tmp = Tempfile.new('tchdb')
     @path_pattern = tmp.path + ".%01x.tch"
     tmp.close!
-    @uri = "tc://#{@path_pattern}"
+    @uri = "tc:///"
+    @app_opts = { :path_pattern => @path_pattern, :uri => @uri }
   end
 
   def teardown
@@ -168,11 +169,11 @@ class Test_TC_HDB < Test::Unit::TestCase
     nr_bytes = 1024 * 1024 * 20
     data = "0" * nr_bytes
     obj = nil
-    assert_nothing_raised { obj = Metropolis.new(:uri => uri) }
+    assert_nothing_raised { obj = Metropolis.new(@app_opts) }
 
     query = "large=true&apow=3&bnum=65536&compress=deflate"
     assert_nothing_raised {
-      obj = Metropolis.new(:uri => "#{uri}?#{query}")
+      obj = Metropolis.new(@app_opts.merge(:uri => "#{uri}?#{query}"))
     }
     optimize_args = obj.instance_variable_get(:@optimize)
     flags = TokyoCabinet::HDB::TLARGE | TokyoCabinet::HDB::TDEFLATE
@@ -183,11 +184,13 @@ class Test_TC_HDB < Test::Unit::TestCase
     assert_nothing_raised { obj.get(k, {}) }
     assert_nothing_raised { obj.put(k,{'rack.input' => StringIO.new(data)}) }
 
-    obj = Metropolis.new(:uri => "#{uri}?#{query}", :readonly => true)
+    opts = @app_opts.merge(:uri => "#{uri}?#{query}", :readonly => true)
+    obj = Metropolis.new(opts)
     assert_equal data, obj.get(k, {})[2].join('')
     obj.close!
 
-    obj = Metropolis.new(:uri => uri, :readonly => true)
+    opts = @app_opts.merge(:uri => uri, :readonly => true)
+    obj = Metropolis.new(opts)
     assert_equal data, obj.get(k, {})[2].join('')
     obj.close!
     sum = obj.instance_eval {
@@ -198,13 +201,15 @@ class Test_TC_HDB < Test::Unit::TestCase
   end
 
   def test_exclusive
-    @app = Metropolis.new(:uri => uri, :exclusive => true)
+    opts = @app_opts.merge(:uri => uri, :exclusive => true)
+    @app = Metropolis.new(opts)
     assert_equal(app.method(:reader), app.method(:writer))
     basic_rest
   end
 
   def test_no_rdlock
-    @app = Metropolis.new(:uri => "#{uri}?rdlock=false")
+    opts = @app_opts.merge(:uri => "#{uri}?rdlock=false")
+    @app = Metropolis.new(opts)
     nolck = ::TokyoCabinet::HDB::ONOLCK
     flags = @app.instance_variable_get(:@rd_flags)
     assert((flags & nolck) == nolck)
@@ -214,7 +219,7 @@ class Test_TC_HDB < Test::Unit::TestCase
   end
 
   def test_no_wrlock
-    @app = Metropolis.new(:uri => "#{uri}?wrlock=false")
+    @app = Metropolis.new(@app_opts.merge(:uri => "#{uri}?wrlock=false"))
     nolck = ::TokyoCabinet::HDB::ONOLCK
     flags = @app.instance_variable_get(:@wr_flags)
     assert((flags & nolck) == nolck)
diff --git a/test/test_tdb.rb b/test/test_tdb.rb
index c5b3245..0fead86 100644
--- a/test/test_tdb.rb
+++ b/test/test_tdb.rb
@@ -11,7 +11,8 @@ class Test_TDB < Test::Unit::TestCase
     tmp = Tempfile.new('tdb')
     @path_pattern = tmp.path + ".%01x.tdb"
     tmp.close!
-    @uri = "tdb://#{@path_pattern}"
+    @uri = "tdb:///"
+    @app_opts = { :uri => @uri, :path_pattern => @path_pattern }
   end
 
   def teardown