diff options
author | John Christopher Anderson <jchris@apache.org> | 2008-09-20 22:18:45 +0000 |
---|---|---|
committer | John Christopher Anderson <jchris@apache.org> | 2008-09-20 22:18:45 +0000 |
commit | 9a93b44ef02014a6e269b70d8c29362a3e4b3afb (patch) | |
tree | 735c0e8c9fd2ea4d4768afd8448993744953d74f | |
parent | 22f822cb8252d87fc19ec62108b566fd5057f9d9 (diff) |
reduce=false closes COUCHDB-76
git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@697427 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | share/www/script/couch_tests.js | 34 | ||||
-rw-r--r-- | src/couchdb/couch_httpd.erl | 24 |
2 files changed, 55 insertions, 3 deletions
diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index 7c48a79a..cc08e6f8 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -539,6 +539,40 @@ var tests = { }, + reduce_false: function(debug) { + var db = new CouchDB("test_suite_db"); + db.deleteDb(); + db.createDb(); + if (debug) debugger; + + var numDocs = 5; + var docs = makeDocs(1,numDocs + 1); + T(db.bulkSave(docs).ok); + var summate = function(N) {return (N+1)*N/2;}; + + var designDoc = { + _id:"_design/test", + language: "javascript", + views: { + summate: {map:"function (doc) {emit(doc.integer, doc.integer)};", + reduce:"function (keys, values) { return sum(values); };"}, + } + }; + T(db.save(designDoc).ok); + + // Test that the reduce works + var res = db.view('test/summate'); + T(res.rows.length == 1 && res.rows[0].value == summate(5)); + + //Test that we get our docs back + res = db.view('test/summate', {reduce: false}); + T(res.rows.length == 5); + for(var i=0; i<5; i++) + { + T(res.rows[i].value == i+1); + } + }, + multiple_rows: function(debug) { var db = new CouchDB("test_suite_db"); db.deleteDb(); diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 604e1454..e2b4f692 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -35,7 +35,8 @@ start_docid = nil, end_docid = {}, skip = 0, - group_level = 0 + group_level = 0, + reduce = true }). start_link() -> @@ -418,7 +419,8 @@ handle_db_request(Req, 'GET', {DbName, _Db, [<<"_view">>, DocId, ViewName]}) -> count = Count, skip = SkipCount, direction = Dir, - start_docid = StartDocId + start_docid = StartDocId, + reduce = Reduce } = QueryArgs = parse_view_query(Req), case couch_view:get_map_view({DbName, <<"_design/", DocId/binary>>, ViewName}) of @@ -433,7 +435,19 @@ handle_db_request(Req, 'GET', {DbName, _Db, [<<"_view">>, DocId, ViewName]}) -> {not_found, Reason} -> case couch_view:get_reduce_view({DbName, <<"_design/", DocId/binary>>, ViewName}) of {ok, View} -> - output_reduce_view(Req, View); + case Reduce of + false -> + {reduce, _N, _Lang, MapView} = View, + {ok, RowCount} = couch_view:get_row_count(MapView), + Start = {StartKey, StartDocId}, + FoldlFun = make_view_fold_fun(Req, QueryArgs, RowCount, + fun couch_view:reduce_to_count/1), + FoldAccInit = {Count, SkipCount, undefined, []}, + FoldResult = couch_view:fold(MapView, Start, Dir, FoldlFun, FoldAccInit), + finish_view_fold(Req, RowCount, FoldResult); + _ -> + output_reduce_view(Req, View) + end; _ -> throw({not_found, Reason}) end @@ -940,6 +954,10 @@ parse_view_query(Req) -> Args#view_query_args{group_level=exact}; {"group_level", LevelStr} -> Args#view_query_args{group_level=list_to_integer(LevelStr)}; + {"reduce", "true"} -> + Args#view_query_args{reduce=true}; + {"reduce", "false"} -> + Args#view_query_args{reduce=false}; _ -> % unknown key Msg = lists:flatten(io_lib:format( "Bad URL query key:~s", [Key])), |