From a684f95cbcee7f2568a2ce04e7dc2bbb605a27b3 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Thu, 15 May 2008 21:51:22 +0000 Subject: Incremental reduce first checkin. Warning! Disk format change. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@656861 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/couch.js | 18 +++++++++- share/www/script/couch_tests.js | 77 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 87 insertions(+), 8 deletions(-) (limited to 'share/www/script') diff --git a/share/www/script/couch.js b/share/www/script/couch.js index 5f42ac38..f72fb712 100644 --- a/share/www/script/couch.js +++ b/share/www/script/couch.js @@ -99,7 +99,23 @@ function CouchDB(name) { mapFun = mapFun.toSource ? mapFun.toSource() : "(" + mapFun.toString() + ")"; var req = request("POST", this.uri + "_temp_view" + encodeOptions(options), { headers: {"Content-Type": "text/javascript"}, - body: mapFun + body: JSON.stringify(mapFun) + }); + var result = JSON.parse(req.responseText); + if (req.status != 200) + throw result; + return result; + } + + // Applies the map function to the contents of database and returns the results. + this.reduce_query = function(mapFun, reduceFun, options) { + if (typeof(mapFun) != "string") + mapFun = mapFun.toSource ? mapFun.toSource() : "(" + mapFun.toString() + ")"; + if (typeof(reduceFun) != "string") + reduceFun = reduceFun.toSource ? reduceFun.toSource() : "(" + reduceFun.toString() + ")"; + var req = request("POST", this.uri + "_temp_view" + encodeOptions(options), { + headers: {"Content-Type": "text/javascript"}, + body: JSON.stringify({map:mapFun, reduce:reduceFun}) }); var result = JSON.parse(req.responseText); if (req.status != 200) diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js index 5f9cb5ae..72ed9f58 100644 --- a/share/www/script/couch_tests.js +++ b/share/www/script/couch_tests.js @@ -91,7 +91,15 @@ var tests = { // 1 more document should now be in the result. T(results.total_rows == 3); T(db.info().doc_count == 6); + + var reduceFunction = function(keys, values){ + return sum(values); + }; + + result = db.reduce_query(mapFunction, reduceFunction); + T(result.result == 33); + // delete a document T(db.deleteDoc(existingDoc).ok); @@ -219,6 +227,39 @@ var tests = { T(results.rows[numDocsToCreate-1-i].key==i); } }, + + reduce: function(debug) { + var db = new CouchDB("test_suite_db"); + db.deleteDb(); + db.createDb(); + if (debug) debugger; + var numDocs = 500 + var docs = makeDocs(1,numDocs + 1); + T(db.bulkSave(docs).ok); + var summate = function(N) {return (N+1)*N/2;}; + + var map = function (doc) {map(doc.integer, doc.integer)}; + var reduce = function (keys, values) { return sum(values); }; + var result = db.reduce_query(map, reduce).result; + T(result == summate(numDocs)); + + result = db.reduce_query(map, reduce, {startkey:4,endkey:4}).result; + + T(result == 4); + + result = db.reduce_query(map, reduce, {startkey:4,endkey:5}).result; + + T(result == 9); + + result = db.reduce_query(map, reduce, {startkey:4,endkey:6}).result; + + T(result == 15); + + for(var i=1; i