diff options
-rw-r--r-- | share/www/script/couch_tests.js | 6 | ||||
-rw-r--r-- | src/couchdb/couch_btree.erl | 21 | ||||
-rw-r--r-- | src/couchdb/couch_httpd.erl | 2 |
3 files changed, 19 insertions, 10 deletions
diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index 28afeaa9..0308ed8d 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -290,6 +290,12 @@ var tests = { result = db.query(map, reduce, {startkey: 4, endkey: 6}); T(result.rows[0].value == 15); + result = db.query(map, reduce, {group:true, count:3}); + T(result.rows.length == 3); + T(result.rows[0].value == 1); + T(result.rows[1].value == 2); + T(result.rows[2].value == 3); + for(var i=1; i<numDocs/2; i+=30) { result = db.query(map, reduce, {startkey: i, endkey: numDocs - i}); T(result.rows[0].value == summate(numDocs-i) - summate(i-1)); diff --git a/src/couchdb/couch_btree.erl b/src/couchdb/couch_btree.erl index ac19b4e9..563e6040 100644 --- a/src/couchdb/couch_btree.erl +++ b/src/couchdb/couch_btree.erl @@ -78,17 +78,20 @@ fold_reduce(#btree{root=Root}=Bt, Dir, StartKey, EndKey, KeyGroupFun, Fun, Acc) rev -> {EndKey, StartKey}; fwd -> {StartKey, EndKey} end, - {ok, Acc2, GroupedRedsAcc2, GroupedKVsAcc2, GroupedKey2} = - reduce_stream_node(Bt, Dir, Root, StartKey2, EndKey2, nil, [], [], - KeyGroupFun, Fun, Acc), - if GroupedKey2 == nil -> - {ok, Acc2}; - true -> - case (catch Fun(GroupedKey2, {GroupedKVsAcc2, GroupedRedsAcc2}, Acc2)) of + try + {ok, Acc2, GroupedRedsAcc2, GroupedKVsAcc2, GroupedKey2} = + reduce_stream_node(Bt, Dir, Root, StartKey2, EndKey2, nil, [], [], + KeyGroupFun, Fun, Acc), + if GroupedKey2 == nil -> + {ok, Acc2}; + true -> + case Fun(GroupedKey2, {GroupedKVsAcc2, GroupedRedsAcc2}, Acc2) of {ok, Acc3} -> {ok, Acc3}; - {stop, Acc3} -> {ok, Acc3}; - Else -> throw(Else) + {stop, Acc3} -> {ok, Acc3} + end end + catch + throw:{stop, AccDone} -> {ok, AccDone} end. full_reduce(#btree{root=nil,reduce=Reduce}) -> diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 49b8e9bc..88271390 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -465,7 +465,7 @@ output_reduce_view(Req, View) -> fun(_Key, _Red, {AccSeparator,AccSkip,AccCount}) when AccSkip > 0 -> {ok, {AccSeparator,AccSkip-1,AccCount}}; (_Key, _Red, {AccSeparator,0,AccCount}) when AccCount == 0 -> - {ok, {AccSeparator,0,AccCount}}; + {stop, {AccSeparator,0,AccCount}}; (_Key, Red, {AccSeparator,0,AccCount}) when GroupLevel == 0 -> Json = lists:flatten(cjson:encode({obj, [{key, null}, {value, Red}]})), Resp:write_chunk(AccSeparator ++ Json), |