Mercurial > browser-couch
changeset 15:01237459756d
Realized I had the wrong idea of what reduce was doing and reimpemented it properly.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 10 Apr 2009 17:03:33 -0700 |
parents | fbee4473dbdd |
children | 53b9f2e988da |
files | browser-couch.js tests.js |
diffstat | 2 files changed, 25 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/browser-couch.js Fri Apr 10 14:46:52 2009 -0700 +++ b/browser-couch.js Fri Apr 10 17:03:33 2009 -0700 @@ -205,15 +205,20 @@ _mapReduce: function BC__mapReduce(map, reduce, dict, progress, finished, chunkSize) { var len = dict.getLength(); - var mapResult = {}; + var mapKeys = {}; + var mapValues = {}; + var currDoc; function emit(key, value) { // TODO: This assumes that the key will always be // an indexable value. We may have to hash the value, // though, if it's e.g. an Object. - if (!mapResult[key]) - mapResult[key] = []; - mapResult[key].push(value); + if (!mapKeys[key]) { + mapKeys[key] = []; + mapValues[key] = []; + } + mapKeys[key].push([key, currDoc.id]); + mapValues[key].push(value); } // Maximum number of items to process before giving the UI a chance @@ -233,7 +238,8 @@ var iAtStart = i; do { - map(dict.getNthValue(i), emit); + currDoc = dict.getNthValue(i); + map(currDoc, emit); i++; } while (i - iAtStart < chunkSize && i < len) @@ -251,26 +257,20 @@ continueMap(); function doReduce() { + var reduceResult; if (reduce) { - var keys = []; - var values = []; - - for (key in mapResult) { - keys.push(key); - values.push(mapResult[key]); + reduceResult = {}; + for (key in mapKeys) { + reduceResult[key] = reduce(mapKeys[key], + mapValues[key]); } - - finished(reduce(keys, values)); } else { - var result = []; - - for (key in mapResult) { - var values = mapResult[key]; - for (var i = 0; i < values.length; i++) - result.push([key, values[i]]); - } - finished(result); + reduceResult = []; + for (key in mapValues) + for (var i = 0; i < mapValues[key].length; i++) + reduceResult.push([key, mapValues[key][i]]); } + finished(reduceResult); } } };
--- a/tests.js Fri Apr 10 14:46:52 2009 -0700 +++ b/tests.js Fri Apr 10 17:03:33 2009 -0700 @@ -79,10 +79,10 @@ emit(words[i], 1); }, reduce: function(keys, values) { - var totals = {}; - for (var i = 0; i < keys.length; i++) - totals[keys[i]] = values[i].length; - return totals; + var sum = 0; + for (var i = 0; i < values.length; i++) + sum += values[i]; + return sum; }, chunkSize: 1, progress: function(percentDone, resume) {