sortedSetsRemove

instead of calling sortedSetRemove() one at a time for each id remove
value from multiple sortedSets with one db call
v1.18.x
barisusakli 11 years ago
parent a4d4e53f45
commit fd12ec763a

@ -27,6 +27,11 @@ module.exports = function(db, module) {
}); });
}; };
module.sortedSetsRemove = function(keys, value, callback) {
// TODO :
// remove value from sorted sets given by keys
};
function flattenSortedSet(set, callback) { function flattenSortedSet(set, callback) {
callback(null, !set.length ? [] : set.reduce(function(a, b) { callback(null, !set.length ? [] : set.reduce(function(a, b) {
return (a.length ? a : [a.value]).concat([b.value]); return (a.length ? a : [a.value]).concat([b.value]);

@ -19,6 +19,12 @@ module.exports = function(db, module) {
db.collection('objects').remove({_key: key, value: value}, helpers.done(callback)); db.collection('objects').remove({_key: key, value: value}, helpers.done(callback));
}; };
module.sortedSetsRemove = function(keys, value, callback) {
value = helpers.valueToString(value);
db.collection('objects').remove({_key: {$in: keys}, value: value}, helpers.done(callback));
};
function getSortedSetRange(key, start, stop, sort, withScores, callback) { function getSortedSetRange(key, start, stop, sort, withScores, callback) {
db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}}) db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}})
.limit(stop - start + 1) .limit(stop - start + 1)

@ -9,6 +9,16 @@ module.exports = function(redisClient, module) {
redisClient.zrem(key, value, callback); redisClient.zrem(key, value, callback);
}; };
module.sortedSetsRemove = function(keys, value, callback) {
var multi = redisClient.multi();
for(var x=0; x<keys.length; ++x) {
multi.zrem(keys[x], value);
}
multi.exec(callback);
};
module.getSortedSetRange = function(key, start, stop, callback) { module.getSortedSetRange = function(key, start, stop, callback) {
redisClient.zrange(key, start, stop, callback); redisClient.zrange(key, start, stop, callback);
}; };

@ -86,9 +86,11 @@ module.exports = function(Posts) {
return callback(err); return callback(err);
} }
async.each(cids, function(cid, next) { var sets = cids.map(function(cid) {
db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid, next); return 'categories:recent_posts:cid:' + cid;
}, callback); });
db.sortedSetsRemove(sets, pid, callback);
}); });
} }
@ -98,9 +100,11 @@ module.exports = function(Posts) {
return callback(err); return callback(err);
} }
async.each(uids, function(uid, next) { var sets = uids.map(function(uid) {
db.sortedSetRemove('uid:' + uid + ':favourites', pid, next); return 'uid:' + uid + ':favourites'
}, function(err) { });
db.sortedSetsRemove(sets, pid, function(err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -123,16 +127,20 @@ module.exports = function(Posts) {
return callback(err); return callback(err);
} }
var upvoterSets = results.upvoters.map(function(uid) {
return 'uid:' + uid + ':upvote';
});
var downvoterSets = results.downvoters.map(function(uid) {
return 'uid:' + uid + ':downvote';
});
async.parallel([ async.parallel([
function(next) { function(next) {
async.each(results.upvoters, function(uid, next) { db.sortedSetsRemove(upvoterSets, pid, next);
db.sortedSetRemove('uid:' + uid + ':upvote', pid, next);
}, next);
}, },
function(next) { function(next) {
async.each(results.downvoters, function(uid, next) { db.sortedSetsRemove(downvoterSets, pid, next);
db.sortedSetRemove('uid:' + uid + ':downvote', pid, next);
}, next);
}, },
function(next) { function(next) {
db.delete('pid:' + pid + ':upvote', next); db.delete('pid:' + pid + ':upvote', next);

Loading…
Cancel
Save