fix: redis tests

v1.18.x
Barış Soner Uşaklı 5 years ago
parent 1e5246f9ad
commit 1f054c7ecb

@ -17,3 +17,11 @@ helpers.resultsToBool = function (results) {
}
return results;
};
helpers.zsetToObjectArray = function (data) {
const objects = new Array(data.length / 2);
for (let i = 0, k = 0; i < objects.length; i += 1, k += 2) {
objects[i] = { value: data[k], score: parseFloat(data[k + 1]) };
}
return objects;
};

@ -1,9 +1,8 @@
'use strict';
module.exports = function (module) {
var _ = require('lodash');
var utils = require('../../utils');
var helpers = require('./helpers');
const utils = require('../../utils');
const helpers = require('./helpers');
require('./sorted/add')(module);
require('./sorted/remove')(module);
@ -27,27 +26,40 @@ module.exports = function (module) {
};
async function sortedSetRange(method, key, start, stop, withScores) {
function getFirst(mapped) {
let selectedArray = mapped[0];
for (let i = 1; i < mapped.length; i++) {
if (mapped[i].length && (
!selectedArray.length ||
(method === 'zrange' && mapped[i][0].score < selectedArray[0].score) ||
(method === 'zrevrange' && mapped[i][0].score > selectedArray[0].score)
)) {
selectedArray = mapped[i];
}
}
return selectedArray.length ? selectedArray.shift() : null;
}
if (Array.isArray(key)) {
if (!key.length) {
return [];
}
const batch = module.client.batch();
key.forEach((key) => {
batch[method]([key, start, stop, 'WITHSCORES']);
});
let data = await helpers.execBatch(batch);
data = _.flatten(data);
let objects = [];
for (let i = 0; i < data.length; i += 2) {
objects.push({ value: data[i], score: parseFloat(data[i + 1]) });
}
key.forEach(key => batch[method]([key, start, stop, 'WITHSCORES']));
const data = await helpers.execBatch(batch);
objects.sort((a, b) => {
if (method === 'zrange') {
return a.score - b.score;
const mapped = data.map(setData => helpers.zsetToObjectArray(setData));
let objects = [];
const count = stop - start + 1;
let item = null;
do {
item = getFirst(mapped);
if (item) {
objects.push(item);
}
return b.score - a.score;
});
} while (item && (objects.length < count || stop === -1));
if (!withScores) {
objects = objects.map(item => item.value);
}
@ -63,10 +75,7 @@ module.exports = function (module) {
if (!withScores) {
return data;
}
const objects = [];
for (var i = 0; i < data.length; i += 2) {
objects.push({ value: data[i], score: parseFloat(data[i + 1]) });
}
const objects = helpers.zsetToObjectArray(data);
return objects;
}
@ -88,11 +97,7 @@ module.exports = function (module) {
async function sortedSetRangeByScoreWithScores(method, key, start, count, min, max) {
const data = await module.client.async[method]([key, min, max, 'WITHSCORES', 'LIMIT', start, count]);
const objects = [];
for (var i = 0; i < data.length; i += 2) {
objects.push({ value: data[i], score: parseFloat(data[i + 1]) });
}
return objects;
return helpers.zsetToObjectArray(data);
}
module.sortedSetCount = async function (key, min, max) {

@ -61,10 +61,6 @@ module.exports = function (module) {
return results ? results[1] : null;
}
results = results[1] || [];
var objects = [];
for (var i = 0; i < results.length; i += 2) {
objects.push({ value: results[i], score: parseFloat(results[i + 1]) });
}
return objects;
return helpers.zsetToObjectArray(results);
}
};

@ -47,10 +47,6 @@ module.exports = function (module) {
return results ? results[1] : null;
}
results = results[1] || [];
var objects = [];
for (var i = 0; i < results.length; i += 2) {
objects.push({ value: results[i], score: parseFloat(results[i + 1]) });
}
return objects;
return helpers.zsetToObjectArray(results);
};
};

Loading…
Cancel
Save