Skip to content

Instantly share code, notes, and snippets.

@nlf
Created November 7, 2012 23:21
Show Gist options
  • Save nlf/4035312 to your computer and use it in GitHub Desktop.
Save nlf/4035312 to your computer and use it in GitHub Desktop.
riak madness
var client = require('simpleriak').createClient({ bucket: 'hb' });
function map(v, keydata, arg) {
if (v.values[0].metadata['X-Riak-Deleted']) return [];
var links = v.values[0].metadata.Links,
ret = [];
ret.push([v.bucket, v.key]);
for (var i = 0; i < links.length; i++) {
if (links[i][2] === arg) {
ret.push([links[i][0], links[i][1]]);
}
}
return ret;
}
function fetchBoth(v) {
return [v];
}
function reduceValues(v, arg) {
var ret = {},
links,
found;
for (var i = 0; i < v.length; i++) {
found = false;
links = v[i].values[0].metadata.Links;
for (var l = 0; l < links.length; l++) {
if (links[l][2] === arg) {
found = true;
ret[arg] = v[i].values[0].data;
}
}
if (!found) ret['object'] = v[i].values[0].data;
}
return ret;
}
client.mapred({ key: 'first', map: [{ source: map, arg: 'bar' }, fetchBoth], reduce: { source: reduceValues, arg: 'bar' } }, function (err, reply) {
console.log(err, reply);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment