diff --git a/src/controllers/tags.js b/src/controllers/tags.js index 9c03cba0af..a8586bd768 100644 --- a/src/controllers/tags.js +++ b/src/controllers/tags.js @@ -27,28 +27,12 @@ tagsController.getTag = function(req, res, next) { }; tagsController.getTags = function(req, res, next) { - topics.getTagsObjects(function(err, tags) { + topics.getTags(0, -1, function(err, tags) { if (err) { return next(err); } - async.map(tags, function(tag, next) { - topics.getTagTopicCount(tag.name, function(err, count) { - if (err) { - return next(err); - } - tag.topicCount = count; - next(null, tag); - }); - }, function(err, tags) { - if (err) { - return next(err); - } - tags = tags.sort(function(a, b) { - return parseInt(b.topicCount, 10) - parseInt(a.topicCount, 10); - }); - res.render('tags', {tags: tags}); - }); + res.render('tags', {tags: tags}); }); }; diff --git a/src/database/level/sorted.js b/src/database/level/sorted.js index b64245c89b..c552842d31 100644 --- a/src/database/level/sorted.js +++ b/src/database/level/sorted.js @@ -5,7 +5,7 @@ var async = require('async'); module.exports = function(db, module) { var helpers = module.helpers.level; - + module.sortedSetAdd = function(key, score, value, callback) { module.getListRange(key, 0, -1, function(err, set) { set = set.filter(function(a) {return a.value !== value.toString();}); @@ -57,6 +57,10 @@ module.exports = function(db, module) { }); }; + module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) { + // should return [{value:"test", score: 2}, {value: "foo", score: 1}, ...] + }; + module.getSortedSetRangeByScore = function(key, start, count, min, max, callback) { module.getListRange(key, 0, -1, function(err, list) { if (min && max) { diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index e0eee69806..e0c2e2cb9b 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -19,8 +19,8 @@ module.exports = function(db, module) { db.collection('objects').remove({_key:key, value:value}, helpers.done(callback)); }; - function getSortedSetRange(key, start, stop, sort, callback) { - db.collection('objects').find({_key:key}, {fields:{value:1}}) + function getSortedSetRange(key, start, stop, sort, withScores, callback) { + db.collection('objects').find({_key:key}, {fields: {_id: 0, value: 1, score: 1}}) .limit(stop - start + 1) .skip(start) .sort({score: sort}) @@ -29,20 +29,26 @@ module.exports = function(db, module) { return callback(err, null); } - data = data.map(function(item) { - return item.value; - }); + if (!withScores) { + data = data.map(function(item) { + return item.value; + }); + } callback(null, data); }); } module.getSortedSetRange = function(key, start, stop, callback) { - getSortedSetRange(key, start, stop, 1, callback); + getSortedSetRange(key, start, stop, 1, false, callback); }; module.getSortedSetRevRange = function(key, start, stop, callback) { - getSortedSetRange(key, start, stop, -1, callback); + getSortedSetRange(key, start, stop, -1, false, callback); + }; + + module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) { + getSortedSetRange(key, start, stop, -1, true, callback) }; module.getSortedSetRangeByScore = function(key, start, count, min, max, callback) { diff --git a/src/database/redis/sorted.js b/src/database/redis/sorted.js index fe99566c5e..13dad80013 100644 --- a/src/database/redis/sorted.js +++ b/src/database/redis/sorted.js @@ -17,6 +17,19 @@ module.exports = function(redisClient, module) { redisClient.zrevrange(key, start, stop, callback); }; + module.getSortedSetRevRangeWithScores = function(key, start, stop, callback) { + redisClient.zrevrange([key, start, stop, 'WITHSCORES'], function(err, data) { + if (err) { + return callback(err); + } + var objects = []; + for(var i=0; i_> - Topics.getTagsObjects = function(callback) { - Topics.getTags(function(err, tags) { - callback(err, mapToObject(tags)); - }); + Topics.getTags = function(start, end, callback) { + db.getSortedSetRevRangeWithScores('tags:topic:count', start, end, callback); }; Topics.getTopicTags = function(tid, callback) { @@ -99,7 +105,7 @@ module.exports = function(Topics) { return callback(null, []); } - db.getSetMembers('tags', function(err, tags) { + db.getSortedSetRevRange('tags:topic:count', 0, -1, function(err, tags) { if (err) { return callback(null, []); } diff --git a/src/upgrade.js b/src/upgrade.js index cfb73ea1b0..35329511ce 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, 4, 2); + latestSchema = Date.UTC(2014, 4, 22); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -689,6 +689,50 @@ Upgrade.upgrade = function(callback) { winston.info('[2014/5/16] Removing allowGuestPosting option - skipped'); next(); } + }, + function(next) { + thisSchemaDate = Date.UTC(2014, 4, 22); + + if (schemaDate < thisSchemaDate) { + db.exists('tags', function(err, exists) { + if (err || !exists) { + winston.info('[2014/5/22] Skipping tag upgrade'); + return Upgrade.update(thisSchemaDate, next); + } + + db.getSetMembers('tags', function(err, tags) { + if (err) { + return next(err); + } + + async.each(tags, function(tag, next) { + db.sortedSetCard('tag:' + tag + ':topics', function(err, count) { + if (err) { + return next(err); + } + db.sortedSetAdd('tags:topic:count', count, tag, next); + }); + }, function(err) { + if (err) { + winston.error('[2014/5/22] Error encountered while upgrading tags'); + return next(err); + } + + db.delete('tags', function(err) { + if (err) { + winston.error('[2014/5/22] Error encountered while upgrading tags'); + return next(err); + } + winston.info('[2014/5/22] Tags upgraded to sorted set'); + Upgrade.update(thisSchemaDate, next); + }); + }); + }); + }); + } else { + winston.info('[2014/5/16] Tags upgrade - skipped'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!!