summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_httpd_view.erl
blob: d281b82b4b7d4bc3ce52e723b500c70ed9e9a854 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
% 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_httpd_view).
-include("couch_db.hrl").

-export([handle_view_req/2,handle_temp_view_req/2]).

-export([parse_view_query/1,parse_view_query/2,make_view_fold_fun/5,finish_view_fold/3]).

-import(couch_httpd,
    [send_json/2,send_json/3,send_json/4,send_method_not_allowed/2,
    start_json_response/2,send_chunk/2,end_json_response/1]).

design_doc_view(Req, Db, Id, ViewName, Keys) ->
    case couch_view:get_map_view({couch_db:name(Db), 
            <<"_design/", Id/binary>>, ViewName}) of
    {ok, View} ->    
        QueryArgs = parse_view_query(Req, Keys),
        output_map_view(Req, View, Db, QueryArgs, Keys);
    {not_found, Reason} ->
        case couch_view:get_reduce_view({couch_db:name(Db),
                <<"_design/", Id/binary>>, ViewName}) of
        {ok, View} ->
            #view_query_args{
                reduce = Reduce
            } = QueryArgs = parse_view_query(Req, Keys, true),
            case Reduce of
            false ->
                {reduce, _N, _Lang, MapView} = View,
                output_map_view(Req, MapView, Db, QueryArgs, Keys);
            _ ->
                output_reduce_view(Req, View, QueryArgs, Keys)
            end;
        _ ->
            throw({not_found, Reason})
        end
    end.

handle_view_req(#httpd{method='GET',path_parts=[_,_, Id, ViewName]}=Req, Db) ->
    design_doc_view(Req, Db, Id, ViewName, nil);

handle_view_req(#httpd{method='POST',path_parts=[_,_, Id, ViewName]}=Req, Db) ->
    {Props} = couch_httpd:json_body(Req),
    Keys = proplists:get_value(<<"keys">>, Props, nil),
    design_doc_view(Req, Db, Id, ViewName, Keys);

handle_view_req(Req, _Db) ->
    send_method_not_allowed(Req, "GET,POST,HEAD").

handle_temp_view_req(#httpd{method='POST'}=Req, Db) ->
    QueryArgs = parse_view_query(Req),

    case couch_httpd:primary_header_value(Req, "content-type") of
        undefined -> ok;
        "application/json" -> ok;
        Else -> throw({incorrect_mime_type, Else})
    end,
    {Props} = couch_httpd:json_body(Req),
    Language = proplists:get_value(<<"language">>, Props, <<"javascript">>),
    MapSrc = proplists:get_value(<<"map">>, Props),
    Keys = proplists:get_value(<<"keys">>, Props, nil),
    case proplists:get_value(<<"reduce">>, Props, null) of
    null ->
        {ok, View} = couch_view:get_map_view({temp, couch_db:name(Db), Language, MapSrc}),
        output_map_view(Req, View, Db, QueryArgs, Keys);
    RedSrc ->
        {ok, View} = couch_view:get_reduce_view(
                {temp,  couch_db:name(Db), Language, MapSrc, RedSrc}),
        output_reduce_view(Req, View, QueryArgs, Keys)
    end;

handle_temp_view_req(Req, _Db) ->
    send_method_not_allowed(Req, "POST").

output_map_view(Req, View, Db, QueryArgs, nil) ->
    #view_query_args{
        count = Count,
        direction = Dir,
        skip = SkipCount,
        start_key = StartKey,
        start_docid = StartDocId
    } = QueryArgs,
    {ok, RowCount} = couch_view:get_row_count(View),
    Start = {StartKey, StartDocId},
    FoldlFun = make_view_fold_fun(Req, QueryArgs, Db, RowCount,
            fun couch_view:reduce_to_count/1),
    FoldAccInit = {Count, SkipCount, undefined, []},
    FoldResult = couch_view:fold(View, Start, Dir, FoldlFun, FoldAccInit),
    finish_view_fold(Req, RowCount, FoldResult);
    
output_map_view(Req, View, Db, QueryArgs, Keys) ->
    #view_query_args{
        count = Count,
        direction = Dir,
        skip = SkipCount,
        start_docid = StartDocId
    } = QueryArgs,
    {ok, RowCount} = couch_view:get_row_count(View),
    FoldAccInit = {Count, SkipCount, undefined, []},
    FoldResult = lists:foldl(
        fun(Key, {ok, FoldAcc}) ->
            Start = {Key, StartDocId},
            FoldlFun = make_view_fold_fun(Req,
                QueryArgs#view_query_args{
                    start_key = Key,
                    end_key = Key
                }, Db, RowCount, fun couch_view:reduce_to_count/1),
            couch_view:fold(View, Start, Dir, FoldlFun, FoldAcc)
        end, {ok, FoldAccInit}, Keys),
    finish_view_fold(Req, RowCount, FoldResult).

