Skip to content

Instantly share code, notes, and snippets.

@fritzy
Created November 5, 2013 17:29
Show Gist options
  • Save fritzy/7322802 to your computer and use it in GitHub Desktop.
Save fritzy/7322802 to your computer and use it in GitHub Desktop.
A Map-Reduce implementation for LevelUp. Maps do not necessarily return database key/value pairs, and may return any number of key/value pairs. For example, it could return word-counts or any derived pairs. Or you could simple return a single [{dbkey: value}] for every key that matched a pattern. Maps could also convert formats. Reduce is then c…
/* mapReduce
* arguments (
* lup: levelup instance,
* map: function ({key, value}) { return [{key: mapkey, value: mapvalue}, ...]; },
* reduce: function (mapkey, [mapvalue, ...]) { return reduced; },
* callback: function (err, {mapkey: reduced, ...})
* )
*
* A map is called for each key pair. A map returns an array of key value pairs to be reduced.
* Key value pairs may be derived keys and values, or database keys and values.
* Each map call may return *any* size of key value pair array.
*
* The reduce function is called from the collated mapkey, mapvalue pairs.
* A reduce function returns a single result to be assigned to that mapkey as a whole.
*
* The callback recieves an object of mapkeys, each with their reduced value.
*/
function mapReduce(lup, map_fn, reduce_fn, callback) {
var mapped = {};
var results = {};
var key_stream;
var kidx;
//go through all of the key/value pairs
key_stream = lup.createReadStream({keys: true, values: true});
//for each key/value pair
key_stream.on('data', function (data) {
var pairs = map_fn(data);
for (var pidx in pairs) {
if (!mapped.hasOwnProperty(pairs[pidx].key)) {
mapped[pairs[pidx].key] = [pairs[pidx].value];
} else {
mapped[pairs[pidx].key].push(pairs[pidx].value);
}
}
});
//reduce when done
key_stream.once('end', function () {
key_stream.removeAllListeners();
for (var midx in mapped) {
results[midx] = reduce_fn(midx, mapped[midx]);
}
callback(err, results);
});
}
module.exports = mapReduce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment