summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Avdey <eiri@eiri.ca>2018-11-16 14:57:37 -0400
committerEric Avdey <eiri@eiri.ca>2018-11-21 17:22:08 -0400
commit99825820d12d992c30caad007de3942761bf457e (patch)
tree0074cf15dfa1ad0f0d823b8de01eb2ce3a890393
parenta2fd9eb8a0f7fba39248ab84764f5d735bcb83aa (diff)
downloadcouchdb-99825820d12d992c30caad007de3942761bf457e.tar.gz
Fix total_rows for _design_docs with keys
This adds a new fabric provider for getting a counter for design docs in a database. It allows for a valid query for total_rows when _design_docs handler queried with an array of keys.
-rw-r--r--src/fabric/src/fabric.erl24
-rw-r--r--src/fabric/src/fabric_design_doc_count.erl69
-rw-r--r--src/fabric/src/fabric_rpc.erl15
-rw-r--r--src/fabric/src/fabric_view_all_docs.erl36
4 files changed, 115 insertions, 29 deletions
diff --git a/src/fabric/src/fabric.erl b/src/fabric/src/fabric.erl
index 36ed67154..9bc99c265 100644
--- a/src/fabric/src/fabric.erl
+++ b/src/fabric/src/fabric.erl
@@ -18,9 +18,10 @@
% DBs
-export([all_dbs/0, all_dbs/1, create_db/1, create_db/2, delete_db/1,
- delete_db/2, get_db_info/1, get_doc_count/1, set_revs_limit/3,
- set_security/2, set_security/3, get_revs_limit/1, get_security/1,
- get_security/2, get_all_security/1, get_all_security/2,
+ delete_db/2, get_db_info/1, get_doc_count/1, get_doc_count/2,
+ set_revs_limit/3, set_security/2, set_security/3,
+ get_revs_limit/1, get_security/1, get_security/2,
+ get_all_security/1, get_all_security/2,
get_purge_infos_limit/1, set_purge_infos_limit/3,
compact/1, compact/2]).
@@ -86,12 +87,21 @@ get_db_info(DbName) ->
fabric_db_info:go(dbname(DbName)).
%% @doc the number of docs in a database
--spec get_doc_count(dbname()) ->
- {ok, non_neg_integer()} |
+%% @equiv get_doc_count(DbName, <<"_all_docs">>)
+get_doc_count(DbName) ->
+ get_doc_count(DbName, <<"_all_docs">>).
+
+%% @doc the number of design docs in a database
+-spec get_doc_count(dbname(), Namespace::binary()) ->
+ {ok, non_neg_integer() | null} |
{error, atom()} |
{error, atom(), any()}.
-get_doc_count(DbName) ->
- fabric_db_doc_count:go(dbname(DbName)).
+get_doc_count(DbName, <<"_all_docs">>) ->
+ fabric_db_doc_count:go(dbname(DbName));
+get_doc_count(DbName, <<"_design">>) ->
+ fabric_design_doc_count:go(dbname(DbName));
+get_doc_count(_DbName, <<"_local">>) ->
+ {ok, null}.
%% @equiv create_db(DbName, [])
create_db(DbName) ->
diff --git a/src/fabric/src/fabric_design_doc_count.erl b/src/fabric/src/fabric_design_doc_count.erl
new file mode 100644
index 000000000..22d03c5d4
--- /dev/null
+++ b/src/fabric/src/fabric_design_doc_count.erl
@@ -0,0 +1,69 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+% http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(fabric_design_doc_count).
+
+-export([go/1]).
+
+-include_lib("fabric/include/fabric.hrl").
+-include_lib("mem3/include/mem3.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+go(DbName) ->
+ Shards = mem3:shards(DbName),
+ Workers = fabric_util:submit_jobs(Shards, get_design_doc_count, []),
+ RexiMon = fabric_util:create_monitors(Shards),
+ Acc0 = {fabric_dict:init(Workers, nil), 0},
+ try fabric_util:recv(Workers, #shard.ref, fun handle_message/3, Acc0) of
+ {timeout, {WorkersDict, _}} ->
+ DefunctWorkers = fabric_util:remove_done_workers(WorkersDict, nil),
+ fabric_util:log_timeout(DefunctWorkers, "get_design_doc_count"),
+ {error, timeout};
+ Else ->
+ Else
+ after
+ rexi_monitor:stop(RexiMon)
+ end.
+
+handle_message({rexi_DOWN, _, {_,NodeRef},_}, _Shard, {Counters, Acc}) ->
+ case fabric_util:remove_down_workers(Counters, NodeRef) of
+ {ok, NewCounters} ->
+ {ok, {NewCounters, Acc}};
+ error ->
+ {error, {nodedown, <<"progress not possible">>}}
+ end;
+
+handle_message({rexi_EXIT, Reason}, Shard, {Counters, Acc}) ->
+ NewCounters = lists:keydelete(Shard, #shard.ref, Counters),
+ case fabric_view:is_progress_possible(NewCounters) of
+ true ->
+ {ok, {NewCounters, Acc}};
+ false ->
+ {error, Reason}
+ end;
+
+handle_message({ok, Count}, Shard, {Counters, Acc}) ->
+ case fabric_dict:lookup_element(Shard, Counters) of
+ undefined ->
+ {ok, {Counters, Acc}};
+ nil ->
+ C1 = fabric_dict:store(Shard, ok, Counters),
+ C2 = fabric_view:remove_overlapping_shards(Shard, C1),
+ case fabric_dict:any(nil, C2) of
+ true ->
+ {ok, {C2, Count+Acc}};
+ false ->
+ {stop, Count+Acc}
+ end
+ end;
+handle_message(_, _, Acc) ->
+ {ok, Acc}.
diff --git a/src/fabric/src/fabric_rpc.erl b/src/fabric/src/fabric_rpc.erl
index 11e675464..c8aa19e0f 100644
--- a/src/fabric/src/fabric_rpc.erl
+++ b/src/fabric/src/fabric_rpc.erl
@@ -12,7 +12,8 @@
-module(fabric_rpc).
--export([get_db_info/1, get_doc_count/1, get_update_seq/1]).
+-export([get_db_info/1, get_doc_count/1, get_design_doc_count/1,
+ get_update_seq/1]).
-export([open_doc/3, open_revs/4, get_doc_info/3, get_full_doc_info/3,
get_missing_revs/2, get_missing_revs/3, update_docs/3]).
-export([all_docs/3, changes/3, map_view/4, reduce_view/4, group_info/2]).
@@ -23,8 +24,9 @@
-export([compact/1, compact/2]).
-export([get_purge_seq/2, purge_docs/3, set_purge_infos_limit/3]).
--export([get_db_info/2, get_doc_count/2, get_update_seq/2,
- changes/4, map_view/5, reduce_view/5, group_info/3, update_mrview/4]).
+-export([get_db_info/2, get_doc_count/2, get_design_doc_count/2,
+ get_update_seq/2, changes/4, map_view/5, reduce_view/5,
+ group_info/3, update_mrview/4]).
-include_lib("fabric/include/fabric.hrl").
-include_lib("couch/include/couch_db.hrl").
@@ -181,6 +183,13 @@ get_doc_count(DbName) ->
get_doc_count(DbName, DbOptions) ->
with_db(DbName, DbOptions, {couch_db, get_doc_count, []}).
+%% equiv get_design_doc_count(DbName, [])
+get_design_doc_count(DbName) ->
+ get_design_doc_count(DbName, []).
+
+get_design_doc_count(DbName, DbOptions) ->
+ with_db(DbName, DbOptions, {couch_db, get_design_doc_count, []}).
+
%% equiv get_update_seq(DbName, [])
get_update_seq(DbName) ->
get_update_seq(DbName, []).
diff --git a/src/fabric/src/fabric_view_all_docs.erl b/src/fabric/src/fabric_view_all_docs.erl
index ac16dac52..30c8e8d51 100644
--- a/src/fabric/src/fabric_view_all_docs.erl
+++ b/src/fabric/src/fabric_view_all_docs.erl
@@ -82,24 +82,22 @@ go(DbName, Options, QueryArgs, Callback, Acc0) ->
true -> lists:sublist(Keys2, Limit);
false -> Keys2
end,
- Resp = case couch_util:get_value(namespace, Extra, <<"_all_docs">>) of
- <<"_local">> ->
- {ok, null};
- _ ->
- Timeout = fabric_util:all_docs_timeout(),
- {_, Ref0} = spawn_monitor(fun() ->
- exit(fabric:get_doc_count(DbName))
- end),
- receive {'DOWN', Ref0, _, _, Result} ->
- Result
- after Timeout ->
- timeout
- end
+ %% namespace can be _set_ to `undefined`, so we want simulate enum here
+ Namespace = case couch_util:get_value(namespace, Extra) of
+ <<"_all_docs">> -> <<"_all_docs">>;
+ <<"_design">> -> <<"_design">>;
+ <<"_local">> -> <<"_local">>;
+ _ -> <<"_all_docs">>
end,
- case Resp of
- {ok, TotalRows} ->
+ Timeout = fabric_util:all_docs_timeout(),
+ {_, Ref} = spawn_monitor(fun() ->
+ exit(fabric:get_doc_count(DbName, Namespace))
+ end),
+ receive
+ {'DOWN', Ref, _, _, {ok, TotalRows}} ->
Meta = case UpdateSeq of
- false -> [{total, TotalRows}, {offset, null}];
+ false ->
+ [{total, TotalRows}, {offset, null}];
true ->
[{total, TotalRows}, {offset, null}, {update_seq, null}]
end,
@@ -108,10 +106,10 @@ go(DbName, Options, QueryArgs, Callback, Acc0) ->
Keys3, queue:new(), SpawnFun, MaxJobs, Callback, Acc1
),
Callback(complete, Acc2);
- timeout ->
- Callback(timeout, Acc0);
- Error ->
+ {'DOWN', Ref, _, _, Error} ->
Callback({error, Error}, Acc0)
+ after Timeout ->
+ Callback(timeout, Acc0)
end.
go(DbName, _Options, Workers, QueryArgs, Callback, Acc0) ->