summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriilyak <iilyak@ca.ibm.com>2017-04-24 13:36:34 -0700
committerGitHub <noreply@github.com>2017-04-24 13:36:34 -0700
commit0d00c102a6f985836f2a6eca656fb018fa9d9227 (patch)
tree310c8022d11762e5b974c2ae995a9ee8d67fd181
parent4aba3bc5c0b48e7902ebd1ae3c5f379e0ea9ac66 (diff)
parent64573fc2a3ddeded169a74039fee3b2bd168f694 (diff)
downloadcouchdb-0d00c102a6f985836f2a6eca656fb018fa9d9227.tar.gz
Merge pull request #488 from cloudant/3337-fix-_local_docs
Fix _local_docs endpoint
-rw-r--r--src/couch_mrview/src/couch_mrview.erl26
-rw-r--r--src/couch_mrview/src/couch_mrview_test_util.erl16
-rw-r--r--src/couch_mrview/src/couch_mrview_util.erl13
-rw-r--r--src/couch_mrview/test/couch_mrview_local_docs_tests.erl132
4 files changed, 177 insertions, 10 deletions
diff --git a/src/couch_mrview/src/couch_mrview.erl b/src/couch_mrview/src/couch_mrview.erl
index 088327c45..037391965 100644
--- a/src/couch_mrview/src/couch_mrview.erl
+++ b/src/couch_mrview/src/couch_mrview.erl
@@ -403,8 +403,7 @@ cleanup(Db) ->
all_docs_fold(Db, #mrargs{keys=undefined}=Args, Callback, UAcc) ->
- {ok, Info} = couch_db:get_db_info(Db),
- Total = couch_util:get_value(doc_count, Info),
+ Total = get_total_rows(Db, Args),
UpdateSeq = couch_db:get_update_seq(Db),
Acc = #mracc{
db=Db,
@@ -421,8 +420,7 @@ all_docs_fold(Db, #mrargs{keys=undefined}=Args, Callback, UAcc) ->
{ok, Offset, FinalAcc} = couch_db:enum_docs(Db, fun map_fold/3, Acc, Opts),
finish_fold(FinalAcc, [{total, Total}, {offset, Offset}]);
all_docs_fold(Db, #mrargs{direction=Dir, keys=Keys0}=Args, Callback, UAcc) ->
- {ok, Info} = couch_db:get_db_info(Db),
- Total = couch_util:get_value(doc_count, Info),
+ Total = get_total_rows(Db, Args),
UpdateSeq = couch_db:get_update_seq(Db),
Acc = #mracc{
db=Db,
@@ -533,15 +531,17 @@ map_fold({{Key, Id}, Val}, _Offset, Acc) ->
user_acc=UAcc1,
last_go=Go
}};
-map_fold({<<"_local/",_/binary>> = DocId, {Rev0, _Body}}, _Offset, #mracc{} = Acc) ->
+map_fold({<<"_local/",_/binary>> = DocId, {Rev0, Body}}, _Offset, #mracc{} = Acc) ->
#mracc{
limit=Limit,
callback=Callback,
- user_acc=UAcc0
+ user_acc=UAcc0,
+ args=Args
} = Acc,
Rev = {0, list_to_binary(integer_to_list(Rev0))},
Value = {[{rev, couch_doc:rev_to_str(Rev)}]},
- Row = [{id, DocId}, {key, DocId}, {value, Value}],
+ Doc = if Args#mrargs.include_docs -> [{doc, Body}]; true -> [] end,
+ Row = [{id, DocId}, {key, DocId}, {value, Value}] ++ Doc,
{Go, UAcc1} = Callback({row, Row}, UAcc0),
{Go, Acc#mracc{
limit=Limit-1,
@@ -653,6 +653,18 @@ make_meta(Args, UpdateSeq, Base) ->
end.
+get_total_rows(#db{local_tree = LocalTree} = Db, #mrargs{extra = Extra}) ->
+ case couch_util:get_value(namespace, Extra) of
+ <<"_local">> ->
+ FoldFun = fun(_, _, Acc) -> {ok, Acc + 1} end,
+ {ok, _, Total} = couch_btree:foldl(LocalTree, FoldFun, 0),
+ Total;
+ _ ->
+ {ok, Info} = couch_db:get_db_info(Db),
+ couch_util:get_value(doc_count, Info)
+ end.
+
+
default_cb(complete, Acc) ->
{ok, lists:reverse(Acc)};
default_cb({final, Info}, []) ->
diff --git a/src/couch_mrview/src/couch_mrview_test_util.erl b/src/couch_mrview/src/couch_mrview_test_util.erl
index 8cebd6dfa..2e0cb794e 100644
--- a/src/couch_mrview/src/couch_mrview_test_util.erl
+++ b/src/couch_mrview/src/couch_mrview_test_util.erl
@@ -24,10 +24,13 @@ init_db(Name, Type) ->
init_db(Name, Type, Count) ->
{ok, Db} = new_db(Name, Type),
- Docs = make_docs(Count),
+ Docs = make_docs(Type, Count),
save_docs(Db, Docs).
+new_db(Name, local) ->
+ couch_server:delete(Name, [?ADMIN_CTX]),
+ couch_db:create(Name, [?ADMIN_CTX]);
new_db(Name, Type) ->
couch_server:delete(Name, [?ADMIN_CTX]),
{ok, Db} = couch_db:create(Name, [?ADMIN_CTX]),
@@ -41,7 +44,9 @@ save_docs(Db, Docs) ->
couch_db:reopen(Db).
-make_docs(Count) ->
+make_docs(local, Count) ->
+ [local_doc(I) || I <- lists:seq(1, Count)];
+make_docs(_, Count) ->
[doc(I) || I <- lists:seq(1, Count)].
ddoc(changes) ->
@@ -117,3 +122,10 @@ doc(Id) ->
{<<"_id">>, list_to_binary(integer_to_list(Id))},
{<<"val">>, Id}
]}).
+
+
+local_doc(Id) ->
+ couch_doc:from_json_obj({[
+ {<<"_id">>, list_to_binary(io_lib:format("_local/~b", [Id]))},
+ {<<"val">>, Id}
+ ]}).
diff --git a/src/couch_mrview/src/couch_mrview_util.erl b/src/couch_mrview/src/couch_mrview_util.erl
index 3830b96c8..27f8737d4 100644
--- a/src/couch_mrview/src/couch_mrview_util.erl
+++ b/src/couch_mrview/src/couch_mrview_util.erl
@@ -319,7 +319,8 @@ get_row_count(#mrview{btree=Bt}) ->
{ok, Count}.
-all_docs_reduce_to_count(Reductions) ->
+all_docs_reduce_to_count(Reductions0) ->
+ Reductions = maybe_convert_reductions(Reductions0),
Reduce = fun couch_db_updater:btree_by_id_reduce/2,
{Count, _, _} = couch_btree:final_reduce(Reduce, Reductions),
Count.
@@ -833,6 +834,16 @@ maybe_load_doc(Db, Id, Val, #mrargs{doc_options=Opts}) ->
doc_row(couch_index_util:load_doc(Db, docid_rev(Id, Val), []), Opts).
+maybe_convert_reductions({KVs0, UserReductions}) ->
+ KVs = lists:map(fun maybe_convert_kv/1, KVs0),
+ {KVs, UserReductions}.
+
+maybe_convert_kv({<<"_local/", _/binary>> = DocId, _}) ->
+ #full_doc_info{id = DocId};
+maybe_convert_kv(DocInfo) ->
+ DocInfo.
+
+
doc_row(null, _Opts) ->
[{doc, null}];
doc_row(Doc, Opts) ->
diff --git a/src/couch_mrview/test/couch_mrview_local_docs_tests.erl b/src/couch_mrview/test/couch_mrview_local_docs_tests.erl
new file mode 100644
index 000000000..c16f53c62
--- /dev/null
+++ b/src/couch_mrview/test/couch_mrview_local_docs_tests.erl
@@ -0,0 +1,132 @@
+% 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(couch_mrview_local_docs_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch/include/couch_db.hrl").
+
+-define(TIMEOUT, 1000).
+
+
+
+setup() ->
+ {ok, Db} = couch_mrview_test_util:init_db(?tempdb(), local),
+ Db.
+
+teardown(Db) ->
+ couch_db:close(Db),
+ couch_server:delete(Db#db.name, [?ADMIN_CTX]),
+ ok.
+
+
+all_docs_test_() ->
+ {
+ "_local_docs view tests",
+ {
+ setup,
+ fun test_util:start_couch/0, fun test_util:stop_couch/1,
+ {
+ foreach,
+ fun setup/0, fun teardown/1,
+ [
+ fun should_query/1,
+ fun should_query_with_range/1,
+ fun should_query_with_range_rev/1,
+ fun should_query_with_limit_and_skip/1,
+ fun should_query_with_include_docs/1
+ ]
+ }
+ }
+ }.
+
+
+should_query(Db) ->
+ Result = run_query(Db, []),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 0}]},
+ mk_row(1),
+ mk_row(10),
+ mk_row(2),
+ mk_row(3),
+ mk_row(4),
+ mk_row(5),
+ mk_row(6),
+ mk_row(7),
+ mk_row(8),
+ mk_row(9)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_range(Db) ->
+ Result = run_query(Db, [
+ {start_key, <<"_local/3">>},
+ {end_key, <<"_local/5">>}
+ ]),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 3}]},
+ mk_row(3),
+ mk_row(4),
+ mk_row(5)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_range_rev(Db) ->
+ Result = run_query(Db, [
+ {direction, rev},
+ {start_key, <<"_local/5">>}, {end_key, <<"_local/3">>},
+ {inclusive_end, true}
+ ]),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 4}]},
+ mk_row(5),
+ mk_row(4),
+ mk_row(3)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_limit_and_skip(Db) ->
+ Result = run_query(Db, [
+ {start_key, <<"_local/2">>},
+ {limit, 3},
+ {skip, 3}
+ ]),
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 5}]},
+ mk_row(5),
+ mk_row(6),
+ mk_row(7)
+ ]},
+ ?_assertEqual(Expect, Result).
+
+should_query_with_include_docs(Db) ->
+ Result = run_query(Db, [
+ {start_key, <<"_local/8">>},
+ {end_key, <<"_local/8">>},
+ {include_docs, true}
+ ]),
+ {row, Doc0} = mk_row(8),
+ Doc = Doc0 ++ [{doc, {[{<<"val">>, 8}]}}],
+ Expect = {ok, [
+ {meta, [{total, 10}, {offset, 8}]},
+ {row, Doc}
+ ]},
+ ?_assertEqual(Expect, Result).
+
+
+mk_row(IntId) ->
+ Id = list_to_binary(io_lib:format("_local/~b", [IntId])),
+ {row, [{id, Id}, {key, Id}, {value, {[{rev, <<"0-1">>}]}}]}.
+
+run_query(Db, Opts0) ->
+ Opts = [{extra, [{namespace, <<"_local">>}]} | Opts0],
+ couch_mrview:query_all_docs(Db, Opts).