output_reduce_view(Req, View, QueryArgs, nil) ->
    #view_query_args{
        start_key = StartKey,
        end_key = EndKey,
        count = Count,
        skip = Skip,
        direction = Dir,
        start_docid = StartDocId,
        end_docid = EndDocId,
        group_level = GroupLevel
    } = QueryArgs,
    {ok, Resp} = start_json_response(Req, 200),
    {ok, GroupRowsFun, RespFun} = make_reduce_fold_funs(Resp, GroupLevel),
    send_chunk(Resp, "{\"rows\":["),
    {ok, _} = couch_view:fold_reduce(View, Dir, {StartKey, StartDocId}, 
        {EndKey, EndDocId}, GroupRowsFun, RespFun, {"", Skip, Count}),
    send_chunk(Resp, "]}"),
    end_json_response(Resp);
    
output_reduce_view(Req, View, QueryArgs, Keys) ->
    #view_query_args{
        count = Count,
        skip = Skip,
        direction = Dir,
        start_docid = StartDocId,
        end_docid = EndDocId,
        group_level = GroupLevel
    } = QueryArgs,
    {ok, Resp} = start_json_response(Req, 200),
    {ok, GroupRowsFun, RespFun} = make_reduce_fold_funs(Resp, GroupLevel),
    send_chunk(Resp, "{\"rows\":["),
    lists:foldl(
        fun(Key, AccSeparator) ->
            {ok, _} = couch_view:fold_reduce(View, Dir, {Key, StartDocId}, 
                {Key, EndDocId}, GroupRowsFun, RespFun, 
                {AccSeparator, Skip, Count}),
            "," % Switch to comma
        end,
    "", Keys), % Start with no comma
    send_chunk(Resp, "]}"),
    end_json_response(Resp).
    
make_reduce_fold_funs(Resp, GroupLevel) ->
    GroupRowsFun =
        fun({_Key1,_}, {_Key2,_}) when GroupLevel == 0 ->
            true;
        ({Key1,_}, {Key2,_})
                when is_integer(GroupLevel) and is_list(Key1) and is_list(Key2) ->
            lists:sublist(Key1, GroupLevel) == lists:sublist(Key2, GroupLevel);
        ({Key1,_}, {Key2,_}) ->
            Key1 == Key2
        end,
    RespFun = fun(_Key, _Red, {AccSeparator,AccSkip,AccCount}) when AccSkip > 0 ->
        {ok, {AccSeparator,AccSkip-1,AccCount}};
    (_Key, _Red, {AccSeparator,0,AccCount}) when AccCount == 0 ->
        {stop, {AccSeparator,0,AccCount}};
    (_Key, Red, {AccSeparator,0,AccCount}) when GroupLevel == 0 ->
        Json = ?JSON_ENCODE({[{key, null}, {value, Red}]}),
        send_chunk(Resp, AccSeparator ++ Json),
        {ok, {",",0,AccCount-1}};
    (Key, Red, {AccSeparator,0,AccCount})
            when is_integer(GroupLevel) 
            andalso is_list(Key) ->
        Json = ?JSON_ENCODE(
            {[{key, lists:sublist(Key, GroupLevel)},{value, Red}]}),
        send_chunk(Resp, AccSeparator ++ Json),
        {ok, {",",0,AccCount-1}};
    (Key, Red, {AccSeparator,0,AccCount}) ->
        Json = ?JSON_ENCODE({[{key, Key}, {value, Red}]}),
        send_chunk(Resp, AccSeparator ++ Json),
        {ok, {",",0,AccCount-1}}
    end,
    {ok, GroupRowsFun, RespFun}.
    



reverse_key_default(nil) -> {};
reverse_key_default({}) -> nil;
reverse_key_default(Key) -> Key.

parse_view_query(Req) ->
    parse_view_query(Req, nil, nil).
parse_view_query(Req, Keys) ->
    parse_view_query(Req, Keys, nil).
