From 53a7eab3e8fd6b93b3abac99eafdfb119578e367 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Wed, 4 Dec 2013 16:31:05 -0500 Subject: [PATCH] removed couple RDB.multis --- src/database/mongo.js | 34 ++++++++++++++++++++++++++++++++++ src/database/redis.js | 9 +++++++++ src/notifications.js | 30 ++++++++++++------------------ src/routes/debug.js | 19 +++++++++++++++---- src/user.js | 15 ++++++++------- tests/categories.js | 2 ++ 6 files changed, 80 insertions(+), 29 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index 3cc0b5c907..9ad149c3eb 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -439,6 +439,20 @@ }); } + module.sortedSetRank = function(key, value, callback) { + module.getSortedSetRange(key, 0, -1, function(err, result) { + if(err) { + return callback(err); + } + var rank = result.indexOf(value); + if(rank === -1) { + return callback(null, null); + } + + callback(null, rank); + }); + } + // lists module.listPrepend = function(key, value, callback) { module.isObjectField(key, 'array', function(err, exists) { @@ -471,6 +485,26 @@ }); } + module.listRemoveLast = function(key, callback) { + module.getListRange(key, -1, 0, function(err, value) { + if(err) { + return callback(err); + } + + db.collection('objects').update({_key: key }, { $pop: { array: 1 } }, function(err, result) { + if(err) { + return callback(err); + } + + if(value && value.length) { + callback(err, value[0]); + } else { + callback(err, null); + } + }); + }); + } + module.getListRange = function(key, start, stop, callback) { if(stop === -1) { diff --git a/src/database/redis.js b/src/database/redis.js index c640a5530b..a5ca2f78f4 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -291,6 +291,10 @@ redisClient.zcount(key, min, max, callback); } + module.sortedSetRank = function(key, value, callback) { + redisClient.zrank(key, value, callback); + } + // lists module.listPrepend = function(key, value, callback) { redisClient.lpush(key, value, callback); @@ -300,11 +304,16 @@ redisClient.rpush(key, value, callback); } + module.listRemoveLast = function(key, callback) { + redisClient.rpop(key, callback); + } + module.getListRange = function(key, start, stop, callback) { redisClient.lrange(key, start, stop, callback); } + }(exports)); diff --git a/src/notifications.js b/src/notifications.js index a6f8fc64a9..ec658b57d9 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -18,28 +18,22 @@ var async = require('async'), }; Notifications.get = function(nid, uid, callback) { - RDB.multi() - .hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId') - .zrank('uid:' + uid + ':notifications:read', nid) - .exists('notifications:' + nid) - .exec(function(err, results) { - var notification = results[0], - readIdx = results[1]; - if (!results[2]) { - return callback(null); - } + db.exists('nofitications:' + nid, function(err, exists) { - callback({ - nid: nid, - text: notification[0], - score: notification[1], - path: notification[2], - datetime: notification[3], - uniqueId: notification[4], - read: readIdx !== null ? true : false + if(!exists) { + return callback(null); + } + + db.sortedSetRank('uid:' + uid + ':notifications:read', nid, function(err, rank) { + + db.getObjectFields('notifications:' + nid, ['nid', 'text', 'score', 'path', 'datetime', 'uniqueId'], function(err, notification) { + + notification.read = rank !== null ? true:false; + callback(notification); }); }); + }); }; Notifications.create = function(text, path, uniqueId, callback) { diff --git a/src/routes/debug.js b/src/routes/debug.js index 76f28036a3..d475ee8774 100644 --- a/src/routes/debug.js +++ b/src/routes/debug.js @@ -197,8 +197,17 @@ var DebugRoute = function(app) { }); } + + function listRemoveLast(callback) { + db.listRemoveLast('myList5', function(err, data) { + console.log('listRemoveLast return', data); + callback(err, {'listRemoveLast': data}); + }); + } + + function getListRange(callback) { - db.getListRange('myList5', 0, 5, function(err, data) { + db.getListRange('myList5', 0, -1, function(err, data) { console.log('getListRange return', data); callback(err, {'getListRange': data}); }); @@ -306,8 +315,10 @@ var DebugRoute = function(app) { ]; var listTasks = [ - listAppend, - listPrepend, + //listAppend, + //listPrepend, + getListRange, + listRemoveLast, getListRange ]; @@ -331,7 +342,7 @@ var DebugRoute = function(app) { isMemberOfSets ]; - require('async').series(setTasks, function(err, results) { + require('async').series(listTasks, function(err, results) { if(err) { console.log(err); res.send(err.message); diff --git a/src/user.js b/src/user.js index 164efd6db1..7770f8862d 100644 --- a/src/user.js +++ b/src/user.js @@ -1005,14 +1005,13 @@ var bcrypt = require('bcrypt'), before = new Date(parseInt(before, 10)); } - // TODO : figure out how to do this with dbal - RDB.multi() - .zrevrangebyscore('uid:' + uid + ':notifications:read', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit) - .zrevrangebyscore('uid:' + uid + ':notifications:unread', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit) - .exec(function(err, results) { - // Merge the read and unread notifications - var nids = results[0].concat(results[1]); + var args1 = ['uid:' + uid + ':notifications:read', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit]; + var args2 = ['uid:' + uid + ':notifications:unread', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit]; + db.getSortedSetRevRangeByScore(args1, function(err, results1) { + db.getSortedSetRevRangeByScore(args2, function(err, results2) { + + var nids = results1.concat(results2); async.map(nids, function(nid, next) { notifications.get(nid, uid, function(notif_data) { next(null, notif_data); @@ -1032,6 +1031,8 @@ var bcrypt = require('bcrypt'), callback(err, notifs); }); }); + }); + }, getUnreadCount: function(uid, callback) { db.sortedSetCount('uid:' + uid + ':notifications:unread', 0, 10, callback); diff --git a/tests/categories.js b/tests/categories.js index 11456b54a4..36a4d63925 100644 --- a/tests/categories.js +++ b/tests/categories.js @@ -62,9 +62,11 @@ describe('Categories', function() { }); after(function() { + // TODO : replace with dbal RDB.multi() .del('category:'+categoryObj.cid) .rpop('categories:cid') .exec(); + }); }); \ No newline at end of file