couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From iil...@apache.org
Subject [1/2] couch commit: updated refs/heads/master to b49d069
Date Tue, 19 Apr 2016 21:51:10 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/master 56e5cbaf1 -> b49d06916


Change normalize_dbname semantic add dbname_suffix

normalize_dbname used to return last componenent of the shard path. But
it was doing so only if argument was passed as a binary. We change the
semantic of normalize_dbname so it does following:

  1. expects either type binary or list
  2. expect any kind of database name
     - plain database name
     - plain database name with '.couch' extension
     - shard name with suffix (creation time)
     - shard name with suffix (creation time) and .couch extension
  3. always returns a binary containing all components of a database name
  4. removes information about shard
  5. removes shard's suffix

For cases we need last component of the path (old behaviour) we
intoroduce dbname_suffix/1

COUCHDB-2983


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/916b0cef
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/916b0cef
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/916b0cef

Branch: refs/heads/master
Commit: 916b0cef98a406609843621033128ed0d2e15893
Parents: 56e5cba
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Fri Apr 8 10:47:24 2016 -0700
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Tue Apr 19 14:48:07 2016 -0700

----------------------------------------------------------------------
 src/couch_db.erl | 124 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 117 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/916b0cef/src/couch_db.erl
----------------------------------------------------------------------
diff --git a/src/couch_db.erl b/src/couch_db.erl
index 1a9f669..8260a5c 100644
--- a/src/couch_db.erl
+++ b/src/couch_db.erl
@@ -35,6 +35,7 @@
 -export([monitored_by/1]).
 -export([normalize_dbname/1]).
 -export([validate_dbname/1]).
+-export([dbname_suffix/1]).
 
 -include_lib("couch/include/couch_db.hrl").
 
@@ -1513,10 +1514,23 @@ select_gt(V1, _V2) -> V1.
 select_lt(V1, V2) when V1 > V2 -> V2;
 select_lt(V1, _V2) -> V1.
 
-normalize_dbname(<<"shards/", _/binary>> = Path) ->
-    lists:last(binary:split(mem3:dbname(Path), <<"/">>, [global]));
-normalize_dbname(DbName) ->
-    DbName.
+-spec normalize_dbname(list() | binary()) -> binary().
+
+normalize_dbname(DbName) when is_list(DbName) ->
+    normalize_dbname(list_to_binary(DbName));
+normalize_dbname(DbName) when is_binary(DbName) ->
+    case filename:extension(DbName) of
+        <<".couch">> ->
+            mem3:dbname(filename:rootname(DbName));
+        _ ->
+            mem3:dbname(DbName)
+    end.
+
+-spec dbname_suffix(list() | binary()) -> binary().
+
+dbname_suffix(DbName) ->
+    filename:basename(normalize_dbname(DbName)).
+
 
 validate_dbname(DbName) when is_list(DbName) ->
     validate_dbname(?l2b(DbName));
@@ -1542,7 +1556,103 @@ validate_dbname_int(DbName, Normalized) when is_binary(DbName) ->
 
 is_systemdb(DbName) when is_list(DbName) ->
     is_systemdb(?l2b(DbName));
-is_systemdb(<<"shards/", _/binary>> = Path) when is_binary(Path) ->
-    is_systemdb(normalize_dbname(Path));
 is_systemdb(DbName) when is_binary(DbName) ->
-    lists:member(DbName, ?SYSTEM_DATABASES).
+    lists:member(dbname_suffix(DbName), ?SYSTEM_DATABASES).
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+setup() ->
+    ok = meck:new(couch_db_plugin, [passthrough]),
+    ok = meck:expect(couch_db_plugin, validate_dbname, fun(_, _) -> false end),
+    ok.
+
+teardown(_) ->
+    (catch meck:unload(couch_db_plugin)).
+
+validate_dbname_success_test_() ->
+    Cases =
+        generate_cases_with_shards("long/co$mplex-/path+/_something")
+        ++ generate_cases_with_shards("something")
+        ++ lists:append(
+            [generate_cases_with_shards(?b2l(SystemDb))
+                || SystemDb <- ?SYSTEM_DATABASES]),
+    {
+        foreach, fun setup/0, fun teardown/1,
+        [{test_name(A), fun() -> should_pass_validate_dbname(A) end} || {_, A} <- Cases]
+    }.
+
+validate_dbname_fail_test_() ->
+    Cases = generate_cases("_long/co$mplex-/path+/_something")
+       ++ generate_cases("_something")
+       ++ generate_cases_with_shards("long/co$mplex-/path+/_something#")
+       ++ generate_cases_with_shards("long/co$mplex-/path+/some.thing"),
+    {
+        foreach, fun setup/0, fun teardown/1,
+        [{test_name(A), fun() -> should_fail_validate_dbname(A) end} || {_, A} <- Cases]
+    }.
+
+normalize_dbname_test_() ->
+    Cases = generate_cases_with_shards("long/co$mplex-/path+/_something")
+       ++ generate_cases_with_shards("_something"),
+    WithExpected = [{?l2b(filename:rootname(A)), B} || {A, B} <- Cases],
+    [{test_name({Expected, Db}), ?_assertEqual(Expected, normalize_dbname(Db))}
+        || {Expected, Db} <- WithExpected].
+
+dbname_suffix_test_() ->
+    Cases = generate_cases_with_shards("long/co$mplex-/path+/_something")
+       ++ generate_cases_with_shards("_something"),
+    WithExpected = [{?l2b(filename:basename(Arg)), Db} || {Arg, Db} <- Cases],
+    [{test_name({Expected, Db}), ?_assertEqual(Expected, dbname_suffix(Db))}
+        || {Expected, Db} <- WithExpected].
+
+is_systemdb_test_() ->
+    Cases = lists:append([
+        generate_cases_with_shards("long/co$mplex-/path+/" ++ ?b2l(Db))
+            || Db <- ?SYSTEM_DATABASES]
+        ++ [generate_cases_with_shards(?b2l(Db)) || Db <- ?SYSTEM_DATABASES
+    ]),
+    WithExpected = [{?l2b(filename:basename(filename:rootname(Arg))), Db}
+        || {Arg, Db} <- Cases],
+    [{test_name({Expected, Db}) ++ " in ?SYSTEM_DATABASES",
+        ?_assert(is_systemdb(Db))} || {Expected, Db} <- WithExpected].
+
+should_pass_validate_dbname(DbName) ->
+    {test_name(DbName), ?_assertEqual(ok, validate_dbname(DbName))}.
+
+should_fail_validate_dbname(DbName) ->
+    {test_name(DbName), ?_test(begin
+        Result = validate_dbname(DbName),
+        ?assertMatch({error, {illegal_database_name, _}}, Result),
+        {error, {illegal_database_name, FailedDbName}} = Result,
+        ?assertEqual(to_binary(DbName), FailedDbName),
+        ok
+    end)}.
+
+to_binary(DbName) when is_list(DbName) ->
+    ?l2b(DbName);
+to_binary(DbName) when is_binary(DbName) ->
+    DbName.
+
+test_name({Expected, DbName}) ->
+    lists:flatten(io_lib:format("~p -> ~p", [DbName, Expected]));
+test_name(DbName) ->
+    lists:flatten(io_lib:format("~p", [DbName])).
+
+generate_cases_with_shards(DbName) ->
+    DbNameWithShard = add_shard(DbName),
+    DbNameWithShardAndExtension = add_shard(DbName) ++ ".couch",
+    Cases = [
+        DbName, ?l2b(DbName),
+        DbNameWithShard, ?l2b(DbNameWithShard),
+        DbNameWithShardAndExtension, ?l2b(DbNameWithShardAndExtension)
+    ],
+    [{DbName, Case} || Case <- Cases].
+
+add_shard(DbName) ->
+    "shards/00000000-3fffffff/" ++ DbName ++ ".1415960794".
+
+generate_cases(DbName) ->
+    [{DbName, DbName}, {DbName, ?l2b(DbName)}].
+
+-endif.


Mime
View raw message