You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.8 KiB
JavaScript

'use strict';
module.exports = function (module) {
const helpers = require('../helpers');
module.sortedSetIntersectCard = async function (keys) {
if (!Array.isArray(keys) || !keys.length) {
return 0;
}
const tempSetName = `temp_${Date.now()}`;
const interParams = [tempSetName, keys.length].concat(keys);
const multi = module.client.multi();
multi.zinterstore(interParams);
multi.zcard(tempSetName);
multi.del(tempSetName);
const results = await helpers.execBatch(multi);
return results[1] || 0;
};
module.getSortedSetIntersect = async function (params) {
params.method = 'zrange';
return await getSortedSetRevIntersect(params);
};
module.getSortedSetRevIntersect = async function (params) {
params.method = 'zrevrange';
return await getSortedSetRevIntersect(params);
};
async function getSortedSetRevIntersect(params) {
const sets = params.sets;
const start = params.hasOwnProperty('start') ? params.start : 0;
const stop = params.hasOwnProperty('stop') ? params.stop : -1;
const weights = params.weights || [];
const tempSetName = `temp_${Date.now()}`;
let interParams = [tempSetName, sets.length].concat(sets);
if (weights.length) {
interParams = interParams.concat(['WEIGHTS'].concat(weights));
}
if (params.aggregate) {
interParams = interParams.concat(['AGGREGATE', params.aggregate]);
}
const rangeParams = [tempSetName, start, stop];
if (params.withScores) {
rangeParams.push('WITHSCORES');
}
const multi = module.client.multi();
multi.zinterstore(interParams);
multi[params.method](rangeParams);
multi.del(tempSetName);
let results = await helpers.execBatch(multi);
if (!params.withScores) {
return results ? results[1] : null;
}
results = results[1] || [];
return helpers.zsetToObjectArray(results);
}
};