mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 17:46:16 +01:00
remove dupe code in mongodb sorted
This commit is contained in:
@@ -12,37 +12,46 @@ module.exports = function (db, module) {
|
|||||||
require('./sorted/intersect')(db, module);
|
require('./sorted/intersect')(db, module);
|
||||||
|
|
||||||
module.getSortedSetRange = function (key, start, stop, callback) {
|
module.getSortedSetRange = function (key, start, stop, callback) {
|
||||||
getSortedSetRange(key, start, stop, 1, false, callback);
|
getSortedSetRange(key, start, stop, '-inf', '+inf', 1, false, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.getSortedSetRevRange = function (key, start, stop, callback) {
|
module.getSortedSetRevRange = function (key, start, stop, callback) {
|
||||||
getSortedSetRange(key, start, stop, -1, false, callback);
|
getSortedSetRange(key, start, stop, '-inf', '+inf', -1, false, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.getSortedSetRangeWithScores = function (key, start, stop, callback) {
|
module.getSortedSetRangeWithScores = function (key, start, stop, callback) {
|
||||||
getSortedSetRange(key, start, stop, 1, true, callback);
|
getSortedSetRange(key, start, stop, '-inf', '+inf', 1, true, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.getSortedSetRevRangeWithScores = function (key, start, stop, callback) {
|
module.getSortedSetRevRangeWithScores = function (key, start, stop, callback) {
|
||||||
getSortedSetRange(key, start, stop, -1, true, callback);
|
getSortedSetRange(key, start, stop, '-inf', '+inf', -1, true, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
function getSortedSetRange(key, start, stop, sort, withScores, callback) {
|
function getSortedSetRange(key, start, stop, min, max, sort, withScores, callback) {
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
if (start < 0 && start > stop) {
|
||||||
var fields = { _id: 0, _key: 0 };
|
return callback(null, []);
|
||||||
if (!withScores) {
|
|
||||||
fields.score = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(key)) {
|
if (Array.isArray(key)) {
|
||||||
key = { $in: key };
|
key = { $in: key };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start < 0 && start > stop) {
|
var query = { _key: key };
|
||||||
return callback(null, []);
|
|
||||||
|
if (min !== '-inf') {
|
||||||
|
query.score = { $gte: min };
|
||||||
|
}
|
||||||
|
if (max !== '+inf') {
|
||||||
|
query.score = query.score || {};
|
||||||
|
query.score.$lte = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
const fields = { _id: 0, _key: 0 };
|
||||||
|
if (!withScores) {
|
||||||
|
fields.score = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
var reverse = false;
|
var reverse = false;
|
||||||
@@ -62,10 +71,10 @@ module.exports = function (db, module) {
|
|||||||
limit = 0;
|
limit = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
db.collection('objects').find({ _key: key }, { projection: fields })
|
db.collection('objects').find(query, { projection: fields })
|
||||||
.limit(limit)
|
|
||||||
.skip(start)
|
|
||||||
.sort({ score: sort })
|
.sort({ score: sort })
|
||||||
|
.skip(start)
|
||||||
|
.limit(limit)
|
||||||
.toArray(function (err, data) {
|
.toArray(function (err, data) {
|
||||||
if (err || !data) {
|
if (err || !data) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@@ -75,9 +84,7 @@ module.exports = function (db, module) {
|
|||||||
data.reverse();
|
data.reverse();
|
||||||
}
|
}
|
||||||
if (!withScores) {
|
if (!withScores) {
|
||||||
data = data.map(function (item) {
|
data = data.map(item => item.value);
|
||||||
return item.value;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, data);
|
callback(null, data);
|
||||||
@@ -101,45 +108,11 @@ module.exports = function (db, module) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function getSortedSetRangeByScore(key, start, count, min, max, sort, withScores, callback) {
|
function getSortedSetRangeByScore(key, start, count, min, max, sort, withScores, callback) {
|
||||||
if (!key) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
if (parseInt(count, 10) === -1) {
|
if (parseInt(count, 10) === -1) {
|
||||||
count = 0;
|
count = 0;
|
||||||
}
|
}
|
||||||
|
var stop = start + count - 1;
|
||||||
var query = { _key: key };
|
getSortedSetRange(key, start, stop, min, max, sort, withScores, callback);
|
||||||
|
|
||||||
if (min !== '-inf') {
|
|
||||||
query.score = { $gte: min };
|
|
||||||
}
|
|
||||||
if (max !== '+inf') {
|
|
||||||
query.score = query.score || {};
|
|
||||||
query.score.$lte = max;
|
|
||||||
}
|
|
||||||
|
|
||||||
var fields = { _id: 0, _key: 0 };
|
|
||||||
if (!withScores) {
|
|
||||||
fields.score = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
db.collection('objects').find(query, { projection: fields })
|
|
||||||
.limit(count)
|
|
||||||
.skip(start)
|
|
||||||
.sort({ score: sort })
|
|
||||||
.toArray(function (err, data) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!withScores) {
|
|
||||||
data = data.map(function (item) {
|
|
||||||
return item.value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(err, data);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.sortedSetCount = function (key, min, max, callback) {
|
module.sortedSetCount = function (key, min, max, callback) {
|
||||||
|
|||||||
@@ -201,6 +201,17 @@ describe('Sorted Set methods', function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return elements from 3 to last', function (done) {
|
||||||
|
db.sortedSetAdd('partialZset', [1, 2, 3, 4, 5], ['value1', 'value2', 'value3', 'value4', 'value5'], function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
db.getSortedSetRangeByScore('partialZset', 3, 10, '-inf', '+inf', function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.deepStrictEqual(data, ['value4', 'value5']);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('getSortedSetRevRangeByScore()', function () {
|
describe('getSortedSetRevRangeByScore()', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user