feat: refactor getSortedSetRange to allow big arrays

This commit is contained in:
Barış Soner Uşaklı
2019-09-24 23:47:45 -04:00
parent 7e7ea7a697
commit b602c04463
4 changed files with 95 additions and 37 deletions

View File

@@ -1,9 +1,11 @@
'use strict';
var utils = require('../../utils');
const utils = require('../../utils');
module.exports = function (module) {
var helpers = require('./helpers');
const helpers = require('./helpers');
const dbHelpers = require('../helpers');
const util = require('util');
const sleep = util.promisify(setTimeout);
@@ -62,37 +64,54 @@ module.exports = function (module) {
fields.score = 0;
}
var reverse = false;
let reverse = false;
if (start === 0 && stop < -1) {
reverse = true;
sort *= -1;
start = Math.abs(stop + 1);
stop = -1;
} else if (start < 0 && stop > start) {
var tmp1 = Math.abs(stop + 1);
const tmp1 = Math.abs(stop + 1);
stop = Math.abs(start + 1);
start = tmp1;
}
var limit = stop - start + 1;
let limit = stop - start + 1;
if (limit <= 0) {
limit = 0;
}
let data = await module.client.collection('objects').find(query, { projection: fields })
.sort({ score: sort })
.skip(start)
.limit(limit)
.toArray();
let result = [];
async function doQuery(fields) {
return await module.client.collection('objects').find(query, { projection: fields })
.sort({ score: sort })
.skip(start)
.limit(limit)
.toArray();
}
if (isArray && key.length > 100) {
const batches = [];
const batch = require('../../batch');
await batch.processArray(key, async currentBatch => batches.push(currentBatch), { batch: 100 });
const batchData = await Promise.all(batches.map(async (batch) => {
query._key = { $in: batch };
return await doQuery({ _id: 0, _key: 0 });
}));
result = dbHelpers.mergeBatch(batchData, start, stop, sort);
} else {
result = await doQuery(fields);
}
if (reverse) {
data.reverse();
result.reverse();
}
if (!withScores) {
data = data.map(item => item.value);
result = result.map(item => item.value);
}
return data;
return result;
}
module.getSortedSetRangeByScore = async function (key, start, count, min, max) {