You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2018/08/27 15:42:16 UTC

[couchdb] 01/01: Improve validation of database creation parameters

This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch improve-dbcreate-validation
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit bc0379543afa0fd6378d9a2ef8f055b3ba38cf61
Author: Robert Newson <rn...@apache.org>
AuthorDate: Mon Aug 27 16:11:43 2018 +0100

    Improve validation of database creation parameters
---
 src/mem3/src/mem3.erl      |  2 +-
 src/mem3/src/mem3_util.erl | 15 +++++++++++----
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/src/mem3/src/mem3.erl b/src/mem3/src/mem3.erl
index 0e5eabf..7033faf 100644
--- a/src/mem3/src/mem3.erl
+++ b/src/mem3/src/mem3.erl
@@ -190,7 +190,7 @@ choose_shards(DbName, Nodes, Options) ->
     if N =:= 0 -> erlang:error(no_nodes_in_zone);
        true -> ok
     end,
-    Q = mem3_util:to_integer(couch_util:get_value(q, Options,
+    Q = mem3_util:q_val(couch_util:get_value(q, Options,
         config:get("cluster", "q", "8"))),
     %% rotate to a random entry in the nodelist for even distribution
     {A, B} = lists:split(crypto:rand_uniform(1,length(Nodes)+1), Nodes),
diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl
index 0b69d79..254a6df 100644
--- a/src/mem3/src/mem3_util.erl
+++ b/src/mem3/src/mem3_util.erl
@@ -13,7 +13,7 @@
 -module(mem3_util).
 
 -export([hash/1, name_shard/2, create_partition_map/5, build_shards/2,
-    n_val/2, to_atom/1, to_integer/1, write_db_doc/1, delete_db_doc/1,
+    n_val/2, q_val/1, to_atom/1, to_integer/1, write_db_doc/1, delete_db_doc/1,
     shard_info/1, ensure_exists/1, open_db_doc/1]).
 -export([is_deleted/1, rotate_list/2]).
 
@@ -52,15 +52,15 @@ make_name(DbName, [B,E], Suffix) ->
 create_partition_map(DbName, N, Q, Nodes) ->
     create_partition_map(DbName, N, Q, Nodes, "").
 
-create_partition_map(DbName, N, Q, Nodes, Suffix) ->
+create_partition_map(DbName, N, Q, Nodes, Suffix) when Q > 0 ->
     UniqueShards = make_key_ranges((?RINGTOP) div Q, 0, []),
     Shards0 = lists:flatten([lists:duplicate(N, S) || S <- UniqueShards]),
     Shards1 = attach_nodes(Shards0, [], Nodes, []),
     [name_shard(S#shard{dbname=DbName}, Suffix) || S <- Shards1].
 
-make_key_ranges(_, CurrentPos, Acc) when CurrentPos >= ?RINGTOP ->
+make_key_ranges(I, CurrentPos, Acc) when I > 0, CurrentPos >= ?RINGTOP ->
     Acc;
-make_key_ranges(Increment, Start, Acc) ->
+make_key_ranges(Increment, Start, Acc) when Increment > 0 ->
     case Start + 2*Increment of
     X when X > ?RINGTOP ->
         End = ?RINGTOP - 1;
@@ -217,6 +217,13 @@ n_val(N, _) when N < 1 ->
 n_val(N, _) ->
     N.
 
+q_val(Q) when is_list(Q) ->
+    q_val(list_to_integer(Q));
+q_val(Q) when Q > 0 ->
+    Q;
+q_val(_) ->
+    throw({error, invalid_q_value}).
+
 shard_info(DbName) ->
     [{n, mem3:n(DbName)},
      {q, length(mem3:shards(DbName)) div mem3:n(DbName)}].