mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: cleanup, use bulk
This commit is contained in:
@@ -209,10 +209,14 @@ module.exports = function (Categories) {
|
|||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next);
|
const bulk = [
|
||||||
},
|
['cid:' + cid + ':pids', postData.timestamp, postData.pid],
|
||||||
function (next) {
|
['cid:' + cid + ':tids:lastposttime', postData.timestamp, postData.tid],
|
||||||
db.sortedSetAdd('cid:' + cid + ':tids:lastposttime', postData.timestamp, postData.tid, next);
|
];
|
||||||
|
if (!pinned) {
|
||||||
|
bulk.push(['cid:' + cid + ':tids', postData.timestamp, postData.tid]);
|
||||||
|
}
|
||||||
|
db.sortedSetAddBulk(bulk, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
db.incrObjectField('category:' + cid, 'post_count', next);
|
db.incrObjectField('category:' + cid, 'post_count', next);
|
||||||
@@ -221,17 +225,7 @@ module.exports = function (Categories) {
|
|||||||
if (pinned) {
|
if (pinned) {
|
||||||
return setImmediate(next);
|
return setImmediate(next);
|
||||||
}
|
}
|
||||||
|
db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, err => next(err));
|
||||||
async.parallel([
|
|
||||||
function (next) {
|
|
||||||
db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
db.sortedSetIncrBy('cid:' + cid + ':tids:posts', 1, postData.tid, next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
next(err);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
Categories.updateRecentTid(cid, postData.tid, next);
|
Categories.updateRecentTid(cid, postData.tid, next);
|
||||||
|
|||||||
@@ -150,22 +150,12 @@ module.exports = function (Categories) {
|
|||||||
Categories.getCategoryField(cid, 'parentCid', next);
|
Categories.getCategoryField(cid, 'parentCid', next);
|
||||||
},
|
},
|
||||||
function (parentCid, next) {
|
function (parentCid, next) {
|
||||||
async.parallel([
|
db.sortedSetsAdd(['categories:cid', 'cid:' + parentCid + ':children'], order, cid, function (err) {
|
||||||
function (next) {
|
|
||||||
db.sortedSetAdd('categories:cid', order, cid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
|
cache.del(['categories:cid', 'cid:' + parentCid + ':children']);
|
||||||
next();
|
next(err);
|
||||||
},
|
|
||||||
], next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
callback(err);
|
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
], err => callback(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
Categories.parseDescription = function (cid, description, callback) {
|
Categories.parseDescription = function (cid, description, callback) {
|
||||||
|
|||||||
@@ -18,15 +18,10 @@ module.exports = function (db, module) {
|
|||||||
value = helpers.valueToString(value);
|
value = helpers.valueToString(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(key) && Array.isArray(value)) {
|
db.collection('objects').deleteMany({
|
||||||
db.collection('objects').deleteMany({ _key: { $in: key }, value: { $in: value } }, done);
|
_key: Array.isArray(key) ? { $in: key } : key,
|
||||||
} else if (Array.isArray(value)) {
|
value: Array.isArray(value) ? { $in: value } : value,
|
||||||
db.collection('objects').deleteMany({ _key: key, value: { $in: value } }, done);
|
}, done);
|
||||||
} else if (Array.isArray(key)) {
|
|
||||||
db.collection('objects').deleteMany({ _key: { $in: key }, value: value }, done);
|
|
||||||
} else {
|
|
||||||
db.collection('objects').deleteOne({ _key: key, value: value }, done);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.sortedSetsRemove = function (keys, value, callback) {
|
module.sortedSetsRemove = function (keys, value, callback) {
|
||||||
|
|||||||
@@ -88,8 +88,7 @@ module.exports = function (Groups) {
|
|||||||
function updateVisibility(groupName, hidden, callback) {
|
function updateVisibility(groupName, hidden, callback) {
|
||||||
if (hidden) {
|
if (hidden) {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
async.apply(db.sortedSetRemove, 'groups:visible:createtime', groupName),
|
async.apply(db.sortedSetsRemove, ['groups:visible:createtime', 'groups:visible:memberCount'], groupName),
|
||||||
async.apply(db.sortedSetRemove, 'groups:visible:memberCount', groupName),
|
|
||||||
async.apply(db.sortedSetRemove, 'groups:visible:name', groupName.toLowerCase() + ':' + groupName),
|
async.apply(db.sortedSetRemove, 'groups:visible:name', groupName.toLowerCase() + ':' + groupName),
|
||||||
], callback);
|
], callback);
|
||||||
} else {
|
} else {
|
||||||
@@ -98,10 +97,10 @@ module.exports = function (Groups) {
|
|||||||
db.getObjectFields('group:' + groupName, ['createtime', 'memberCount'], next);
|
db.getObjectFields('group:' + groupName, ['createtime', 'memberCount'], next);
|
||||||
},
|
},
|
||||||
function (groupData, next) {
|
function (groupData, next) {
|
||||||
async.parallel([
|
db.sortedSetAddBulk([
|
||||||
async.apply(db.sortedSetAdd, 'groups:visible:createtime', groupData.createtime, groupName),
|
['groups:visible:createtime', groupData.createtime, groupName],
|
||||||
async.apply(db.sortedSetAdd, 'groups:visible:memberCount', groupData.memberCount, groupName),
|
['groups:visible:memberCount', groupData.memberCount, groupName],
|
||||||
async.apply(db.sortedSetAdd, 'groups:visible:name', 0, groupName.toLowerCase() + ':' + groupName),
|
['groups:visible:name', 0, groupName.toLowerCase() + ':' + groupName],
|
||||||
], next);
|
], next);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
|
|||||||
@@ -527,9 +527,7 @@ function enableDefaultPlugins(next) {
|
|||||||
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
|
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
|
||||||
|
|
||||||
var db = require('./database');
|
var db = require('./database');
|
||||||
var order = defaultEnabled.map(function (plugin, index) {
|
var order = defaultEnabled.map((plugin, index) => index);
|
||||||
return index;
|
|
||||||
});
|
|
||||||
db.sortedSetAdd('plugins:active', order, defaultEnabled, next);
|
db.sortedSetAdd('plugins:active', order, defaultEnabled, next);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -124,10 +124,8 @@ module.exports = function (Messaging) {
|
|||||||
if (!inRoom) {
|
if (!inRoom) {
|
||||||
return next(new Error('[[error:cant-add-users-to-chat-room]]'));
|
return next(new Error('[[error:cant-add-users-to-chat-room]]'));
|
||||||
}
|
}
|
||||||
var now = Date.now();
|
const now = Date.now();
|
||||||
var timestamps = uids.map(function () {
|
const timestamps = uids.map(() => now);
|
||||||
return now;
|
|
||||||
});
|
|
||||||
db.sortedSetAdd('chat:room:' + roomId + ':uids', timestamps, uids, next);
|
db.sortedSetAdd('chat:room:' + roomId + ':uids', timestamps, uids, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
|
|||||||
@@ -108,10 +108,7 @@ Notifications.filterExists = function (nids, callback) {
|
|||||||
db.isSortedSetMembers('notifications', nids, next);
|
db.isSortedSetMembers('notifications', nids, next);
|
||||||
},
|
},
|
||||||
function (exists, next) {
|
function (exists, next) {
|
||||||
nids = nids.filter(function (notifId, idx) {
|
nids = nids.filter((notifId, idx) => exists[idx]);
|
||||||
return exists[idx];
|
|
||||||
});
|
|
||||||
|
|
||||||
next(null, nids);
|
next(null, nids);
|
||||||
},
|
},
|
||||||
], callback);
|
], callback);
|
||||||
@@ -397,52 +394,32 @@ Notifications.markReadMultiple = function (nids, uid, callback) {
|
|||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
var notificationKeys = nids.map(function (nid) {
|
let notificationKeys = nids.map(nid => 'notifications:' + nid);
|
||||||
return 'notifications:' + nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(db.getObjectsFields, notificationKeys, ['mergeId']),
|
async.apply(db.getObjectsFields, notificationKeys, ['mergeId']),
|
||||||
function (mergeIds, next) {
|
function (mergeIds, next) {
|
||||||
// Isolate mergeIds and find related notifications
|
// Isolate mergeIds and find related notifications
|
||||||
mergeIds = mergeIds.map(function (set) {
|
mergeIds = _.uniq(mergeIds.map(set => set.mergeId));
|
||||||
return set.mergeId;
|
|
||||||
}).reduce(function (memo, mergeId, idx, arr) {
|
|
||||||
if (mergeId && idx === arr.indexOf(mergeId)) {
|
|
||||||
memo.push(mergeId);
|
|
||||||
}
|
|
||||||
return memo;
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
Notifications.findRelated(mergeIds, 'uid:' + uid + ':notifications:unread', next);
|
Notifications.findRelated(mergeIds, 'uid:' + uid + ':notifications:unread', next);
|
||||||
},
|
},
|
||||||
function (relatedNids, next) {
|
function (relatedNids, next) {
|
||||||
notificationKeys = _.union(nids, relatedNids).map(function (nid) {
|
notificationKeys = _.union(nids, relatedNids).map(nid => 'notifications:' + nid);
|
||||||
return 'notifications:' + nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
db.getObjectsFields(notificationKeys, ['nid', 'datetime'], next);
|
db.getObjectsFields(notificationKeys, ['nid', 'datetime'], next);
|
||||||
},
|
},
|
||||||
function (notificationData, next) {
|
function (notificationData, next) {
|
||||||
// Filter out notifications that didn't exist
|
notificationData = notificationData.filter(n => n && n.nid);
|
||||||
notificationData = notificationData.filter(function (notification) {
|
|
||||||
return notification && notification.nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Extract nid
|
nids = notificationData.map(n => n.nid);
|
||||||
nids = notificationData.map(function (notification) {
|
|
||||||
return notification.nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
var datetimes = notificationData.map(function (notification) {
|
|
||||||
return (notification && notification.datetime) || Date.now();
|
|
||||||
});
|
|
||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nids, next);
|
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nids, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
|
const datetimes = notificationData.map(n => (n && n.datetime) || Date.now());
|
||||||
db.sortedSetAdd('uid:' + uid + ':notifications:read', datetimes, nids, next);
|
db.sortedSetAdd('uid:' + uid + ':notifications:read', datetimes, nids, next);
|
||||||
},
|
},
|
||||||
], next);
|
], next);
|
||||||
@@ -465,12 +442,11 @@ Notifications.markAllRead = function (uid, callback) {
|
|||||||
|
|
||||||
Notifications.prune = function (callback) {
|
Notifications.prune = function (callback) {
|
||||||
callback = callback || function () {};
|
callback = callback || function () {};
|
||||||
var week = 604800000;
|
|
||||||
|
|
||||||
var cutoffTime = Date.now() - week;
|
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
|
const week = 604800000;
|
||||||
|
const cutoffTime = Date.now() - week;
|
||||||
db.getSortedSetRangeByScore('notifications', 0, 500, '-inf', cutoffTime, next);
|
db.getSortedSetRangeByScore('notifications', 0, 500, '-inf', cutoffTime, next);
|
||||||
},
|
},
|
||||||
function (nids, next) {
|
function (nids, next) {
|
||||||
@@ -478,15 +454,12 @@ Notifications.prune = function (callback) {
|
|||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
var keys = nids.map(function (nid) {
|
|
||||||
return 'notifications:' + nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetRemove('notifications', nids, next);
|
db.sortedSetRemove('notifications', nids, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
|
const keys = nids.map(nid => 'notifications:' + nid);
|
||||||
db.deleteAll(keys, next);
|
db.deleteAll(keys, next);
|
||||||
},
|
},
|
||||||
], next);
|
], next);
|
||||||
|
|||||||
@@ -68,13 +68,11 @@ module.exports = function (Topics) {
|
|||||||
Topics.updateRecent(tid, topicData.lastposttime, next);
|
Topics.updateRecent(tid, topicData.lastposttime, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetAdd('topics:posts', topicData.postcount, tid, next);
|
db.sortedSetAddBulk([
|
||||||
},
|
['topics:posts', topicData.postcount, tid],
|
||||||
function (next) {
|
['topics:views', topicData.viewcount, tid],
|
||||||
db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);
|
['topics:votes', parseInt(topicData.votes, 10) || 0, tid],
|
||||||
},
|
], next);
|
||||||
function (next) {
|
|
||||||
db.sortedSetAdd('topics:votes', parseInt(topicData.votes, 10) || 0, tid, next);
|
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
|
|||||||
@@ -188,15 +188,11 @@ module.exports = function (Topics) {
|
|||||||
} else {
|
} else {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
async.apply(db.sortedSetRemove, 'cid:' + topicData.cid + ':tids:pinned', tid),
|
async.apply(db.sortedSetRemove, 'cid:' + topicData.cid + ':tids:pinned', tid),
|
||||||
async.apply(db.sortedSetsAdd, [
|
async.apply(db.sortedSetAddBulk, [
|
||||||
'cid:' + topicData.cid + ':tids',
|
['cid:' + topicData.cid + ':tids', topicData.lastposttime, tid],
|
||||||
'cid:' + topicData.cid + ':tids:posts',
|
['cid:' + topicData.cid + ':tids:posts', topicData.postcount, tid],
|
||||||
'cid:' + topicData.cid + ':tids:votes',
|
['cid:' + topicData.cid + ':tids:votes', parseInt(topicData.votes, 10) || 0, tid],
|
||||||
], [
|
]),
|
||||||
topicData.lastposttime,
|
|
||||||
topicData.postcount,
|
|
||||||
parseInt(topicData.votes, 10) || 0,
|
|
||||||
], tid),
|
|
||||||
], next);
|
], next);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -14,18 +14,13 @@ module.exports = function (User) {
|
|||||||
return setImmediate(callback);
|
return setImmediate(callback);
|
||||||
}
|
}
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
async.waterfall([
|
const bulk = [
|
||||||
function (next) {
|
['uid:' + uid + ':ip', now, ip || 'Unknown'],
|
||||||
db.sortedSetAdd('uid:' + uid + ':ip', now, ip || 'Unknown', next);
|
];
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
if (ip) {
|
if (ip) {
|
||||||
db.sortedSetAdd('ip:' + ip + ':uid', now, uid, next);
|
bulk.push(['ip:' + ip + ':uid', now, uid]);
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
},
|
db.sortedSetAddBulk(bulk, callback);
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
User.getIPs = function (uid, stop, callback) {
|
User.getIPs = function (uid, stop, callback) {
|
||||||
|
|||||||
@@ -40,8 +40,10 @@ module.exports = function (User) {
|
|||||||
}
|
}
|
||||||
var now = Date.now();
|
var now = Date.now();
|
||||||
async.parallel([
|
async.parallel([
|
||||||
async.apply(db.sortedSetAdd, 'following:' + uid, now, theiruid),
|
async.apply(db.sortedSetAddBulk, [
|
||||||
async.apply(db.sortedSetAdd, 'followers:' + theiruid, now, uid),
|
['following:' + uid, now, theiruid],
|
||||||
|
['followers:' + theiruid, now, uid],
|
||||||
|
]),
|
||||||
async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1),
|
async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1),
|
||||||
async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1),
|
async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1),
|
||||||
], next);
|
], next);
|
||||||
|
|||||||
@@ -206,13 +206,12 @@ module.exports = function (User) {
|
|||||||
function (next) {
|
function (next) {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function (next) {
|
function (next) {
|
||||||
db.sortedSetAdd('email:uid', uid, newEmail.toLowerCase(), next);
|
db.sortedSetAddBulk([
|
||||||
},
|
['email:uid', uid, newEmail.toLowerCase()],
|
||||||
function (next) {
|
['email:sorted', 0, newEmail.toLowerCase() + ':' + uid],
|
||||||
db.sortedSetAdd('email:sorted', 0, newEmail.toLowerCase() + ':' + uid, next);
|
['user:' + uid + ':emails', Date.now(), newEmail + ':' + Date.now()],
|
||||||
},
|
['users:notvalidated', Date.now(), uid],
|
||||||
function (next) {
|
], next);
|
||||||
db.sortedSetAdd('user:' + uid + ':emails', Date.now(), newEmail + ':' + Date.now(), next);
|
|
||||||
},
|
},
|
||||||
function (next) {
|
function (next) {
|
||||||
User.setUserField(uid, 'email', newEmail, next);
|
User.setUserField(uid, 'email', newEmail, next);
|
||||||
@@ -227,9 +226,6 @@ module.exports = function (User) {
|
|||||||
}
|
}
|
||||||
User.setUserField(uid, 'email:confirmed', 0, next);
|
User.setUserField(uid, 'email:confirmed', 0, next);
|
||||||
},
|
},
|
||||||
function (next) {
|
|
||||||
db.sortedSetAdd('users:notvalidated', Date.now(), uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
function (next) {
|
||||||
User.reset.cleanByUid(uid, next);
|
User.reset.cleanByUid(uid, next);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user