Skip to content

Instantly share code, notes, and snippets.

@kocolosk
Created January 22, 2021 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kocolosk/4f568e6f425dffe362d3e181efec2601 to your computer and use it in GitHub Desktop.
Save kocolosk/4f568e6f425dffe362d3e181efec2601 to your computer and use it in GitHub Desktop.
Find hot partitions in CouchDB
function (keys, values, rereduce) {
var topTenPlusBoundaryKeys = function(partitions) {
// preserve boundary keys because we may not have the correct count for them yet
// not that it matters, but the array is reversed so these labels are correct
var first = partitions.pop();
var last = partitions.shift();
// sort the remaining entries by value
partitions.sort(function(p1, p2) { return p2.count - p1.count; });
// return the top ten partitions, plus the boundary partitions, all sorted
var topTen = partitions.slice(0, 10);
if(first) { topTen.push(first) };
if(last) { topTen.push(last) };
topTen.sort(function(p1, p2) { return p2.count - p1.count; });
return topTen;
};
if (rereduce) {
// account for boundary keys by summing over each partition
var totals = values.reduce(function(acc, currentVals) {
currentVals.forEach(function(elem) {
if(acc[elem.partition]) {
acc[elem.partition] += elem.count;
} else {
acc[elem.partition] = elem.count;
}
});
return acc;
}, {});
// convert back into an Array with expected structure
var reduced = [];
for (var elem in totals) {
reduced.push({partition: elem, count: totals[elem]})
};
// sort in reverse order just to stay consistent with rereduce=false
// again, all that's required is to find the boundary keys
reduced.sort(function(p1, p2) {
if(p2.partition < p1.partition) {
return -1;
}
if(p1.partition < p2.partition) {
return 1;
}
return 0;
});
return topTenPlusBoundaryKeys(reduced);
}
else {
// compute the number of index entries per partition
var reduced = keys.reduce(function(output, currentKey, index) {
if(currentKey[0] == output[0].partition) {
output[0].count += values[index]
}
else {
output.unshift({partition: currentKey[0], count: values[index]})
}
return output;
}, [{partition: keys[0][0], count: 0}]);
return topTenPlusBoundaryKeys(reduced);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment