Last active
November 26, 2019 19:40
-
-
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.
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
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); | |
} | |
} |
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
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