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) {
callback(null, !set.length ? [] : set.reduce(function(a, b) {
return (a.length ? a : [a.value]).concat([b.value]);

@ -10,13 +10,19 @@ module.exports = function(db, module) {
value: value
};
db.collection('objects').update({_key:key, value:value}, {$set:data}, {upsert:true, w: 1}, helpers.done(callback));
db.collection('objects').update({_key: key, value: value}, {$set: data}, {upsert:true, w: 1}, helpers.done(callback));
};
module.sortedSetRemove = function(key, value, callback) {
value = helpers.valueToString(value);
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) {

@ -9,6 +9,16 @@ module.exports = function(redisClient, module) {
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) {
redisClient.zrange(key, start, stop, callback);
};

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

Loading…
Cancel
Save