summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--share/server/main.js4
-rw-r--r--share/www/script/couch_tests.js40
-rw-r--r--src/couchdb/couch_btree.erl7
-rw-r--r--src/couchdb/couch_view.erl16
4 files changed, 31 insertions, 36 deletions
diff --git a/share/server/main.js b/share/server/main.js
index e7184682..59acb02f 100644
--- a/share/server/main.js
+++ b/share/server/main.js
@@ -16,7 +16,7 @@ var map_results = []; // holds temporary emitted values during doc map
var sandbox = null;
-map = function(key, value) {
+emit = function(key, value) {
map_results.push([key, value]);
}
@@ -32,7 +32,7 @@ sum = function(values) {
try {
// if possible, use evalcx (not always available)
sandbox = evalcx('');
- sandbox.map = map;
+ sandbox.emit = emit;
sandbox.sum = sum;
} catch (e) {}
diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js
index 7b35cf87..cb8e8506 100644
--- a/share/www/script/couch_tests.js
+++ b/share/www/script/couch_tests.js
@@ -59,7 +59,7 @@ var tests = {
// has a value of 4, and then returns the document's b value.
var mapFunction = function(doc){
if(doc.a==4)
- map(null, doc.b);
+ emit(null, doc.b);
};
results = db.query(mapFunction);
@@ -208,7 +208,7 @@ var tests = {
}
// query all documents, and return the doc.integer member as a key.
- results = db.query(function(doc){ map(doc.integer, null) });
+ results = db.query(function(doc){ emit(doc.integer, null) });
T(results.total_rows == numDocsToCreate);
@@ -218,7 +218,7 @@ var tests = {
}
// do the query again, but with descending output
- results = db.query(function(doc){ map(doc.integer, null) }, {descending:true});
+ results = db.query(function(doc){ emit(doc.integer, null) }, {descending:true});
T(results.total_rows == numDocsToCreate);
@@ -238,7 +238,7 @@ var tests = {
T(db.bulkSave(docs).ok);
var summate = function(N) {return (N+1)*N/2;};
- var map = function (doc) {map(doc.integer, doc.integer)};
+ var map = function (doc) {emit(doc.integer, doc.integer)};
var reduce = function (keys, values) { return sum(values); };
var result = db.reduce_query(map, reduce).result;
T(result == summate(numDocs));
@@ -277,7 +277,7 @@ var tests = {
var generateListOfCitiesAndState = "function(doc) {" +
" for (var i = 0; i < doc.cities.length; i++)" +
- " map(doc.cities[i] + \", \" + doc._id, null);" +
+ " emit(doc.cities[i] + \", \" + doc._id, null);" +
"}";
var results = db.query(generateListOfCitiesAndState);
@@ -348,7 +348,7 @@ var tests = {
// query all documents, and return the doc.foo member as a key.
results = db.query(function(doc){
- map(null, doc.longtest);
+ emit(null, doc.longtest);
});
},
@@ -376,7 +376,7 @@ var tests = {
}
// check that views and key collation don't blow up
- var rows = db.query(function(doc) { map(null, doc.text) }).rows;
+ var rows = db.query(function(doc) { emit(null, doc.text) }).rows;
for (var i=0; i<texts.length; i++) {
T(rows[i].value == texts[i]);
}
@@ -438,10 +438,10 @@ var tests = {
_id:"_design/test",
language: "javascript",
views: {
- all_docs: "function(doc) { map(doc.integer, null) }",
- no_docs: "function(doc) {}",
- single_doc: "function(doc) { if (doc._id == \"1\") { map(1, null) }}",
- summate: {map:"function (doc) {map(doc.integer, doc.integer)};",
+ all_docs: {map: "function(doc) { emit(doc.integer, null) }"},
+ no_docs: {map: "function(doc) {}"},
+ single_doc: {map: "function(doc) { if (doc._id == \"1\") { emit(1, null) }}"},
+ summate: {map:"function (doc) {emit(doc.integer, doc.integer)};",
reduce:"function (keys, values) { return sum(values); };"}
}
}
@@ -548,7 +548,7 @@ var tests = {
db.save({_id:(i).toString(), foo:values[i]});
}
- var queryFun = function(doc) { map(doc.foo, null); }
+ var queryFun = function(doc) { emit(doc.foo, null); }
var rows = db.query(queryFun).rows;
for (i=0; i<values.length; i++) {
T(equals(rows[i].key, values[i]))
@@ -600,7 +600,7 @@ var tests = {
var results = dbB.query(function(doc) {
if (doc._conflicts) {
- map(doc._id, doc._conflicts);
+ emit(doc._id, doc._conflicts);
}
});
T(results.rows[0].value[0] == conflictRev);
@@ -615,7 +615,7 @@ var tests = {
var docs = makeDocs(0, 100);
T(db.bulkSave(docs).ok);
- var queryFun = function(doc) { map(doc.integer, null) };
+ var queryFun = function(doc) { emit(doc.integer, null) };
var i;
// page through the view ascending and going forward
@@ -695,7 +695,7 @@ var tests = {
// make sure that attempting to change the document throws an error
var results = db.query(function(doc) {
doc._id = "foo";
- map(null, doc);
+ emit(null, doc);
});
T(results.total_rows == 0);
@@ -703,18 +703,18 @@ var tests = {
// garbage collector
var results = db.query(function(doc) {
gc();
- map(null, doc);
+ emit(null, doc);
});
T(results.total_rows == 0);
// make sure that a view cannot access the map_funs array defined used by
// the view server
- var results = db.query(function(doc) { map_funs.push(1); map(null, doc) });
+ var results = db.query(function(doc) { map_funs.push(1); emit(null, doc) });
T(results.total_rows == 0);
// make sure that a view cannot access the map_results array defined used by
// the view server
- var results = db.query(function(doc) { map_results.push(1); map(null, doc) });
+ var results = db.query(function(doc) { map_results.push(1); emit(null, doc) });
T(results.total_rows == 0);
},
@@ -730,7 +730,7 @@ var tests = {
var results = db.query(
"function(doc) {\n" +
" var xml = new XML(doc.content);\n" +
- " map(xml.title.text(), null);\n" +
+ " emit(xml.title.text(), null);\n" +
"}");
T(results.total_rows == 2);
T(results.rows[0].key == "Testing E4X");
@@ -739,7 +739,7 @@ var tests = {
var results = db.query(
"function(doc) {\n" +
" var xml = new XML(doc.content);\n" +
- " map(xml.title.@id, null);\n" +
+ " emit(xml.title.@id, null);\n" +
"}");
T(results.total_rows == 2);
T(results.rows[0].key == "e4x");
diff --git a/src/couchdb/couch_btree.erl b/src/couchdb/couch_btree.erl
index 6fd9b0c8..57013ee9 100644
--- a/src/couchdb/couch_btree.erl
+++ b/src/couchdb/couch_btree.erl
@@ -419,7 +419,7 @@ collect_node(Bt, {P, R}, KeyStart, KeyEnd) ->
true -> % got full node, return the already calculated reduction
{[], [{nil, {P, R}}]};
false -> % otherwise return the keyvalues for later reduction
- {KVs2, []}
+ {[assemble(Bt,K,V) || {K,V} <- KVs2], []}
end
end.
@@ -568,9 +568,10 @@ stream_kv_node(Bt, Reds, KVs, StartKey, Dir, Fun, Acc) ->
stream_kv_node2(_Bt, _Reds, _PrevKVs, [], _Dir, _Fun, Acc) ->
{ok, Acc};
stream_kv_node2(Bt, Reds, PrevKVs, [{K,V} | RestKVs], Dir, Fun, Acc) ->
- case Fun(assemble(Bt, K, V), {PrevKVs, Reds}, Acc) of
+ AssembledKV = assemble(Bt, K, V),
+ case Fun(AssembledKV, {PrevKVs, Reds}, Acc) of
{ok, Acc2} ->
- stream_kv_node2(Bt, Reds, [{K,V} | PrevKVs], RestKVs, Dir, Fun, Acc2);
+ stream_kv_node2(Bt, Reds, [AssembledKV | PrevKVs], RestKVs, Dir, Fun, Acc2);
{stop, Acc2} ->
{stop, Acc2}
end.
diff --git a/src/couchdb/couch_view.erl b/src/couchdb/couch_view.erl
index 4f6a2f47..b14f4564 100644
--- a/src/couchdb/couch_view.erl
+++ b/src/couchdb/couch_view.erl
@@ -155,32 +155,26 @@ reduce_to_count(Reductions) ->
design_doc_to_view_group(#doc{id=Id,body={obj, Fields}}) ->
Language = proplists:get_value("language", Fields, "javascript"),
{obj, RawViews} = proplists:get_value("views", Fields, {obj, []}),
-
- % extract the map/reduce views from the json fields and into lists
- MapViewsRaw = [{Name, Src, nil} || {Name, Src} <- RawViews, is_list(Src)],
- MapReduceViewsRaw =
- [{Name,
- proplists:get_value("map", MRFuns),
- proplists:get_value("reduce", MRFuns)}
- || {Name, {obj, MRFuns}} <- RawViews],
% add the views to a dictionary object, with the map source as the key
DictBySrc =
lists:foldl(
- fun({Name, MapSrc, RedSrc}, DictBySrcAcc) ->
+ fun({Name, {obj, MRFuns}}, DictBySrcAcc) ->
+ MapSrc = proplists:get_value("map", MRFuns),
+ RedSrc = proplists:get_value("reduce", MRFuns, null),
View =
case dict:find(MapSrc, DictBySrcAcc) of
{ok, View0} -> View0;
error -> #view{def=MapSrc} % create new view object
end,
View2 =
- if RedSrc == nil ->
+ if RedSrc == null ->
View#view{map_names=[Name|View#view.map_names]};
true ->
View#view{reduce_funs=[{Name,RedSrc}|View#view.reduce_funs]}
end,
dict:store(MapSrc, View2, DictBySrcAcc)
- end, dict:new(), MapViewsRaw ++ MapReduceViewsRaw),
+ end, dict:new(), RawViews),
% number the views
{Views, _N} = lists:mapfoldl(
fun({_Src, View}, N) ->