From 7c00bce63c9606e18f3c7ec023d16c1c0d7cc545 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 16 Apr 2014 18:07:41 -0400 Subject: [PATCH] faster checkAndReplace still need a better solution for this uniqueId business but this is better than before. --- src/notifications.js | 72 ++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/src/notifications.js b/src/notifications.js index 7524816d16..e6a6c4baf9 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -135,52 +135,40 @@ var async = require('async'), function checkReplace(uniqueId, uid, newNotifObj, callback) { var replace = false, matched = false; - async.parallel([ - function(next) { - db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { - if (nids && nids.length > 0) { - async.each(nids, function(nid, next) { - Notifications.get(nid, uid, function(nid_info) { - if (nid_info && nid_info.uniqueId === uniqueId) { - matched = true; - if ((nid_info.importance || 5) >= newNotifObj.importance) { - replace = true; - db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid); - } - } - - next(); - }); - }, function(err) { - next(); - }); - } else { - next(); + function checkAndRemove(set, next) { + db.getSortedSetRange(set, 0, -1, function(err, nids) { + if (err || !nids || !nids.length) { + return next(err); + } + + var keys = nids.map(function(nid) { + return 'notifications:' + nid; + }); + + db.getObjectsFields(keys, ['uniqueId', 'importance'], function(err, nid_infos) { + if (err) { + return next(err); } + + nid_infos.forEach(function(nid_info) { + if (nid_info && nid_info.uniqueId === uniqueId) { + matched = true; + if ((nid_info.importance || 5) >= newNotifObj.importance) { + replace = true; + db.sortedSetRemove(set, nid_info.nid); + } + } + }); }); + }); + } + + async.parallel([ + function(next) { + checkAndRemove('uid:' + uid + ':notifications:unread', next); }, function(next) { - db.getSortedSetRange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) { - if (nids && nids.length > 0) { - async.each(nids, function(nid, next) { - Notifications.get(nid, uid, function(nid_info) { - if (nid_info && nid_info.uniqueId === uniqueId) { - matched = true; - if ((nid_info.importance || 5) >= newNotifObj.importance) { - replace = true; - db.sortedSetRemove('uid:' + uid + ':notifications:read', nid); - } - } - - next(); - }); - }, function(err) { - next(); - }); - } else { - next(); - } - }); + checkAndRemove('uid:' + uid + ':notifcations:read', next); } ], function(err) { if (!err) {