You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ch...@apache.org on 2020/03/20 23:28:34 UTC

[couchdb] 02/03: Ensure shards are created with db options

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

chewbranca pushed a commit to branch fix-create-db-options
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 2ea23a6865ed6a6f1cd38ed502eff705cc0454f1
Author: Russell Branca <ch...@apache.org>
AuthorDate: Fri Mar 20 14:34:39 2020 -0700

    Ensure shards are created with db options
---
 src/couch/include/couch_eunit.hrl |  5 ++++
 src/fabric/src/fabric_rpc.erl     |  2 +-
 src/mem3/src/mem3_rpc.erl         |  2 +-
 src/mem3/src/mem3_shards.erl      | 10 +++++++
 src/mem3/src/mem3_util.erl        | 56 ++++++++++++++++++++++++++++++++++++++-
 5 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/src/couch/include/couch_eunit.hrl b/src/couch/include/couch_eunit.hrl
index d3611c8..1885248 100644
--- a/src/couch/include/couch_eunit.hrl
+++ b/src/couch/include/couch_eunit.hrl
@@ -49,6 +49,11 @@
         Suffix = couch_uuids:random(),
         iolist_to_binary(["eunit-test-db-", Suffix])
     end).
+-define(tempshard,
+    fun() ->
+        Suffix = couch_uuids:random(),
+        iolist_to_binary(["shards/80000000-ffffffff/eunit-test-db-", Suffix])
+    end).
 -define(docid,
     fun() ->
         integer_to_list(couch_util:unique_monotonic_integer())
diff --git a/src/fabric/src/fabric_rpc.erl b/src/fabric/src/fabric_rpc.erl
index a67dcd1..85da3ff 100644
--- a/src/fabric/src/fabric_rpc.erl
+++ b/src/fabric/src/fabric_rpc.erl
@@ -439,7 +439,7 @@ get_node_seqs(Db, Nodes) ->
 
 
 get_or_create_db(DbName, Options) ->
-    couch_db:open_int(DbName, [{create_if_missing, true} | Options]).
+    mem3_util:get_or_create_db(DbName, Options).
 
 
 get_view_cb(#mrargs{extra = Options}) ->
diff --git a/src/mem3/src/mem3_rpc.erl b/src/mem3/src/mem3_rpc.erl
index 0991aa7..5d1c62c 100644
--- a/src/mem3/src/mem3_rpc.erl
+++ b/src/mem3/src/mem3_rpc.erl
@@ -401,7 +401,7 @@ rexi_call(Node, MFA, Timeout) ->
 
 
 get_or_create_db(DbName, Options) ->
-    couch_db:open_int(DbName, [{create_if_missing, true} | Options]).
+    mem3_util:get_or_create_db(DbName, Options).
 
 
 -ifdef(TEST).
diff --git a/src/mem3/src/mem3_shards.erl b/src/mem3/src/mem3_shards.erl
index bfee302..4f33237 100644
--- a/src/mem3/src/mem3_shards.erl
+++ b/src/mem3/src/mem3_shards.erl
@@ -20,6 +20,7 @@
 -export([handle_config_change/5, handle_config_terminate/3]).
 
 -export([start_link/0]).
+-export([opts_for_db/1]).
 -export([for_db/1, for_db/2, for_docid/2, for_docid/3, get/3, local/1, fold/2]).
 -export([for_shard_range/1]).
 -export([set_max_size/1]).
@@ -45,6 +46,15 @@
 start_link() ->
     gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
 
+opts_for_db(DbName) ->
+    {ok, Db} = mem3_util:ensure_exists(mem3_sync:shards_db()),
+    case couch_db:open_doc(Db, DbName, [ejson_body]) of
+        {ok, #doc{body = {Props}}} ->
+            mem3_util:get_shard_opts(Props);
+        {not_found, _} ->
+            erlang:error(database_does_not_exist, ?b2l(DbName))
+    end.
+
 for_db(DbName) ->
     for_db(DbName, []).
 
diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl
index 619f781..2d1a3de 100644
--- a/src/mem3/src/mem3_util.erl
+++ b/src/mem3/src/mem3_util.erl
@@ -14,8 +14,9 @@
 
 -export([name_shard/2, create_partition_map/5, build_shards/2,
     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]).
+    shard_info/1, ensure_exists/1, open_db_doc/1, get_or_create_db/2]).
 -export([is_deleted/1, rotate_list/2]).
+-export([get_shard_opts/1, get_engine_opt/1, get_props_opt/1]).
 -export([
     iso8601_timestamp/0,
     live_nodes/0,
@@ -506,6 +507,59 @@ sort_ranges_fun({B1, _}, {B2, _}) ->
     B1 =< B2.
 
 
+get_or_create_db(DbName, Options) ->
+    case couch_db:open_int(DbName, Options) of
+        {ok, _} = OkDb ->
+            OkDb;
+        {not_found, no_db_file} ->
+            try
+                DbOpts = case mem3:dbname(DbName) of
+                    DbName  -> [];
+                    MDbName -> mem3_shards:opts_for_db(MDbName)
+                end,
+                %% Options1 = opts_merge(DbOpts, Options)
+                Options1 = DbOpts ++ [{create_if_missing, true} | Options],
+                couch_db:open_int(DbName, Options1)
+            catch error:database_does_not_exist ->
+                {error, missing_target}
+            end;
+        Else ->
+            Else
+    end.
+
+
+opts_merge(L1, L2) ->
+    lists:reverse(opts_merge(lists:sort(L1), lists:sort(L2, []))).
+
+opts_merge([], [], Acc) ->
+    Acc;
+opts_merge(L1, [], Acc) ->
+    L1 ++ Acc;
+opts_merge([], R1, Acc) ->
+    R1 ++ Acc;
+opts_merge([A1|R1], [A2|R2], Acc) when is_atom(A1) orelse is_atom(A2) ->
+    case {A1, A2} of
+        {{K1,_V1}, A2} when K1 < A2 ->
+            opts_merge(R1, [A2|R2], [A1 | Acc]);
+        {{K1,_V1}, A2} when K1 >= A2 ->
+            opts_merge([A1|R1], R2, [A2|Acc]);
+        {A1, {K2,_V2}} when A1 < K2 ->
+            opts_merge(R1, [A2|R2], [A1|Acc]);
+        {A1, {K2,_V2}} when A1 >= K2 ->
+            opts_merge([A1|R1], R2, [A2|Acc]);
+        {A1, A2} when A1 < A2 ->
+            opts_merge(R1, [A2|R2], [A1|Acc]);
+        {A1, A2} ->
+            opts_merge([A1|R1], R2, [A2|Acc])
+    end;
+opts_merge([{K,V1}|R1], [{K,_V2}|R2], Acc) ->
+    opts_merge(R1, R2, [{K, V1} | Acc]);
+opts_merge([{K1,V1}|R1], [{K2,_V2}|_]=R2, Acc) when K1 < K2 ->
+    opts_merge(R1, R2, [{K1, V1} | Acc]);
+opts_merge([{K1,_V1}|_]=R1, [{K2,V2}|R2], Acc) when K1 >= K2 ->
+    opts_merge(R1, R2, [{K2, V2} | Acc]).
+
+
 -ifdef(TEST).
 
 -include_lib("eunit/include/eunit.hrl").