| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | const utils = require('../../utils'); | 
					
						
							| 
									
										
										
										
											2014-07-29 17:33:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | module.exports = function (module) { | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 	const helpers = require('./helpers'); | 
					
						
							|  |  |  | 	const dbHelpers = require('../helpers'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	const util = require('util'); | 
					
						
							|  |  |  | 	const sleep = util.promisify(setTimeout); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 	require('./sorted/add')(module); | 
					
						
							|  |  |  | 	require('./sorted/remove')(module); | 
					
						
							|  |  |  | 	require('./sorted/union')(module); | 
					
						
							|  |  |  | 	require('./sorted/intersect')(module); | 
					
						
							| 
									
										
										
										
											2014-09-08 16:13:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRange = async function (key, start, stop) { | 
					
						
							|  |  |  | 		return await getSortedSetRange(key, start, stop, '-inf', '+inf', 1, false); | 
					
						
							| 
									
										
										
										
											2014-12-31 14:41:58 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRevRange = async function (key, start, stop) { | 
					
						
							|  |  |  | 		return await getSortedSetRange(key, start, stop, '-inf', '+inf', -1, false); | 
					
						
							| 
									
										
										
										
											2014-12-31 14:41:58 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRangeWithScores = async function (key, start, stop) { | 
					
						
							|  |  |  | 		return await getSortedSetRange(key, start, stop, '-inf', '+inf', 1, true); | 
					
						
							| 
									
										
										
										
											2014-12-31 14:41:58 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRevRangeWithScores = async function (key, start, stop) { | 
					
						
							|  |  |  | 		return await getSortedSetRange(key, start, stop, '-inf', '+inf', -1, true); | 
					
						
							| 
									
										
										
										
											2014-12-31 14:41:58 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	async function getSortedSetRange(key, start, stop, min, max, sort, withScores) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		const isArray = Array.isArray(key); | 
					
						
							|  |  |  | 		if ((start < 0 && start > stop) || (isArray && !key.length)) { | 
					
						
							|  |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2014-09-27 17:46:39 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-09-24 22:04:20 -04:00
										 |  |  | 		const query = { _key: key }; | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		if (isArray) { | 
					
						
							| 
									
										
										
										
											2018-12-12 13:46:13 -05:00
										 |  |  | 			if (key.length > 1) { | 
					
						
							| 
									
										
										
										
											2019-09-24 22:04:20 -04:00
										 |  |  | 				query._key = { $in: key }; | 
					
						
							| 
									
										
										
										
											2018-12-12 13:46:13 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2019-09-24 22:04:20 -04:00
										 |  |  | 				query._key = key[0]; | 
					
						
							| 
									
										
										
										
											2018-12-12 13:46:13 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-03-03 19:52:48 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 16:20:38 -04:00
										 |  |  | 		if (min !== '-inf') { | 
					
						
							|  |  |  | 			query.score = { $gte: min }; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (max !== '+inf') { | 
					
						
							|  |  |  | 			query.score = query.score || {}; | 
					
						
							|  |  |  | 			query.score.$lte = max; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 		if (max === min) { | 
					
						
							|  |  |  | 			query.score = max; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-28 16:20:38 -04:00
										 |  |  | 		const fields = { _id: 0, _key: 0 }; | 
					
						
							|  |  |  | 		if (!withScores) { | 
					
						
							|  |  |  | 			fields.score = 0; | 
					
						
							| 
									
										
										
										
											2017-12-07 20:43:02 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 		let reverse = false; | 
					
						
							| 
									
										
										
										
											2017-12-07 20:43:02 -05:00
										 |  |  | 		if (start === 0 && stop < -1) { | 
					
						
							|  |  |  | 			reverse = true; | 
					
						
							|  |  |  | 			sort *= -1; | 
					
						
							|  |  |  | 			start = Math.abs(stop + 1); | 
					
						
							|  |  |  | 			stop = -1; | 
					
						
							|  |  |  | 		} else if (start < 0 && stop > start) { | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 			const tmp1 = Math.abs(stop + 1); | 
					
						
							| 
									
										
										
										
											2017-12-07 20:43:02 -05:00
										 |  |  | 			stop = Math.abs(start + 1); | 
					
						
							|  |  |  | 			start = tmp1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 		let limit = stop - start + 1; | 
					
						
							| 
									
										
										
										
											2016-08-31 21:22:28 +03:00
										 |  |  | 		if (limit <= 0) { | 
					
						
							|  |  |  | 			limit = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 		let result = []; | 
					
						
							| 
									
										
										
										
											2019-09-25 21:06:57 -04:00
										 |  |  | 		async function doQuery(_key, fields, skip, limit) { | 
					
						
							| 
									
										
										
										
											2019-09-25 00:08:28 -04:00
										 |  |  | 			return await module.client.collection('objects').find({ ...query, ...{ _key: _key } }, { projection: fields }) | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 				.sort({ score: sort }) | 
					
						
							| 
									
										
										
										
											2019-09-25 21:06:57 -04:00
										 |  |  | 				.skip(skip) | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 				.limit(limit) | 
					
						
							|  |  |  | 				.toArray(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (isArray && key.length > 100) { | 
					
						
							|  |  |  | 			const batches = []; | 
					
						
							|  |  |  | 			const batch = require('../../batch'); | 
					
						
							| 
									
										
										
										
											2019-09-25 21:06:57 -04:00
										 |  |  | 			const batchSize = Math.ceil(key.length / Math.ceil(key.length / 100)); | 
					
						
							|  |  |  | 			await batch.processArray(key, async currentBatch => batches.push(currentBatch), { batch: batchSize }); | 
					
						
							|  |  |  | 			const batchData = await Promise.all(batches.map(batch => doQuery({ $in: batch }, { _id: 0, _key: 0 }, 0, stop + 1))); | 
					
						
							|  |  |  | 			result = dbHelpers.mergeBatch(batchData, 0, stop, sort); | 
					
						
							|  |  |  | 			if (start > 0) { | 
					
						
							|  |  |  | 				result = result.slice(start, stop !== -1 ? stop + 1 : undefined); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-09-25 21:06:57 -04:00
										 |  |  | 			result = await doQuery(query._key, fields, start, limit); | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		if (reverse) { | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 			result.reverse(); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (!withScores) { | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 			result = result.map(item => item.value); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-24 23:47:45 -04:00
										 |  |  | 		return result; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRangeByScore = async function (key, start, count, min, max) { | 
					
						
							|  |  |  | 		return await getSortedSetRangeByScore(key, start, count, min, max, 1, false); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRevRangeByScore = async function (key, start, count, max, min) { | 
					
						
							|  |  |  | 		return await getSortedSetRangeByScore(key, start, count, min, max, -1, false); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRangeByScoreWithScores = async function (key, start, count, min, max) { | 
					
						
							|  |  |  | 		return await getSortedSetRangeByScore(key, start, count, min, max, 1, true); | 
					
						
							| 
									
										
										
										
											2014-10-31 17:19:50 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRevRangeByScoreWithScores = async function (key, start, count, max, min) { | 
					
						
							|  |  |  | 		return await getSortedSetRangeByScore(key, start, count, min, max, -1, true); | 
					
						
							| 
									
										
										
										
											2014-09-27 17:41:49 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	async function getSortedSetRangeByScore(key, start, count, min, max, sort, withScores) { | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 		if (parseInt(count, 10) === 0) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 		const stop = (parseInt(count, 10) === -1) ? -1 : (start + count - 1); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return await getSortedSetRange(key, start, stop, min, max, sort, withScores); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetCount = async function (key, min, max) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		var query = { _key: key }; | 
					
						
							| 
									
										
										
										
											2014-12-31 16:09:33 -05:00
										 |  |  | 		if (min !== '-inf') { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			query.score = { $gte: min }; | 
					
						
							| 
									
										
										
										
											2014-12-31 16:09:33 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (max !== '+inf') { | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:55 -05:00
										 |  |  | 			query.score = query.score || {}; | 
					
						
							|  |  |  | 			query.score.$lte = max; | 
					
						
							| 
									
										
										
										
											2014-12-31 16:09:33 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-03-06 15:12:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const count = await module.client.collection('objects').countDocuments(query); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return count || 0; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetCard = async function (key) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const count = await module.client.collection('objects').countDocuments({ _key: key }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return parseInt(count, 10) || 0; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetsCard = async function (keys) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!Array.isArray(keys) || !keys.length) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		const promises = keys.map(k => module.sortedSetCard(k)); | 
					
						
							|  |  |  | 		return await Promise.all(promises); | 
					
						
							| 
									
										
										
										
											2014-08-14 21:12:12 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetsCardSum = async function (keys) { | 
					
						
							| 
									
										
										
										
											2019-06-24 15:21:43 -04:00
										 |  |  | 		if (!keys || (Array.isArray(keys) && !keys.length)) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return 0; | 
					
						
							| 
									
										
										
										
											2019-06-24 15:21:43 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const count = await module.client.collection('objects').countDocuments({ _key: Array.isArray(keys) ? { $in: keys } : keys }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return parseInt(count, 10) || 0; | 
					
						
							| 
									
										
										
										
											2019-06-24 15:21:43 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetRank = async function (key, value) { | 
					
						
							|  |  |  | 		return await getSortedSetRank(false, key, value); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetRevRank = async function (key, value) { | 
					
						
							|  |  |  | 		return await getSortedSetRank(true, key, value); | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	async function getSortedSetRank(reverse, key, value) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 		value = helpers.valueToString(value); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		const score = await module.sortedSetScore(key, value); | 
					
						
							|  |  |  | 		if (score === null) { | 
					
						
							|  |  |  | 			return null; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		return await module.client.collection('objects').countDocuments({ | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			$or: [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					_key: key, | 
					
						
							|  |  |  | 					score: reverse ? { $gt: score } : { $lt: score }, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					_key: key, | 
					
						
							|  |  |  | 					score: score, | 
					
						
							|  |  |  | 					value: reverse ? { $gt: value } : { $lt: value }, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetsRanks = async function (keys, values) { | 
					
						
							|  |  |  | 		return await sortedSetsRanks(module.sortedSetRank, keys, values); | 
					
						
							| 
									
										
										
										
											2019-07-03 12:48:26 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetsRevRanks = async function (keys, values) { | 
					
						
							|  |  |  | 		return await sortedSetsRanks(module.sortedSetRevRank, keys, values); | 
					
						
							| 
									
										
										
										
											2019-07-03 12:48:26 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	async function sortedSetsRanks(method, keys, values) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!Array.isArray(keys) || !keys.length) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-07-29 17:33:28 -04:00
										 |  |  | 		var data = new Array(values.length); | 
					
						
							| 
									
										
										
										
											2017-02-18 01:12:18 -07:00
										 |  |  | 		for (var i = 0; i < values.length; i += 1) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			data[i] = { key: keys[i], value: values[i] }; | 
					
						
							| 
									
										
										
										
											2014-07-29 17:33:28 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		const promises = data.map(item => method(item.key, item.value)); | 
					
						
							|  |  |  | 		return await Promise.all(promises); | 
					
						
							| 
									
										
										
										
											2019-07-03 12:48:26 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-29 17:33:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetRanks = async function (key, values) { | 
					
						
							| 
									
										
										
										
											2020-05-16 09:36:17 -04:00
										 |  |  | 		return await sortedSetRanks(false, key, values); | 
					
						
							| 
									
										
										
										
											2019-07-03 12:48:26 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetRevRanks = async function (key, values) { | 
					
						
							| 
									
										
										
										
											2020-05-16 09:36:17 -04:00
										 |  |  | 		return await sortedSetRanks(true, key, values); | 
					
						
							| 
									
										
										
										
											2019-07-03 12:48:26 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 09:36:17 -04:00
										 |  |  | 	async function sortedSetRanks(reverse, key, values) { | 
					
						
							|  |  |  | 		if (values.length === 1) { | 
					
						
							|  |  |  | 			return [await getSortedSetRank(reverse, key, values[0])]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const sortedSet = await module[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](key, 0, -1); | 
					
						
							|  |  |  | 		return values.map(function (value) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			if (!value) { | 
					
						
							|  |  |  | 				return null; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2020-05-16 09:36:17 -04:00
										 |  |  | 			const index = sortedSet.indexOf(value.toString()); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return index !== -1 ? index : null; | 
					
						
							| 
									
										
										
										
											2014-09-26 22:19:26 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-03 12:48:26 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-26 22:19:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetScore = async function (key, value) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return null; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 		value = helpers.valueToString(value); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const result = await module.client.collection('objects').findOne({ _key: key, value: value }, { projection: { _id: 0, _key: 0, value: 0 } }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return result ? result.score : null; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetsScore = async function (keys, value) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!Array.isArray(keys) || !keys.length) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-08-15 11:48:01 -04:00
										 |  |  | 		value = helpers.valueToString(value); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const result = await module.client.collection('objects').find({ _key: { $in: keys }, value: value }, { projection: { _id: 0, value: 0 } }).toArray(); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		var map = {}; | 
					
						
							|  |  |  | 		result.forEach(function (item) { | 
					
						
							|  |  |  | 			if (item) { | 
					
						
							|  |  |  | 				map[item._key] = item; | 
					
						
							| 
									
										
										
										
											2014-08-15 11:48:01 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return keys.map(key => (map[key] ? map[key].score : null)); | 
					
						
							| 
									
										
										
										
											2014-08-15 11:48:01 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetScores = async function (key, values) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return null; | 
					
						
							| 
									
										
										
										
											2018-12-17 18:56:09 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (!values.length) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-08-15 11:48:01 -04:00
										 |  |  | 		values = values.map(helpers.valueToString); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const result = await module.client.collection('objects').find({ _key: key, value: { $in: values } }, { projection: { _id: 0, _key: 0 } }).toArray(); | 
					
						
							| 
									
										
										
										
											2014-08-15 11:48:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		var valueToScore = {}; | 
					
						
							|  |  |  | 		result.forEach(function (item) { | 
					
						
							|  |  |  | 			if (item) { | 
					
						
							|  |  |  | 				valueToScore[item.value] = item.score; | 
					
						
							| 
									
										
										
										
											2014-08-15 11:48:01 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return values.map(v => (utils.isNumber(valueToScore[v]) ? valueToScore[v] : null)); | 
					
						
							| 
									
										
										
										
											2014-08-15 11:48:01 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.isSortedSetMember = async function (key, value) { | 
					
						
							| 
									
										
										
										
											2015-04-20 23:26:02 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2015-04-20 23:26:02 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		value = helpers.valueToString(value); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const result = await module.client.collection('objects').findOne({ _key: key, value: value }, { projection: { _id: 0, _key: 0, score: 0 } }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return !!result; | 
					
						
							| 
									
										
										
										
											2014-04-11 15:44:53 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.isSortedSetMembers = async function (key, values) { | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-21 13:30:20 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-07-22 12:56:34 -04:00
										 |  |  | 		values = values.map(helpers.valueToString); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const results = await module.client.collection('objects').find({ _key: key, value: { $in: values } }, { projection: { _id: 0, _key: 0, score: 0 } }).toArray(); | 
					
						
							| 
									
										
										
										
											2014-08-12 14:39:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		var isMember = {}; | 
					
						
							|  |  |  | 		results.forEach(function (item) { | 
					
						
							|  |  |  | 			if (item) { | 
					
						
							|  |  |  | 				isMember[item.value] = true; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-07-22 12:56:34 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return values.map(value => !!isMember[value]); | 
					
						
							| 
									
										
										
										
											2014-07-22 12:56:34 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.isMemberOfSortedSets = async function (keys, value) { | 
					
						
							| 
									
										
										
										
											2018-12-12 11:15:34 -05:00
										 |  |  | 		if (!Array.isArray(keys) || !keys.length) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		value = helpers.valueToString(value); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const results = await module.client.collection('objects').find({ _key: { $in: keys }, value: value }, { projection: { _id: 0, score: 0 } }).toArray(); | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		var isMember = {}; | 
					
						
							|  |  |  | 		results.forEach(function (item) { | 
					
						
							|  |  |  | 			if (item) { | 
					
						
							|  |  |  | 				isMember[item._key] = true; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return keys.map(key => !!isMember[key]); | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetsMembers = async function (keys) { | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 		if (!Array.isArray(keys) || !keys.length) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return []; | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-18 23:58:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const data = await module.client.collection('objects').find({ | 
					
						
							| 
									
										
										
										
											2019-07-18 23:58:59 -04:00
										 |  |  | 			_key: keys.length === 1 ? keys[0] : { $in: keys }, | 
					
						
							|  |  |  | 		}, { projection: { _id: 0, score: 0 } }).sort({ score: 1 }).toArray(); | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		var sets = {}; | 
					
						
							|  |  |  | 		data.forEach(function (set) { | 
					
						
							|  |  |  | 			sets[set._key] = sets[set._key] || []; | 
					
						
							|  |  |  | 			sets[set._key].push(set.value); | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return keys.map(k => sets[k] || []); | 
					
						
							| 
									
										
										
										
											2015-01-21 22:48:22 -05:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetIncrBy = async function (key, increment, value) { | 
					
						
							| 
									
										
										
										
											2014-09-27 15:48:16 -04:00
										 |  |  | 		if (!key) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-09-27 15:48:16 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		var data = {}; | 
					
						
							| 
									
										
										
										
											2016-05-25 21:18:14 +03:00
										 |  |  | 		value = helpers.valueToString(value); | 
					
						
							| 
									
										
										
										
											2016-12-11 15:58:57 +01:00
										 |  |  | 		data.score = parseFloat(increment); | 
					
						
							| 
									
										
										
										
											2014-09-27 15:48:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 			const result = await module.client.collection('objects').findOneAndUpdate({ _key: key, value: value }, { $inc: data }, { returnOriginal: false, upsert: true }); | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			return result && result.value ? result.value.score : null; | 
					
						
							|  |  |  | 		} catch (err) { | 
					
						
							| 
									
										
										
										
											2016-03-24 21:01:20 +02:00
										 |  |  | 			// if there is duplicate key error retry the upsert
 | 
					
						
							|  |  |  | 			// https://github.com/NodeBB/NodeBB/issues/4467
 | 
					
						
							|  |  |  | 			// https://jira.mongodb.org/browse/SERVER-14322
 | 
					
						
							|  |  |  | 			// https://docs.mongodb.org/manual/reference/command/findAndModify/#upsert-and-unique-index
 | 
					
						
							|  |  |  | 			if (err && err.message.startsWith('E11000 duplicate key error')) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				return await module.sortedSetIncrBy(key, increment, value); | 
					
						
							| 
									
										
										
										
											2016-03-24 21:01:20 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			throw err; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-09-27 15:48:16 -04:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2015-05-19 23:04:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRangeByLex = async function (key, min, max, start, count) { | 
					
						
							|  |  |  | 		return await sortedSetLex(key, min, max, 1, start, count); | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.getSortedSetRevRangeByLex = async function (key, max, min, start, count) { | 
					
						
							|  |  |  | 		return await sortedSetLex(key, min, max, -1, start, count); | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetLexCount = async function (key, min, max) { | 
					
						
							|  |  |  | 		const data = await sortedSetLex(key, min, max, 1, 0, 0); | 
					
						
							|  |  |  | 		return data ? data.length : null; | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	async function sortedSetLex(key, min, max, sort, start, count) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		var query = { _key: key }; | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		start = start !== undefined ? start : 0; | 
					
						
							|  |  |  | 		count = count !== undefined ? count : 0; | 
					
						
							| 
									
										
										
										
											2016-12-23 14:12:00 +03:00
										 |  |  | 		buildLexQuery(query, min, max); | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		const data = await module.client.collection('objects').find(query, { projection: { _id: 0, _key: 0, score: 0 } }) | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			.sort({ value: sort }) | 
					
						
							| 
									
										
										
										
											2015-05-19 23:04:28 -04:00
										 |  |  | 			.skip(start) | 
					
						
							|  |  |  | 			.limit(count === -1 ? 0 : count) | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 			.toArray(); | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		return data.map(item => item && item.value); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.sortedSetRemoveRangeByLex = async function (key, min, max) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		var query = { _key: key }; | 
					
						
							| 
									
										
										
										
											2016-12-23 14:12:00 +03:00
										 |  |  | 		buildLexQuery(query, min, max); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		await module.client.collection('objects').deleteMany(query); | 
					
						
							| 
									
										
										
										
											2016-12-23 14:12:00 +03:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 14:12:00 +03:00
										 |  |  | 	function buildLexQuery(query, min, max) { | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 		if (min !== '-') { | 
					
						
							| 
									
										
										
										
											2016-10-16 21:53:02 -04:00
										 |  |  | 			if (min.match(/^\(/)) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				query.value = { $gt: min.slice(1) }; | 
					
						
							| 
									
										
										
										
											2016-10-16 21:53:02 -04:00
										 |  |  | 			} else if (min.match(/^\[/)) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				query.value = { $gte: min.slice(1) }; | 
					
						
							| 
									
										
										
										
											2016-10-16 21:29:45 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				query.value = { $gte: min }; | 
					
						
							| 
									
										
										
										
											2016-10-16 21:29:45 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if (max !== '+') { | 
					
						
							|  |  |  | 			query.value = query.value || {}; | 
					
						
							| 
									
										
										
										
											2016-10-16 21:53:02 -04:00
										 |  |  | 			if (max.match(/^\(/)) { | 
					
						
							|  |  |  | 				query.value.$lt = max.slice(1); | 
					
						
							|  |  |  | 			} else if (max.match(/^\[/)) { | 
					
						
							|  |  |  | 				query.value.$lte = max.slice(1); | 
					
						
							| 
									
										
										
										
											2016-10-16 21:29:45 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2016-10-16 21:53:02 -04:00
										 |  |  | 				query.value.$lte = max; | 
					
						
							| 
									
										
										
										
											2016-10-16 21:29:45 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-16 20:37:48 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-23 14:12:00 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-29 14:05:17 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 	module.processSortedSet = async function (setKey, processFn, options) { | 
					
						
							| 
									
										
										
										
											2016-03-01 21:38:36 +02:00
										 |  |  | 		var done = false; | 
					
						
							|  |  |  | 		var ids = []; | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 		var project = { _id: 0, _key: 0 }; | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-26 15:02:57 -04:00
										 |  |  | 		if (!options.withScores) { | 
					
						
							|  |  |  | 			project.score = 0; | 
					
						
							| 
									
										
										
										
											2018-06-22 16:28:19 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-05 09:20:00 -04:00
										 |  |  | 		var cursor = await module.client.collection('objects').find({ _key: setKey }, { projection: project }) | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			.sort({ score: 1 }) | 
					
						
							| 
									
										
										
										
											2017-06-23 18:18:34 -04:00
										 |  |  | 			.batchSize(options.batch); | 
					
						
							| 
									
										
										
										
											2016-02-29 14:05:17 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 		if (processFn && processFn.constructor && processFn.constructor.name !== 'AsyncFunction') { | 
					
						
							|  |  |  | 			processFn = util.promisify(processFn); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		while (!done) { | 
					
						
							|  |  |  | 			/* eslint-disable no-await-in-loop */ | 
					
						
							|  |  |  | 			const item = await cursor.next(); | 
					
						
							|  |  |  | 			if (item === null) { | 
					
						
							|  |  |  | 				done = true; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ids.push(options.withScores ? item : item.value); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (ids.length >= options.batch || (done && ids.length !== 0)) { | 
					
						
							|  |  |  | 				await processFn(ids); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				ids.length = 0; | 
					
						
							|  |  |  | 				if (options.interval) { | 
					
						
							|  |  |  | 					await sleep(options.interval); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-29 14:05:17 -06:00
										 |  |  | 	}; | 
					
						
							|  |  |  | }; |