diff --git a/src/categories.js b/src/categories.js index a768358b36..abb0d4defd 100644 --- a/src/categories.js +++ b/src/categories.js @@ -114,7 +114,7 @@ var db = require('./database'), var tids; async.waterfall([ function(next) { - Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tid' : 'categories:' + data.cid + ':tid', data.start, data.stop, next); + Categories.getTopicIds(data.targetUid ? 'cid:' + data.cid + ':uid:' + data.targetUid + ':tids' : 'cid:' + data.cid + ':tids', data.start, data.stop, next); }, function(topicIds, next) { tids = topicIds; @@ -169,7 +169,7 @@ var db = require('./database'), return callback(err); } - db.sortedSetRevRank('categories:' + cid + ':tid', tid, callback); + db.sortedSetRevRank('cid:' + cid + ':tids', tid, callback); }); }; @@ -467,7 +467,7 @@ var db = require('./database'), async.parallel([ function(next) { - db.sortedSetAdd('categories:recent_posts:cid:' + cid, postData.timestamp, postData.pid, next); + db.sortedSetAdd('cid:' + cid + ':pids', postData.timestamp, postData.pid, next); }, function(next) { db.incrObjectField('category:' + cid, 'post_count', next); @@ -476,7 +476,7 @@ var db = require('./database'), if (parseInt(topicData.pinned, 10) === 1) { next(); } else { - db.sortedSetAdd('categories:' + cid + ':tid', postData.timestamp, postData.tid, next); + db.sortedSetAdd('cid:' + cid + ':tids', postData.timestamp, postData.tid, next); } } ], callback); diff --git a/src/categories/activeusers.js b/src/categories/activeusers.js index 2cd10ef2b8..2debf62e4b 100644 --- a/src/categories/activeusers.js +++ b/src/categories/activeusers.js @@ -8,7 +8,7 @@ module.exports = function(Categories) { Categories.getActiveUsers = function(cid, callback) { async.waterfall([ function(next) { - db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, 24, next); + db.getSortedSetRevRange('cid:' + cid + ':pids', 0, 24, next); }, function(pids, next) { var keys = pids.map(function(pid) { diff --git a/src/categories/delete.js b/src/categories/delete.js index efc814bd27..98c485ceed 100644 --- a/src/categories/delete.js +++ b/src/categories/delete.js @@ -9,7 +9,7 @@ var async = require('async'), module.exports = function(Categories) { Categories.purge = function(cid, callback) { - batch.processSortedSet('categories:' + cid + ':tid', function(tids, next) { + batch.processSortedSet('cid:' + cid + ':tids', function(tids, next) { async.eachLimit(tids, 10, function(tid, next) { threadTools.purge(tid, 0, next); }, next); @@ -27,7 +27,11 @@ module.exports = function(Categories) { db.sortedSetRemove('categories:cid', cid, next); }, function(next) { - db.deleteAll(['categories:' + cid + ':tid', 'categories:recent_posts:cid:' + cid, 'category:' + cid], next); + db.deleteAll([ + 'cid:' + cid + ':tids', + 'cid:' + cid + ':pids', + 'category:' + cid + ], next); } ], callback); } diff --git a/src/categories/recentreplies.js b/src/categories/recentreplies.js index 7d9cbcc047..ea500f96f9 100644 --- a/src/categories/recentreplies.js +++ b/src/categories/recentreplies.js @@ -15,7 +15,7 @@ module.exports = function(Categories) { return callback(null, []); } - db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, count - 1, function(err, pids) { + db.getSortedSetRevRange('cid:' + cid + ':pids', 0, count - 1, function(err, pids) { if (err || !pids || !pids.length) { return callback(err, []); } @@ -64,7 +64,7 @@ module.exports = function(Categories) { return callback(null, []); } - db.getSortedSetRevRange('categories:recent_posts:cid:' + category.cid, 0, 0, function(err, pids) { + db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, function(err, pids) { if (err || !Array.isArray(pids) || !pids.length) { return callback(err, []); } @@ -75,10 +75,10 @@ module.exports = function(Categories) { async.parallel({ pinnedTids: function(next) { - db.getSortedSetRevRangeByScore('categories:' + category.cid + ':tid', 0, -1, Infinity, Date.now(), next); + db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, Infinity, Date.now(), next); }, tids: function(next) { - db.getSortedSetRevRangeByScore('categories:' + category.cid + ':tid', 0, Math.max(0, count), Date.now(), 0, next); + db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(0, count), Date.now(), 0, next); } }, function(err, results) { if (err) { @@ -140,10 +140,10 @@ module.exports = function(Categories) { async.parallel([ function(next) { - db.sortedSetRemove('categories:recent_posts:cid:' + oldCid, movePids, next); + db.sortedSetRemove('cid:' + oldCid + ':pids', movePids, next); }, function(next) { - db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamps, movePids, next); + db.sortedSetAdd('cid:' + cid + ':pids', timestamps, movePids, next); } ], function(err) { if (err) { diff --git a/src/posts/delete.js b/src/posts/delete.js index e48dda467f..40e827c574 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -87,7 +87,7 @@ module.exports = function(Posts) { return callback(err); } - db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid, callback); + db.sortedSetRemove('cid:' + cid + ':pids', pid, callback); }); } @@ -97,7 +97,7 @@ module.exports = function(Posts) { return callback(err); } - db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid, callback); + db.sortedSetAdd('cid:' + cid + ':pids', timestamp, pid, callback); }); } @@ -180,7 +180,7 @@ module.exports = function(Posts) { } var sets = cids.map(function(cid) { - return 'categories:recent_posts:cid:' + cid; + return 'cid:' + cid + ':pids'; }); db.sortedSetsRemove(sets, pid, callback); diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 960878a8ea..2448f032d1 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -383,7 +383,7 @@ SocketTopics.moveAll = function(socket, data, callback) { return callback(err || new Error('[[error:no-privileges]]')); } - categories.getTopicIds('categories:' + data.currentCid + ':tid', 0, -1, function(err, tids) { + categories.getTopicIds('cid:' + data.currentCid + ':tids', 0, -1, function(err, tids) { if (err) { return callback(err); } diff --git a/src/threadTools.js b/src/threadTools.js index 1d192f7090..d1924bbfe2 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -163,7 +163,7 @@ var winston = require('winston'), topics.setTopicField(tid, 'pinned', pin ? 1 : 0); topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) { - db.sortedSetAdd('categories:' + topicData.cid + ':tid', pin ? Math.pow(2, 53) : topicData.lastposttime, tid); + db.sortedSetAdd('cid:' + topicData.cid + ':tids', pin ? Math.pow(2, 53) : topicData.lastposttime, tid); }); plugins.fireHook('action:topic.pin', { @@ -192,11 +192,11 @@ var winston = require('winston'), }, function(topicData, next) { topic = topicData; - db.sortedSetRemove('categories:' + topicData.cid + ':tid', tid, next); + db.sortedSetRemove('cid:' + topicData.cid + ':tids', tid, next); }, function(next) { var timestamp = parseInt(topic.pinned, 10) ? Math.pow(2, 53) : topic.lastposttime; - db.sortedSetAdd('categories:' + cid + ':tid', timestamp, tid, next); + db.sortedSetAdd('cid:' + cid + ':tids', timestamp, tid, next); } ], function(err) { if (err) { diff --git a/src/topics/create.js b/src/topics/create.js index e9699ab858..56c1f5dd8f 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -62,7 +62,11 @@ module.exports = function(Topics) { async.parallel([ function(next) { - db.sortedSetsAdd(['topics:tid', 'categories:' + cid + ':tid', 'cid:' + cid + ':uid:' + uid + ':tid'], timestamp, tid, next); + db.sortedSetsAdd([ + 'topics:tid', + 'cid:' + cid + ':tids', + 'cid:' + cid + ':uid:' + uid + ':tids' + ], timestamp, tid, next); }, function(next) { user.addTopicIdToUser(uid, tid, timestamp, next); diff --git a/src/topics/delete.js b/src/topics/delete.js index 8daec210be..630e4eddc7 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -77,7 +77,11 @@ module.exports = function(Topics) { return callback(err); } - db.sortedSetsRemove(['categories:' + topicData.cid + ':tid', 'uid:' + topicData.uid + ':topics'], tid, callback); + db.sortedSetsRemove([ + 'cid:' + topicData.cid + ':tids', + 'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids', + 'uid:' + topicData.uid + ':topics' + ], tid, callback); }); } diff --git a/src/upgrade.js b/src/upgrade.js index 15897e8898..3f4c1f7e39 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -19,7 +19,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2014, 10, 6, 18, 30); + latestSchema = Date.UTC(2014, 10, 7); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -1170,7 +1170,7 @@ Upgrade.upgrade = function(callback) { function(next) { thisSchemaDate = Date.UTC(2014, 10, 6, 18, 30); if (schemaDate < thisSchemaDate) { - winston.info('[2014/10/31] Updating topic authorship sorted set'); + winston.info('[2014/11/6] Updating topic authorship sorted set'); async.waterfall([ async.apply(db.getObjectField, 'global', 'nextTid'), @@ -1201,18 +1201,79 @@ Upgrade.upgrade = function(callback) { } ], function(err) { if (err) { - winston.error('[2014/10/31] Error encountered while Updating topic authorship sorted set'); + winston.error('[2014/11/6] Error encountered while Updating topic authorship sorted set'); return next(err); } - winston.info('[2014/10/31] Updating topic authorship sorted set done'); + winston.info('[2014/11/6] Updating topic authorship sorted set done'); Upgrade.update(thisSchemaDate, next); }); } else { - winston.info('[2014/10/31] Updating topic authorship sorted set skipped'); + winston.info('[2014/11/6] Updating topic authorship sorted set skipped'); + next(); + } + }, + function(next) { + thisSchemaDate = Date.UTC(2014, 10, 7); + if (schemaDate < thisSchemaDate) { + winston.info('[2014/11/7] Renaming sorted set names'); + + async.waterfall([ + function(next) { + async.parallel({ + cids: function(next) { + db.getSortedSetRange('categories:cid', 0, -1, next); + }, + uids: function(next) { + db.getSortedSetRange('users:joindate', 0, -1, next); + } + }, next); + }, + function(results, next) { + async.eachLimit(results.cids, 50, function(cid, next) { + async.parallel([ + function(next) { + db.exists('categories:' + cid + ':tid', function(err, exists) { + if (err || !exists) { + return next(err); + } + db.rename('categories:' + cid + ':tid', 'cid:' + cid + ':tids', next); + }); + }, + function(next) { + db.exists('categories:recent_posts:cid:' + cid, function(err, exists) { + if (err || !exists) { + return next(err); + } + db.rename('categories:recent_posts:cid:' + cid, 'cid:' + cid + ':pids', next); + }); + }, + function(next) { + async.eachLimit(results.uids, 50, function(uid, next) { + db.exists('cid:' + cid + ':uid:' + uid + ':tid', function(err, exists) { + if (err || !exists) { + return next(err); + } + db.rename('cid:' + cid + ':uid:' + uid + ':tid', 'cid:' + cid + ':uid:' + uid + ':tids', next); + }); + }, next); + } + ], next); + }, next); + } + ], function(err) { + if (err) { + winston.error('[2014/11/7] Error encountered while renaming sorted sets'); + return next(err); + } + winston.info('[2014/11/7] Renaming sorted sets done'); + Upgrade.update(thisSchemaDate, next); + }); + } else { + winston.info('[2014/11/7] Renaming sorted sets skipped'); next(); } } - // Meta.configs.setOnEmpty = function (field, value, callback) { + // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!! ], function(err) {