Skip to content

Node.js based forum software built for the modern web

optimized user.delete

barisusakli4b80bbb9c485
diff --git a/src/database/level/sets.js b/src/database/level/sets.js
index 525477db7d..b7697f1209 100644
--- a/src/database/level/sets.js
+++ b/src/database/level/sets.js
@@ -29,6 +29,10 @@ module.exports = function(db, module) {
});
};
+ module.setsRemove = function(keys, value, callback) {
+ throw new Error('not-implemented');
+ };
+
module.isSetMember = function(key, value, callback) {
module.getListRange(key, 0, -1, function(err, set) {
callback(err, set.indexOf(value) !== -1);
diff --git a/src/database/mongo/sets.js b/src/database/mongo/sets.js
index b70b3a044a..a48b51cded 100644
--- a/src/database/mongo/sets.js
+++ b/src/database/mongo/sets.js
@@ -69,6 +69,27 @@ module.exports = function(db, module) {
db.collection('objects').update({_key: key}, {$pullAll: {members: value}}, callback);
};
+ module.setsRemove = function(keys, value, callback) {
+ callback = callback || helpers.noop;
+ if (!Array.isArray(keys) || !keys.length) {
+ return callback();
+ }
+ value = helpers.valueToString(value);
+
+ var bulk = db.collection('objects').initializeUnorderedBulkOp();
+
+ for(var i=0; i<keys.length; ++i) {
+ bulk.find({_key: keys[i]}).updateOne({$pull: {
+ members: value
+ }});
+ }
+
+ bulk.execute(function(err, res) {
+ callback(err);
+ });
+ };
+
+
module.isSetMember = function(key, value, callback) {
value = helpers.valueToString(value);
diff --git a/src/database/redis/sets.js b/src/database/redis/sets.js
index e3a4790862..9148dd34f8 100644
--- a/src/database/redis/sets.js
+++ b/src/database/redis/sets.js
@@ -23,6 +23,18 @@ module.exports = function(redisClient, module) {
redisClient.srem(key, value, callback);
};
+ module.setsRemove = function(keys, value, callback) {
+ callback = callback || function() {};
+
+ var multi = redisClient.multi();
+ for(var i=0; i<keys.length; ++i) {
+ multi.srem(keys[i], value);
+ }
+ multi.exec(function(err, res) {
+ callback(err);
+ });
+ };
+
module.isSetMember = function(key, value, callback) {
redisClient.sismember(key, value, function(err, result) {
if(err) {
diff --git a/src/user/delete.js b/src/user/delete.js
index ac800d0836..8c34fe6542 100644
--- a/src/user/delete.js
+++ b/src/user/delete.js
@@ -6,8 +6,8 @@ var async = require('async'),
user = require('../user'),
topics = require('../topics'),
groups = require('../groups'),
- plugins = require('../plugins');
-
+ plugins = require('../plugins'),
+ batch = require('../batch');
module.exports = function(User) {
@@ -34,13 +34,13 @@ module.exports = function(User) {
}
function deleteSortedSetElements(set, deleteMethod, callback) {
- db.getSortedSetRange(set, 0, -1, function(err, ids) {
+ batch.processSortedSet(set, function(err, ids, next) {
if (err) {
return callback(err);
}
- async.each(ids, deleteMethod, callback);
- });
+ async.eachLimit(ids, 10, deleteMethod, next);
+ }, {alwaysStartAt: 0}, callback);
}
User.deleteAccount = function(uid, callback) {
@@ -60,49 +60,18 @@ module.exports = function(User) {
db.deleteObjectField('email:uid', userData.email.toLowerCase(), next);
},
function(next) {
- db.delete('uid:' + uid + ':notifications:read', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':notifications:unread', next);
- },
- function(next) {
- db.sortedSetRemove('users:joindate', uid, next);
- },
- function(next) {
- db.sortedSetRemove('users:postcount', uid, next);
- },
- function(next) {
- db.sortedSetRemove('users:reputation', uid, next);
- },
- function(next) {
- db.delete('uid:' + uid + ':favourites', next);
- },
- function(next) {
- db.delete('user:' + uid + ':settings', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':topics', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':posts', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':chats', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':chats:unread', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':ip', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':upvote', next);
+ db.sortedSetsRemove(['users:joindate', 'users:postcount', 'users:reputation'], uid, next);
},
function(next) {
- db.delete('uid:' + uid + ':downvote', next);
- },
- function(next) {
- db.delete('uid:' + uid + ':ignored:cids', next);
+ var keys = [
+ 'uid:' + uid + ':notifications:read', 'uid:' + uid + ':notifications:unread',
+ 'uid:' + uid + ':favourites', 'user:' + uid + ':settings',
+ 'uid:' + uid + ':topics', 'uid:' + uid + ':posts',
+ 'uid:' + uid + ':chats', 'uid:' + uid + ':chats:unread',
+ 'uid:' + uid + ':ip', 'uid:' + uid + ':upvote', 'uid:' + uid + ':downvote',
+ 'uid:' + uid + ':ignored:cids'
+ ];
+ db.deleteAll(keys, next);
},
function(next) {
deleteUserFromFollowers(uid, next);
@@ -120,13 +89,7 @@ module.exports = function(User) {
async.parallel([
function(next) {
- db.delete('followers:' + uid, next);
- },
- function(next) {
- db.delete('following:' + uid, next);
- },
- function(next) {
- db.delete('user:' + uid, next);
+ db.deleteAll(['followers:' + uid, 'following:' + uid, 'user:' + uid], next);
},
function(next) {
db.decrObjectField('global', 'userCount', next);
@@ -142,9 +105,11 @@ module.exports = function(User) {
return callback(err);
}
- async.each(uids, function(theiruid, next) {
- db.setRemove('following:' + theiruid, uid, next);
- }, callback);
+ var sets = uids.map(function(uid) {
+ return 'following:' + uid;
+ });
+
+ db.setsRemove(sets, uid, callback);
});
}
};