Created
November 7, 2012 23:21
-
-
Save nlf/4035312 to your computer and use it in GitHub Desktop.
riak madness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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