From 48538b28bea22f097a8f2c0ab171bf7e0f2f687e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 31 May 2019 21:44:53 -0400 Subject: [PATCH] feat: optimize group rename removed async.each --- src/groups/update.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/groups/update.js b/src/groups/update.js index 11cd0b81d6..5075292a79 100644 --- a/src/groups/update.js +++ b/src/groups/update.js @@ -225,9 +225,8 @@ module.exports = function (Groups) { if (err) { return next(err); } - async.each(groups, function (group, next) { - renameGroupMember('group:' + group + ':members', oldName, newName, next); - }, next); + const keys = groups.map(group => 'group:' + group + ':members'); + renameGroupsMember(keys, oldName, newName, next); }); }, async.apply(db.rename, 'group:' + oldName, 'group:' + newName), @@ -237,10 +236,8 @@ module.exports = function (Groups) { async.apply(db.rename, 'group:' + oldName + ':invited', 'group:' + newName + ':invited'), async.apply(db.rename, 'group:' + oldName + ':member:pids', 'group:' + newName + ':member:pids'), - async.apply(renameGroupMember, 'groups:createtime', oldName, newName), - async.apply(renameGroupMember, 'groups:visible:createtime', oldName, newName), - async.apply(renameGroupMember, 'groups:visible:memberCount', oldName, newName), - async.apply(renameGroupMember, 'groups:visible:name', oldName.toLowerCase() + ':' + oldName, newName.toLowerCase() + ':' + newName), + async.apply(renameGroupsMember, ['groups:createtime', 'groups:visible:createtime', 'groups:visible:memberCount'], oldName, newName), + async.apply(renameGroupsMember, ['groups:visible:name'], oldName.toLowerCase() + ':' + oldName, newName.toLowerCase() + ':' + newName), function (next) { plugins.fireHook('action:group.rename', { old: oldName, @@ -274,25 +271,25 @@ module.exports = function (Groups) { }, callback); } - function renameGroupMember(group, oldName, newName, callback) { - var score; + function renameGroupsMember(keys, oldName, newName, callback) { + var scores; async.waterfall([ function (next) { - db.isSortedSetMember(group, oldName, next); + db.isMemberOfSortedSets(keys, oldName, next); }, - function (isMember, next) { - if (!isMember) { + function (isMembers, next) { + keys = keys.filter((key, index) => isMembers[index]); + if (!keys.length) { return callback(); } - - db.sortedSetScore(group, oldName, next); + db.sortedSetsScore(keys, oldName, next); }, - function (_score, next) { - score = _score; - db.sortedSetRemove(group, oldName, next); + function (_scores, next) { + scores = _scores; + db.sortedSetsRemove(keys, oldName, next); }, function (next) { - db.sortedSetAdd(group, score, newName, next); + db.sortedSetsAdd(keys, scores, newName, next); }, ], callback); }