messages:<uid>:<uid> list changed to messages:uid:<uid>:to:<uid>
sortedset
v1.18.x
barisusakli 11 years ago
parent a79bb29216
commit c7de38f28e

@ -20,10 +20,10 @@ var db = require('./database'),
if (err) {
return callback(err);
}
var timestamp = Date.now();
var message = {
content: content,
timestamp: Date.now(),
timestamp: timestamp,
fromuid: fromuid,
touid: touid
};
@ -44,7 +44,7 @@ var db = require('./database'),
return callback(err);
}
db.listAppend('messages:' + uids[0] + ':' + uids[1], mid);
db.sortedSetAdd('messages:uid:' + uids[0] + ':to:' + uids[1], timestamp, mid);
db.listPrepend('messages:recent:' + fromuid, message.content, function(err) {
if (err) {
return callback(err);
@ -85,7 +85,7 @@ var db = require('./database'),
Messaging.getMessages = function(fromuid, touid, isNew, callback) {
var uids = sortUids(fromuid, touid);
db.getListRange('messages:' + uids[0] + ':' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) {
db.getSortedSetRange('messages:uid:' + uids[0] + ':to:' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) {
if (err) {
return callback(err);
}

@ -19,7 +19,7 @@ var db = require('./database'),
schemaDate, thisSchemaDate,
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema
latestSchema = Date.UTC(2014, 6, 23);
latestSchema = Date.UTC(2014, 6, 24);
Upgrade.check = function(callback) {
db.get('schemaDate', function(err, value) {
@ -882,6 +882,87 @@ Upgrade.upgrade = function(callback) {
winston.info('[2014/7/23] Upgrading db dependencies - skipped');
next();
}
},
function(next) {
thisSchemaDate = Date.UTC(2014, 6, 24);
if (schemaDate < thisSchemaDate) {
winston.info('[2014/7/24] Upgrading chats to sorted set...');
db.getSortedSetRange('users:joindate', 0, -1, function(err, uids) {
if (err) {
return next(err);
}
async.eachLimit(uids, 10, function(uid, next) {
db.getSortedSetRange('uid:' + uid + ':chats', 0, -1, function(err, toUids) {
if (err) {
return next(err);
}
if (!Array.isArray(toUids) || !toUids.length) {
return next();
}
async.eachLimit(toUids, 10, function(toUid, next) {
var uids = [uid, toUid].sort();
db.getListRange('messages:' + uids[0] + ':' + uids[1], 0, -1, function(err, mids) {
if (err) {
return next(err);
}
if (!Array.isArray(mids) || !mids.length) {
return next();
}
async.eachLimit(mids, 10, function(mid, next) {
db.getObjectField('message:' + mid, 'timestamp', function(err, timestamp) {
if (err || !timestamp) {
return next(err);
}
db.sortedSetAdd('messages:uid:' + uids[0] + ':to:' + uids[1], timestamp, mid, next);
});
}, next);
});
}, next);
});
}, function(err) {
if (err) {
winston.error('[2014/7/24] Error encountered while updating chats to sorted set');
return next(err);
}
async.eachLimit(uids, 10, function(uid, next) {
db.getSortedSetRange('uid:' + uid + ':chats', 0, -1, function(err, toUids) {
if (err) {
return next(err);
}
if (!Array.isArray(toUids) || !toUids.length) {
return next();
}
async.eachLimit(toUids, 10, function(toUid, next) {
var uids = [uid, toUid].sort();
db.delete('messages:' + uids[0] + ':' + uids[1], next);
}, next);
});
}, function(err) {
if (err) {
winston.error('[2014/7/24] Error encountered while updating chats to sorted set');
return next(err);
}
winston.info('[2014/7/24] Upgraded chats to sorted set');
Upgrade.update(thisSchemaDate, next);
});
});
});
} else {
winston.info('[2014/7/24] Upgrading chats to sorted set - skipped');
next();
}
}
// Add new schema updates here
// IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 22!!!

Loading…
Cancel
Save