updated getPostsByPids method to resolve premature return issue, and

refactored it to use async.map instead of eachSeries
v1.18.x
Julian Lam 12 years ago
parent 607ee8bbc1
commit 4cea313060

@ -19,7 +19,6 @@ var RDB = require('./redis.js'),
Posts.getPostsByTid = function(tid, start, end, callback) { Posts.getPostsByTid = function(tid, start, end, callback) {
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) { RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
RDB.handle(err); RDB.handle(err);
if (pids.length) { if (pids.length) {
@ -154,28 +153,23 @@ var RDB = require('./redis.js'),
RDB.hset('post:' + pid, field, value); RDB.hset('post:' + pid, field, value);
} }
/* getPostsByPids using redis's multi pipeline */
Posts.getPostsByPids = function(pids, callback) { Posts.getPostsByPids = function(pids, callback) {
var posts = [] var posts = [],
var multi = RDB.multi(); multi = RDB.multi();
for (v in pids) { for(var x=0,numPids=pids.length;x<numPids;x++) {
var _pid = pids[v] multi.hgetall("post:"+pids[x]);
multi.hgetall("post:"+_pid);
} }
multi.exec(function (err, replies) { multi.exec(function (err, replies) {
async.eachSeries(replies, function(postData, _callback) { async.map(replies, function(postData, _callback) {
if(postData) { if (postData) {
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString(); postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
postData.post_rep = postData.reputation; postData.post_rep = postData.reputation;
postData['edited-class'] = postData.editor !== '' ? '' : 'none'; postData['edited-class'] = postData.editor !== '' ? '' : 'none';
postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : ''; postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
if(postData.uploadedImages) { if (postData.uploadedImages) {
try { try {
postData.uploadedImages = JSON.parse(postData.uploadedImages); postData.uploadedImages = JSON.parse(postData.uploadedImages);
} catch(err) { } catch(err) {
@ -188,15 +182,13 @@ var RDB = require('./redis.js'),
postTools.parse(postData.content, function(err, content) { postTools.parse(postData.content, function(err, content) {
postData.content = content; postData.content = content;
posts.push(postData); _callback(null, postData);
}); });
return _callback(null) } else {
} _callback(null);
else {
return _callback(null)
} }
}, function(err) { }, function(err, posts) {
if(!err) { if (!err) {
return callback(null, posts); return callback(null, posts);
} else { } else {
return callback(err, null); return callback(err, null);
@ -205,37 +197,6 @@ var RDB = require('./redis.js'),
}) })
} }
Posts.getPostsByPids_original = function(pids, callback) {
var posts = [];
async.eachSeries(pids, function(pid, callback) {
Posts.getPostData(pid, function(postData) {
if (postData) {
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
postData.post_rep = postData.reputation;
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
postTools.parse(postData.content, function(err, content) {
postData.content = content;
posts.push(postData);
callback(null);
});
}
});
}, function(err) {
if (!err) {
callback(null, posts);
} else {
callback(err, null);
}
});
}
Posts.get_cid_by_pid = function(pid, callback) { Posts.get_cid_by_pid = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(tid) { Posts.getPostField(pid, 'tid', function(tid) {
if (tid) { if (tid) {

Loading…
Cancel
Save