diff --git a/src/batch.js b/src/batch.js index 8442def721..4fb6c9a8e4 100644 --- a/src/batch.js +++ b/src/batch.js @@ -23,6 +23,7 @@ exports.processSortedSet = async function (setKey, process, options) { } options.batch = options.batch || DEFAULT_BATCH_SIZE; + options.reverse = options.reverse || false; // use the fast path if possible if (db.processSortedSet && typeof options.doneIf !== 'function' && !utils.isNumber(options.alwaysStartAt)) { @@ -38,10 +39,10 @@ exports.processSortedSet = async function (setKey, process, options) { if (process && process.constructor && process.constructor.name !== 'AsyncFunction') { process = util.promisify(process); } - + const method = options.reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'; while (true) { /* eslint-disable no-await-in-loop */ - const ids = await db[`getSortedSetRange${options.withScores ? 'WithScores' : ''}`](setKey, start, stop); + const ids = await db[`${method}${options.withScores ? 'WithScores' : ''}`](setKey, start, stop); if (!ids.length || options.doneIf(start, stop, ids)) { return; } diff --git a/src/database/mongo/sorted.js b/src/database/mongo/sorted.js index 4e03a93509..9dbf947b1a 100644 --- a/src/database/mongo/sorted.js +++ b/src/database/mongo/sorted.js @@ -563,12 +563,12 @@ module.exports = function (module) { let done = false; const ids = []; const project = { _id: 0, _key: 0 }; - + const sort = options.reverse ? -1 : 1; if (!options.withScores) { project.score = 0; } const cursor = await module.client.collection('objects').find({ _key: setKey }, { projection: project }) - .sort({ score: 1 }) + .sort({ score: sort }) .batchSize(options.batch); if (processFn && processFn.constructor && processFn.constructor.name !== 'AsyncFunction') { diff --git a/src/database/postgres/sorted.js b/src/database/postgres/sorted.js index 2ed679b683..70e66af314 100644 --- a/src/database/postgres/sorted.js +++ b/src/database/postgres/sorted.js @@ -664,6 +664,7 @@ SELECT z."value", module.processSortedSet = async function (setKey, process, options) { const client = await module.pool.connect(); const batchSize = (options || {}).batch || 100; + const sort = options.reverse ? 'DESC' : 'ASC'; const cursor = client.query(new Cursor(` SELECT z."value", z."score" FROM "legacy_object_live" o @@ -671,7 +672,7 @@ SELECT z."value", z."score" ON o."_key" = z."_key" AND o."type" = z."type" WHERE o."_key" = $1::TEXT - ORDER BY z."score" ASC, z."value" ASC`, [setKey])); + ORDER BY z."score" ${sort}, z."value" ${sort}`, [setKey])); if (process && process.constructor && process.constructor.name !== 'AsyncFunction') { process = util.promisify(process); diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js index 0ef05c5acd..ccde83e3f4 100644 --- a/src/messaging/notifications.js +++ b/src/messaging/notifications.js @@ -83,6 +83,7 @@ module.exports = function (Messaging) { notifications.push(notification, uids); }, { + reverse: true, batch: 500, interval: 1000, });