parse_view_query(Req, Keys, IsReduce) ->
    QueryList = couch_httpd:qs(Req),
    #view_query_args{
        group_level = GroupLevel
    } = QueryArgs = lists:foldl(fun({Key,Value}, Args) ->
        case {Key, Value} of
        {"", _} ->
            Args;
        {"key", Value} ->
            case Keys of
            nil ->
                JsonKey = ?JSON_DECODE(Value),
                Args#view_query_args{start_key=JsonKey,end_key=JsonKey};
            _ ->
                Msg = io_lib:format("Query parameter \"~s\" not compatible with multi key mode.", [Key]),
                throw({query_parse_error, Msg})
            end;
        {"startkey_docid", DocId} ->
            Args#view_query_args{start_docid=list_to_binary(DocId)};
        {"endkey_docid", DocId} ->
            Args#view_query_args{end_docid=list_to_binary(DocId)};
        {"startkey", Value} ->
            case Keys of
            nil ->
                Args#view_query_args{start_key=?JSON_DECODE(Value)};
            _ ->
                Msg = io_lib:format("Query parameter \"~s\" not compatible with multi key mode.", [Key]),
                throw({query_parse_error, Msg})
            end;
        {"endkey", Value} ->
            case Keys of
            nil ->
                Args#view_query_args{end_key=?JSON_DECODE(Value)};
            _ ->
                Msg = io_lib:format("Query parameter \"~s\" not compatible with multi key mode.", [Key]),
                throw({query_parse_error, Msg})
            end;
        {"count", Value} ->
            case (catch list_to_integer(Value)) of
            Count when is_integer(Count) ->
                if Count < 0 ->
                    Msg = io_lib:format("Count must be a positive integer: count=~s", [Value]),
                    throw({query_parse_error, Msg});
                true ->
                    Args#view_query_args{count=Count}
                end;
            _Error ->
                Msg = io_lib:format("Bad URL query value, number expected: count=~s", [Value]),
                throw({query_parse_error, Msg})
            end;
        {"update", "false"} ->
            Args#view_query_args{update=false};
        {"descending", "true"} ->
            case Args#view_query_args.direction of
            fwd ->
                Args#view_query_args {
                    direction = rev,
                    start_key = reverse_key_default(Args#view_query_args.start_key),
                    start_docid = reverse_key_default(Args#view_query_args.start_docid),
                    end_key = reverse_key_default(Args#view_query_args.end_key),
                    end_docid =  reverse_key_default(Args#view_query_args.end_docid)};
            _ ->
                Args %already reversed
            end;
        {"descending", "false"} ->
          % The descending=false behaviour is the default behaviour, so we
          % simpply ignore it. This is only for convenience when playing with
          % the HTTP API, so that a user doesn't get served an error when
          % flipping true to false in the descending option.
          Args;
        {"skip", Value} ->
            case (catch list_to_integer(Value)) of
            Count when is_integer(Count) ->
                Args#view_query_args{skip=Count};
            _Error ->
                Msg = lists:flatten(io_lib:format(
                "Bad URL query value, number expected: skip=~s", [Value])),
                throw({query_parse_error, Msg})
            end;
        {"group", Value} ->
            case Value of
            "true" ->
                Args#view_query_args{group_level=exact};
            "false" ->
                Args#view_query_args{group_level=0};
            _ ->
                Msg = "Bad URL query value for 'group' expected \"true\" or \"false\".",
                throw({query_parse_error, Msg})
            end;
        {"group_level", LevelStr} ->
            case Keys of
            nil ->
                Args#view_query_args{group_level=list_to_integer(LevelStr)};
            _ ->
                Msg = lists:flatten(io_lib:format("Multi-key fetches for a reduce view must include group=true", [])),
                throw({query_parse_error, Msg})
            end;
        {"reduce", "true"} ->
            Args#view_query_args{reduce=true};
        {"reduce", "false"} ->
            Args#view_query_args{reduce=false};
        {"include_docs", Value} ->
            case IsReduce of
            true ->
                Msg = lists:flatten(io_lib:format("Bad URL query key for reduce operation: ~s", [Key])),
                throw({query_parse_error, Msg});
            _ ->
                ok
            end,
            case Value of
            "true" ->
                Args#view_query_args{include_docs=true};
            "false" ->
                Args#view_query_args{include_docs=false};
            _ ->
                Msg1 = "Bad URL query value for 'include_docs' expected \"true\" or \"false\".",
                throw({query_parse_error, Msg1})
            end;
        _ -> % unknown key
            Msg = lists:flatten(io_lib:format(
                "Bad URL query key:~s", [Key])),
            throw({query_parse_error, Msg})
        end
    end, #view_query_args{}, QueryList),
    case Keys of
    nil ->
        QueryArgs;
    _ ->
        case IsReduce of
        nil ->
            QueryArgs;
        _ ->
            case GroupLevel of
            exact ->
                QueryArgs;
            _ ->
                Msg = lists:flatten(io_lib:format(
                    "Multi-key fetches for a reduce view must include group=true", [])),
                throw({query_parse_error, Msg})
            end
        end
    end.


make_view_fold_fun(Req, QueryArgs, Db, TotalViewCount, ReduceCountFun) ->
    #view_query_args{
        end_key = EndKey,
        end_docid = EndDocId,
        include_docs = IncludeDocs,
        direction = Dir
    } = QueryArgs,

    PassedEndFun =
    case Dir of
    fwd ->
        fun(ViewKey, ViewId) ->
            couch_view:less_json([EndKey, EndDocId], [ViewKey, ViewId])
        end;
    rev->
        fun(ViewKey, ViewId) ->
            couch_view:less_json([ViewKey, ViewId], [EndKey, EndDocId])
        end
    end,

    fun({{Key, DocId}, Value}, OffsetReds,
                      {AccCount, AccSkip, Resp, AccRevRows}) ->
        Offset = ReduceCountFun(OffsetReds), % I think we only need this call once per view
        PassedEnd = PassedEndFun(Key, DocId),
        case {PassedEnd, AccCount, AccSkip, Resp} of
        {true, _, _, _} ->
            % The stop key has been passed, stop looping.
            {stop, {AccCount, AccSkip, Resp, AccRevRows}};
        {_, 0, _, _} ->
            % we've done "count" rows, stop foldling
            {stop, {0, 0, Resp, AccRevRows}};
        {_, _, AccSkip, _} when AccSkip > 0 ->
            {ok, {AccCount, AccSkip - 1, Resp, AccRevRows}};
        {_, _, _, undefined} ->
            {ok, Resp2} = start_json_response(Req, 200),
            JsonBegin = io_lib:format("{\"total_rows\":~w,\"offset\":~w,\"rows\":[\r\n",
                    [TotalViewCount, Offset]),
            JsonObj = view_row_obj(Db, {{Key, DocId}, Value}, IncludeDocs),
            send_chunk(Resp2, JsonBegin ++ ?JSON_ENCODE(JsonObj)),
            {ok, {AccCount - 1, 0, Resp2, AccRevRows}};
        {_, AccCount, _, Resp} when (AccCount > 0) ->
            JsonObj = view_row_obj(Db, {{Key, DocId}, Value}, IncludeDocs),
            send_chunk(Resp, ",\r\n" ++  ?JSON_ENCODE(JsonObj)),
            {ok, {AccCount - 1, 0, Resp, AccRevRows}}
        end
    end.

view_row_obj(Db, {{Key, DocId}, Value}, IncludeDocs) ->
    case DocId of
    error ->
        {[{key, Key}, {error, Value}]};
    _ ->
        case IncludeDocs of
        true ->
            Rev = case Value of
            {Props} ->
                case is_list(Props) of
                true ->
                    proplists:get_value(<<"_rev">>, Props, []);
                _ ->
                    []
                end;
            _ ->
                []
            end,
            ?LOG_DEBUG("Include Doc: ~p ~p", [DocId, Rev]),
            case (catch couch_httpd_db:couch_doc_open(Db, DocId, 
                Rev, [])) of
            {{not_found, missing}, _} ->
                {[{id, DocId}, {key, Key}, {value, Value}, {error, missing}]};
            Doc ->
                JsonDoc = couch_doc:to_json_obj(Doc, []),
                {[{id, DocId}, {key, Key}, {value, Value}, {doc, JsonDoc}]}
            end;
        _ ->
            {[{id, DocId}, {key, Key}, {value, Value}]}
        end
    end.

finish_view_fold(Req, TotalRows, FoldResult) ->
    case FoldResult of
    {ok, {_, _, undefined, _}} ->
        % nothing found in the view, nothing has been returned
        % send empty view
        send_json(Req, 200, {[
            {total_rows, TotalRows},
            {rows, []}
        ]});
    {ok, {_, _, Resp, AccRevRows}} ->
        % end the view
        send_chunk(Resp, AccRevRows ++ "\r\n]}"),
        end_json_response(Resp);
    Error ->
        throw(Error)
    end.