summaryrefslogtreecommitdiff
path: root/share
diff options
context:
space:
mode:
authorDamien F. Katz <damien@apache.org>2008-06-05 23:49:48 +0000
committerDamien F. Katz <damien@apache.org>2008-06-05 23:49:48 +0000
commit712830243c89a0863831ead8e983f089fd37fd42 (patch)
treeeb3f6f7cb8f08684f63d71869817cd898412ffd0 /share
parent43776b676961ec093556fa5693298463ecee2ff5 (diff)
Added reduce/combine example. Fixed broken node chunking with very large keys/reduction values
git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@663786 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'share')
-rw-r--r--share/www/script/couch_tests.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js
index 97e91f1e..effab90c 100644
--- a/share/www/script/couch_tests.js
+++ b/share/www/script/couch_tests.js
@@ -294,6 +294,9 @@ var tests = {
result = db.query(map, reduce, {startkey: i, endkey: numDocs - i});
T(result.rows[0].value == summate(numDocs-i) - summate(i-1));
}
+
+ db.deleteDb();
+ db.createDb();
for(var i=1; i <= 5; i++) {
@@ -340,6 +343,68 @@ var tests = {
T(equals(results.rows[5], {key:["d","b"],value:10*i}));
T(equals(results.rows[6], {key:["d","c"],value:10*i}));
}
+
+ // now test out more complex reductions that need to use the combine option.
+
+ db.deleteDb();
+ db.createDb();
+
+
+ var map = function (doc) {emit(null, doc.val)};
+ var reduceCombine = function (keys, values, combine) {
+ // this computes the standard deviation
+
+ var stdDeviation=0;
+ var count=0;
+ var total=0;
+ var sqrTotal=0;
+
+ if (combine) {
+ for(var i in values) {
+ count = count + values[i].count;
+ total = total + values[i].total;
+ sqrTotal = sqrTotal + (values[i].sqrTotal * values[i].sqrTotal);
+ }
+ var variance = (sqrTotal - ((total * total)/count)) / count;
+ stdDeviation = Math.sqrt(variance);
+
+ return {"stdDeviation":stdDeviation,"count":count,
+ "total":total,"sqrTotal":sqrTotal};
+ }
+ else {
+ for(var i in values) {
+ total = total + values[i]
+ sqrTotal = sqrTotal + (values[i] * values[i])
+ }
+ count = values.length;
+ var variance = (sqrTotal - ((total * total)/count)) / count;
+ stdDeviation = Math.sqrt(variance);
+ }
+
+ return {"stdDeviation":stdDeviation,"count":count,
+ "total":total,"sqrTotal":sqrTotal};
+ };
+
+ for(var j=0; j < 10; j++) {
+ // these docs are in the order of the keys collation, for clarity
+ var docs = [];
+ docs.push({val:10});
+ docs.push({val:20});
+ docs.push({val:30});
+ docs.push({val:40});
+ docs.push({val:50});
+ docs.push({val:60});
+ docs.push({val:70});
+ docs.push({val:80});
+ docs.push({val:90});
+ docs.push({val:100});
+ T(db.bulkSave(docs).ok);
+ }
+
+ var results = db.query(map, reduceCombine);
+ //account for floating point error
+ T(results.rows[0].value.stdDeviation == 28.722813232690143);
+
},
multiple_rows: function(debug) {