Skip to content

Instantly share code, notes, and snippets.

@muhqu
Created January 19, 2012 15:42
Show Gist options
  • Select an option

  • Save muhqu/1640662 to your computer and use it in GitHub Desktop.

Select an option

Save muhqu/1640662 to your computer and use it in GitHub Desktop.
function (head, req) {
var CACHED_COUNT=0;
var CACHED_REV=1;
var CURRENT_COUNT=2;
var row;
var lastRow = null;
function currAdd(curr, row) {
var k = row.key[0];
var t = row.key[1];
curr['key'] = k;
if(t == CACHED_REV) {
curr['rev'] = row.key[2];
}
else if (t == CACHED_COUNT) {
curr['old_value'] = row.key[2];
}
else if (t == CURRENT_COUNT) {
curr['new_value'] = row.value;
}
}
function iterate() {
var curr = {'key':null,'old_value':0,'new_value':0};
var found = false;
if (lastRow) {
currAdd(curr, lastRow);
found = true;
}
while (row = getRow()) {
if (lastRow && (row.key[0] != lastRow.key[0])) {
lastRow = row;
return curr;
}
currAdd(curr, row);
found = true;
lastRow = row;
}
lastRow = null;
return found ? curr : null;
}
provides("json", function() {
start({"headers": { "Content-Type": "text/plain"} });
var cnt=0;
while (row = iterate()) {
var v = {
_id: 'cached_likes_count:'+row.key,
value: row.new_value
};
if (row.rev) {
v._rev = row.rev;
}
if (row.old_value != row.new_value) {
send((cnt++?',' : '{"docs":[\
') + toJSON(v) + '\
');
}
}
return (cnt?'':'{"docs":[')+']}\
';
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment