Skip to content

Instantly share code, notes, and snippets.

@radleta
Last active November 26, 2019 19:40
Show Gist options
  • Save radleta/f275d8f77008627889b1f98b9b12c2f0 to your computer and use it in GitHub Desktop.
Save radleta/f275d8f77008627889b1f98b9b12c2f0 to your computer and use it in GitHub Desktop.
The map and reduce functions for Couchbase View to count the prefix of a cache key separated with an underscore.
function (doc, meta) {
var firstSeparator = meta.id.indexOf('_');
if (firstSeparator > -1) {
var firstKeyPart = meta.id.substring(0, firstSeparator);
emit(firstKeyPart, null);
} else {
emit('n/a', null);
}
}
function (keys, values, rereduce) {
var result = {}
if (rereduce) {
values.forEach(function (v) {
for(var n in v) {
result[n] = (result[n] || 0) + v[n];
}
});
} else {
keys.forEach(function (k) {
result[k] = (result[k] || 0) + 1;
});
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment