Skip to content

Commit ff933c9

Browse files
committed
Implemented select to support reduce view
1 parent c0275bc commit ff933c9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lib/couchlib/src/couchlib.erl

+41
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,31 @@ select({#db{}=Db0, Design, View}, StartKey, EndKey, Options)
285285
{not_found, missing_named_view} -> {error, view_not_found}
286286
catch
287287
_:{not_found, missing} -> {error, design_not_found}
288+
end;
289+
290+
select({#db{}=Db0, Design, ReduceView, GroupLevel}, StartKey, EndKey, Options)
291+
when is_list(Design), is_list(ReduceView) ->
292+
Db = reopen(Db0),
293+
BaseArgs = view_query_base_args(Options),
294+
#view_query_args{stale=Stale,
295+
limit=Limit,
296+
skip=Skip} = BaseArgs,
297+
Args = BaseArgs#view_query_args{start_key=StartKey, end_key=EndKey},
298+
try couch_view:get_reduce_view(Db, design_id(Design),
299+
list_to_binary(ReduceView), Stale) of
300+
{ok, Reduce, Group} ->
301+
{ok, GroupRowsFun, RespFun} = fold_reduce_view_acc_fun(Db, Group, GroupLevel),
302+
case couch_view:fold_reduce(Reduce, RespFun,
303+
{Limit, Skip, undefined, []},
304+
[{key_group_fun, GroupRowsFun}|fold_view_options(Args)]) of
305+
{ok, {_,_,_,Rows}} ->
306+
{ok, Rows};
307+
Error ->
308+
{error, Error}
309+
end;
310+
{not_found, missing_named_view} -> {error, view_not_found}
311+
catch
312+
_:{not_found, missing} -> {error, design_not_found}
288313
end.
289314

290315
%% ---------------------------------------------------------------------------
@@ -446,6 +471,9 @@ enum_db_acc_fun(Db, DocCount, #view_query_args{}=Args) ->
446471
%% Preserves the initial offset and provides an empty list for acc.
447472
%% ---------------------------------------------------------------------------
448473

474+
doc_fold_reduce_start_response(_Req, _Etag, _Acc, _UpdateSeq) ->
475+
{ok, nil, []}.
476+
449477
doc_fold_start_response(_Req, _Etag, _RowCount, Offset, _Acc, _UpdateSeq) ->
450478
{ok, nil, {Offset, []}}.
451479

@@ -458,6 +486,9 @@ doc_fold_start_response(_Req, _Etag, _RowCount, Offset, _Acc, _UpdateSeq) ->
458486
%% Removes the outer tuple for "objects" to present them as tuple lists.
459487
%% ---------------------------------------------------------------------------
460488

489+
doc_fold_reduce_send_row(_Resp, {Key, Value}, Acc) ->
490+
{ok, [{Key, Value}|Acc]}.
491+
461492
doc_fold_send_row(_Resp, Db, Doc, IncludeDocs, {Offset, Acc}) ->
462493
{Row} = couch_httpd_view:view_row_obj(Db, Doc, IncludeDocs),
463494
{ok, {Offset, [lists:map(fun format_row_item/1, Row)|Acc]}}.
@@ -489,6 +520,16 @@ enum_db_options(#view_query_args{start_docid=StartId,
489520
%% It borrows from hovercraft.
490521
%% ---------------------------------------------------------------------------
491522

523+
fold_reduce_view_acc_fun(Db, Group, GroupLevel) ->
524+
UpdateSeq = couch_db:get_update_seq(Db),
525+
CurrentEtag = couch_httpd_view:view_group_etag(Group, Db),
526+
Helpers = #reduce_fold_helper_funs{
527+
%%reduce_count=fun couch_view:reduce_to_count/1,
528+
start_response=fun doc_fold_reduce_start_response/4,
529+
send_row=fun doc_fold_reduce_send_row/3},
530+
couch_httpd_view:make_reduce_fold_funs(
531+
nil, GroupLevel, undefined, CurrentEtag, UpdateSeq, Helpers).
532+
492533
fold_view_acc_fun(Db, RowCount, Args) ->
493534
UpdateSeq = couch_db:get_update_seq(Db),
494535
Helpers = #view_fold_helper_funs{

0 commit comments

Comments
 (0)