mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-05-07 12:16:54 +02:00
Merge pull request #5124 from yariplus/lexistuff
Add additional lexical dbal operations.
This commit is contained in:
@@ -579,16 +579,50 @@ module.exports = function (db, module) {
|
||||
};
|
||||
|
||||
module.getSortedSetRangeByLex = function (key, min, max, start, count, callback) {
|
||||
sortedSetLex(key, min, max, 1, start, count, callback);
|
||||
};
|
||||
|
||||
module.getSortedSetRevRangeByLex = function (key, max, min, start, count, callback) {
|
||||
sortedSetLex(key, min, max, -1, start, count, callback);
|
||||
};
|
||||
|
||||
module.sortedSetLexCount = function (key, min, max, callback) {
|
||||
sortedSetLex(key, min, max, 1, 0, 0, function (err, data) {
|
||||
callback(err, data ? data.length : null);
|
||||
});
|
||||
};
|
||||
|
||||
function sortedSetLex(key, min, max, sort, start, count, callback) {
|
||||
if (!callback) {
|
||||
callback = start;
|
||||
start = 0;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
var query = {_key: key};
|
||||
|
||||
if (min !== '-') {
|
||||
query.value = {$gte: min};
|
||||
if (min.match(/^\(/)) {
|
||||
query.value = {$gt: min.slice(1)};
|
||||
} else if (min.match(/^\[/)) {
|
||||
query.value = {$gte: min.slice(1)};
|
||||
} else {
|
||||
query.value = {$gte: min};
|
||||
}
|
||||
}
|
||||
if (max !== '+') {
|
||||
query.value = query.value || {};
|
||||
query.value.$lte = max;
|
||||
if (max.match(/^\(/)) {
|
||||
query.value.$lt = max.slice(1);
|
||||
} else if (max.match(/^\[/)) {
|
||||
query.value.$lte = max.slice(1);
|
||||
} else {
|
||||
query.value.$lte = max;
|
||||
}
|
||||
}
|
||||
|
||||
db.collection('objects').find(query, {_id: 0, value: 1})
|
||||
.sort({value: 1})
|
||||
.sort({value: sort})
|
||||
.skip(start)
|
||||
.limit(count === -1 ? 0 : count)
|
||||
.toArray(function (err, data) {
|
||||
@@ -600,6 +634,36 @@ module.exports = function (db, module) {
|
||||
});
|
||||
callback(err, data);
|
||||
});
|
||||
}
|
||||
|
||||
module.sortedSetRemoveRangeByLex = function (key, min, max, callback) {
|
||||
callback = callback || helpers.noop;
|
||||
|
||||
var query = {_key: key};
|
||||
|
||||
if (min !== '-') {
|
||||
if (min.match(/^\(/)) {
|
||||
query.value = {$gt: min.slice(1)};
|
||||
} else if (min.match(/^\[/)) {
|
||||
query.value = {$gte: min.slice(1)};
|
||||
} else {
|
||||
query.value = {$gte: min};
|
||||
}
|
||||
}
|
||||
if (max !== '+') {
|
||||
query.value = query.value || {};
|
||||
if (max.match(/^\(/)) {
|
||||
query.value.$lt = max.slice(1);
|
||||
} else if (max.match(/^\[/)) {
|
||||
query.value.$lte = max.slice(1);
|
||||
} else {
|
||||
query.value.$lte = max;
|
||||
}
|
||||
}
|
||||
|
||||
db.collection('objects').remove(query, function (err) {
|
||||
callback(err);
|
||||
});
|
||||
};
|
||||
|
||||
module.processSortedSet = function (setKey, process, batch, callback) {
|
||||
@@ -639,7 +703,6 @@ module.exports = function (db, module) {
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
module.sortedSetIntersectCard = function (keys, callback) {
|
||||
if (!Array.isArray(keys) || !keys.length) {
|
||||
return callback(null, 0);
|
||||
|
||||
@@ -293,15 +293,50 @@ module.exports = function (redisClient, module) {
|
||||
};
|
||||
|
||||
module.getSortedSetRangeByLex = function (key, min, max, start, count, callback) {
|
||||
if (min !== '-') {
|
||||
min = '[' + min;
|
||||
}
|
||||
if (max !== '+') {
|
||||
max = '(' + max;
|
||||
}
|
||||
redisClient.zrangebylex([key, min, max, 'LIMIT', start, count], callback);
|
||||
sortedSetLex('zrangebylex', false, key, min, max, start, count, callback);
|
||||
};
|
||||
|
||||
module.getSortedSetRevRangeByLex = function (key, max, min, start, count, callback) {
|
||||
sortedSetLex('zrevrangebylex', true, key, max, min, start, count, callback);
|
||||
};
|
||||
|
||||
module.sortedSetRemoveRangeByLex = function (key, min, max, callback) {
|
||||
callback = callback || helpers.noop;
|
||||
sortedSetLex('zremrangebylex', false, key, min, max, function (err) {
|
||||
callback(err);
|
||||
});
|
||||
};
|
||||
|
||||
module.sortedSetLexCount = function (key, min, max, callback) {
|
||||
sortedSetLex('zlexcount', false, key, min, max, callback);
|
||||
};
|
||||
|
||||
function sortedSetLex(method, reverse, key, min, max, start, count, callback) {
|
||||
callback = callback || start;
|
||||
|
||||
var minmin, maxmax;
|
||||
if (reverse) {
|
||||
minmin = '+';
|
||||
maxmax = '-';
|
||||
} else {
|
||||
minmin = '-';
|
||||
maxmax = '+';
|
||||
}
|
||||
|
||||
if (min !== minmin) {
|
||||
if (!min.match(/^[\[\(]/)) min = '[' + min;
|
||||
}
|
||||
if (max !== maxmax) {
|
||||
if (!max.match(/^[\[\(]/)) max = '[' + max;
|
||||
}
|
||||
|
||||
if (count) {
|
||||
redisClient[method]([key, min, max, 'LIMIT', start, count], callback);
|
||||
} else {
|
||||
redisClient[method]([key, min, max], callback);
|
||||
}
|
||||
}
|
||||
|
||||
module.sortedSetIntersectCard = function (keys, callback) {
|
||||
if (!Array.isArray(keys) || !keys.length) {
|
||||
return callback(null, 0);
|
||||
|
||||
Reference in New Issue
Block a user