Skip to content

Commit

Permalink
fix: handle scan/zscan returning duplicate elements on redis
Browse files Browse the repository at this point in the history
increase count on redis zscan
  • Loading branch information
barisusakli committed Jul 6, 2020
1 parent ee38e05 commit 746222d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/database/redis/main.js
Expand Up @@ -26,11 +26,19 @@ module.exports = function (module) {
module.scan = async function (params) {
let cursor = '0';
let returnData = [];
const seen = {};
do {
/* eslint-disable no-await-in-loop */
const res = await module.client.async.scan(cursor, 'MATCH', params.match, 'COUNT', 10000);
cursor = res[0];
returnData = returnData.concat(res[1]);
const values = res[1].filter((value) => {
const isSeen = !!seen[value];
if (!isSeen) {
seen[value] = 1;
}
return !isSeen;
});
returnData = returnData.concat(values);
} while (cursor !== '0');
return returnData;
};
Expand Down
24 changes: 14 additions & 10 deletions src/database/redis/sorted.js
Expand Up @@ -282,24 +282,28 @@ module.exports = function (module) {

const returnData = [];
let done = false;
const seen = {};
do {
/* eslint-disable no-await-in-loop */
const res = await module.client.async.zscan(params.key, cursor, 'MATCH', params.match, 'COUNT', 100);
const res = await module.client.async.zscan(params.key, cursor, 'MATCH', params.match, 'COUNT', 5000);
cursor = res[0];
done = cursor === '0';
const data = res[1];

for (let i = 0; i < data.length; i += 2) {
const value = data[i];
const score = parseFloat(data[i + 1]);
if (params.withScores) {
returnData.push({ value: value, score: score });
} else {
returnData.push(value);
}
if (params.limit && returnData.length >= params.limit) {
done = true;
break;
if (!seen[value]) {
seen[value] = 1;

if (params.withScores) {
returnData.push({ value: value, score: parseFloat(data[i + 2]) });
} else {
returnData.push(value);
}
if (params.limit && returnData.length >= params.limit) {
done = true;
break;
}
}
}
} while (!done);
Expand Down

0 comments on commit 746222d

Please sign in to comment.