Skip to content

Commit

Permalink
Replaced with the original file from apache-couchdb-1.0.2 source tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
YasuhiroABE committed Jan 29, 2011
1 parent efa5508 commit bc6b0fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 82 deletions.
9 changes: 1 addition & 8 deletions couch_db.hrl
Expand Up @@ -190,7 +190,6 @@
skip = 0,

group_level = 0,
group_numrows = false,

view_type = nil,
include_docs = false,
Expand Down Expand Up @@ -265,13 +264,7 @@
body = nil,
options = [
{response_format,binary},
{inactivity_timeout, 30000},
{max_sessions, list_to_integer(
couch_config:get("replicator", "max_http_sessions", "10")
)},
{max_pipeline_size, list_to_integer(
couch_config:get("replicator", "max_http_pipeline_size", "10")
)}
{inactivity_timeout, 30000}
],
retries = 10,
pause = 500,
Expand Down
86 changes: 12 additions & 74 deletions couch_httpd_view.erl
Expand Up @@ -155,22 +155,15 @@ output_reduce_view(Req, Db, View, Group, QueryArgs, nil) ->
group_level = GroupLevel
} = QueryArgs,
CurrentEtag = view_group_etag(Group, Db),
Pid = case get_group_numrows_type(Req) of
true -> spawn_link(fun() -> group_numrows_server() end);
_ -> false
end,
couch_httpd:etag_respond(Req, CurrentEtag, fun() ->
{ok, GroupRowsFun, RespFun} = make_reduce_fold_funs(Req, GroupLevel,
QueryArgs, CurrentEtag, Group#group.current_seq,
#reduce_fold_helper_funs{}, Pid),
#reduce_fold_helper_funs{}),
FoldAccInit = {Limit, Skip, undefined, []},
{ok, {_, _, Resp, _}} = couch_view:fold_reduce(View,
RespFun, FoldAccInit, [{key_group_fun, GroupRowsFun} |
make_key_options(QueryArgs)]),
case get_group_numrows_type(Req) of
true -> finish_reduce_fold(Req, Resp, [], Pid);
_ -> finish_reduce_fold(Req, Resp)
end
finish_reduce_fold(Req, Resp)
end);

output_reduce_view(Req, Db, View, Group, QueryArgs, Keys) ->
Expand All @@ -180,14 +173,10 @@ output_reduce_view(Req, Db, View, Group, QueryArgs, Keys) ->
group_level = GroupLevel
} = QueryArgs,
CurrentEtag = view_group_etag(Group, Db, Keys),
Pid = case get_group_numrows_type(Req) of
true -> spawn_link(fun() -> group_numrows_server() end);
_ -> false
end,
couch_httpd:etag_respond(Req, CurrentEtag, fun() ->
{ok, GroupRowsFun, RespFun} = make_reduce_fold_funs(Req, GroupLevel,
QueryArgs, CurrentEtag, Group#group.current_seq,
#reduce_fold_helper_funs{}, Pid),
#reduce_fold_helper_funs{}),
{Resp, _RedAcc3} = lists:foldl(
fun(Key, {Resp, RedAcc}) ->
% run the reduce once for each key in keys, with limit etc
Expand All @@ -201,10 +190,7 @@ output_reduce_view(Req, Db, View, Group, QueryArgs, Keys) ->
{Resp2, RedAcc2}
end,
{undefined, []}, Keys), % Start with no comma
case get_group_numrows_type(Req) of
true -> finish_reduce_fold(Req, Resp, [{update_seq,Group#group.current_seq}], Pid);
_ -> finish_reduce_fold(Req, Resp, [{update_seq,Group#group.current_seq}])
end
finish_reduce_fold(Req, Resp, [{update_seq,Group#group.current_seq}])
end).

reverse_key_default(?MIN_STR) -> ?MAX_STR;
Expand All @@ -217,9 +203,6 @@ get_stale_type(Req) ->
get_reduce_type(Req) ->
list_to_existing_atom(couch_httpd:qs_value(Req, "reduce", "true")).

get_group_numrows_type(Req) ->
list_to_existing_atom(couch_httpd:qs_value(Req, "group_numrows", "false")).

load_view(Req, Db, {ViewDesignId, ViewName}, Keys) ->
Stale = get_stale_type(Req),
Reduce = get_reduce_type(Req),
Expand Down Expand Up @@ -320,8 +303,6 @@ parse_view_param("reduce", Value) ->
[{reduce, parse_bool_param(Value)}];
parse_view_param("include_docs", Value) ->
[{include_docs, parse_bool_param(Value)}];
parse_view_param("group_numrows", Value) ->
[{group_numrows, parse_bool_param(Value)}];
parse_view_param("list", Value) ->
[{list, ?l2b(Value)}];
parse_view_param("callback", _) ->
Expand Down Expand Up @@ -384,6 +365,8 @@ validate_view_query(group_level, Value, Args) ->
end;
validate_view_query(inclusive_end, Value, Args) ->
Args#view_query_args{inclusive_end=Value};
validate_view_query(reduce, false, Args) ->
Args;
validate_view_query(reduce, _, Args) ->
case Args#view_query_args.view_type of
map ->
Expand All @@ -404,8 +387,6 @@ validate_view_query(include_docs, true, Args) ->
% Use the view_query_args record's default value
validate_view_query(include_docs, _Value, Args) ->
Args;
validate_view_query(group_numrows, _Value, Args) ->
Args;
validate_view_query(extra, _Value, Args) ->
Args.

Expand All @@ -414,7 +395,7 @@ make_view_fold_fun(Req, QueryArgs, Etag, Db, UpdateSeq, TotalViewCount, HelperFu
start_response = StartRespFun,
send_row = SendRowFun,
reduce_count = ReduceCountFun
} = apply_default_helper_funs(HelperFuns, Req, Req),
} = apply_default_helper_funs(HelperFuns),

#view_query_args{
include_docs = IncludeDocs
Expand Down Expand Up @@ -446,13 +427,10 @@ make_view_fold_fun(Req, QueryArgs, Etag, Db, UpdateSeq, TotalViewCount, HelperFu
end.

make_reduce_fold_funs(Req, GroupLevel, _QueryArgs, Etag, UpdateSeq, HelperFuns) ->
make_reduce_fold_funs(Req, GroupLevel, _QueryArgs, Etag, UpdateSeq, HelperFuns, nil).

make_reduce_fold_funs(Req, GroupLevel, _QueryArgs, Etag, UpdateSeq, HelperFuns, Pid) ->
#reduce_fold_helper_funs{
start_response = StartRespFun,
send_row = SendRowFun
} = apply_default_helper_funs(HelperFuns, Req, Pid),
} = apply_default_helper_funs(HelperFuns),

GroupRowsFun =
fun({_Key1,_}, {_Key2,_}) when GroupLevel == 0 ->
Expand Down Expand Up @@ -512,7 +490,7 @@ apply_default_helper_funs(
#view_fold_helper_funs{
start_response = StartResp,
send_row = SendRow
}=Helpers, _Req, _Pid) ->
}=Helpers) ->
StartResp2 = case StartResp of
undefined -> fun json_view_start_resp/6;
_ -> StartResp
Expand All @@ -528,21 +506,19 @@ apply_default_helper_funs(
send_row = SendRow2
};


apply_default_helper_funs(
#reduce_fold_helper_funs{
start_response = StartResp,
send_row = SendRow
}=Helpers, Req, Pid) ->
}=Helpers) ->
StartResp2 = case StartResp of
undefined -> fun json_reduce_start_resp/4;
_ -> StartResp
end,

SendRow2 = case SendRow of
undefined -> case get_group_numrows_type(Req) of
true -> gen_send_json_reduce_row(Pid);
_ -> fun send_json_reduce_row/3
end;
undefined -> fun send_json_reduce_row/3;
_ -> SendRow
end,

Expand Down Expand Up @@ -612,24 +588,6 @@ send_json_reduce_row(Resp, {Key, Value}, RowFront) ->
send_chunk(Resp, RowFront ++ ?JSON_ENCODE({[{key, Key}, {value, Value}]})),
{ok, ",\r\n"}.

gen_send_json_reduce_row(Pid) ->
fun(_Req, _KV, _RowFront) ->
Pid ! {countup},
{ok, ",\r\n"}
end.

group_numrows_server() ->
group_numrows_server(0).
group_numrows_server(X) ->
receive
{status, From} ->
From ! X, group_numrows_server(X);
{countup} ->
group_numrows_server(X+1);
{stop, From} ->
From ! X, exit(normal)
end.

view_group_etag(Group, Db) ->
view_group_etag(Group, Db, nil).

Expand Down Expand Up @@ -695,26 +653,6 @@ finish_view_fold(Req, TotalRows, Offset, FoldResult, Fields) ->
finish_reduce_fold(Req, Resp) ->
finish_reduce_fold(Req, Resp, []).

get_group_numrows_final_result(Pid) ->
Pid ! {stop, self()},
receive
X -> X
after 80000 ->
0
end.

finish_reduce_fold(Req, Resp, Fields, Pid) ->
case Resp of
undefined ->
send_json(Req, 200, {[
{group_numrows, 0}
] ++ Fields});
Resp ->
X = get_group_numrows_final_result(Pid),
send_chunk(Resp, "{\"group_numrows\":\"" ++ integer_to_list(X) ++ "\"}"),
end_json_response(Resp)
end.

finish_reduce_fold(Req, Resp, Fields) ->
case Resp of
undefined ->
Expand Down

0 comments on commit bc6b0fc

Please sign in to comment.